<?php

namespace PloverCRM\Modules\Integrations\Services;

use PloverCRM\Common\BaseLogger;

/**
 * Forms Integration Logger
 * 
 * Handles logging for form-based integrations
 */
class FormsLogger extends BaseLogger
{
  /**
   * Constructor
   */
  public function __construct()
  {
    parent::__construct(
      'data_sources_forms',
      'data-sources/forms',
      'forms',
      [
        'module' => 'integrations',
        'feature' => 'data-sources',
      ]
    );
  }

  /**
   * Log form submission received
   *
   * @param string $provider Provider key (fluent_form, wpforms, etc.)
   * @param string $formId Form ID
   * @param string $formName Form name
   * @param array $rawData Raw form data received
   */
  public function logFormSubmission(string $provider, string $formId, string $formName, array $rawData): void
  {
    $this->logInfo('Form submission received', [
      'provider' => $provider,
      'form_id' => $formId,
      'form_name' => $formName,
      'fields_count' => count($rawData),
      'fields' => array_keys($rawData),
    ]);
  }

  /**
   * Log field mapping completed
   *
   * @param string $provider Provider key
   * @param string $formId Form ID
   * @param int $integrationId Integration ID
   * @param array $mappedFields Mapped fields (source => target)
   */
  public function logFieldMapping(string $provider, string $formId, int $integrationId, array $mappedFields): void
  {
    $this->logInfo('Field mapping completed', [
      'provider' => $provider,
      'form_id' => $formId,
      'integration_id' => $integrationId,
      'mapped_fields' => $mappedFields,
    ]);
  }

  /**
   * Log contact created successfully
   *
   * @param string $provider Provider key
   * @param string $formId Form ID
   * @param int $integrationId Integration ID
   * @param int $contactId Created contact ID
   * @param string $contactEmail Contact email
   * @param array $mappedData Mapped contact data
   * @param int $executionTimeMs Execution time in milliseconds
   */
  public function logContactCreated(string $provider, string $formId, int $integrationId, int $contactId, string $contactEmail, array $mappedData, int $executionTimeMs): void
  {
    $this->logInfo('Contact created successfully', [
      'provider' => $provider,
      'form_id' => $formId,
      'integration_id' => $integrationId,
      'contact_id' => $contactId,
      'contact_email' => $contactEmail,
      'mapped_data' => json_encode($mappedData),
      'execution_time_ms' => $executionTimeMs,
    ]);
  }

  /**
   * Log contact updated successfully
   *
   * @param string $provider Provider key
   * @param string $formId Form ID
   * @param int $integrationId Integration ID
   * @param int $contactId Updated contact ID
   * @param string $contactEmail Contact email
   * @param array $mappedData Mapped contact data
   * @param int $executionTimeMs Execution time in milliseconds
   */
  public function logContactUpdated(string $provider, string $formId, int $integrationId, int $contactId, string $contactEmail, array $mappedData, int $executionTimeMs): void
  {
    $this->logInfo('Contact updated successfully', [
      'provider' => $provider,
      'form_id' => $formId,
      'integration_id' => $integrationId,
      'contact_id' => $contactId,
      'contact_email' => $contactEmail,
      'mapped_data' => json_encode($mappedData),
      'execution_time_ms' => $executionTimeMs,
    ]);
  }

  /**
   * Log processing error.
   *
   * Keeps the form logger compatible with the shared data-source pipeline,
   * which expects each category logger to expose a generic processing-error API.
   *
   * @param string $provider Provider key
   * @param string $formId Form ID
   * @param int $integrationId Integration ID
   * @param string $error Error message
   * @param array $context Additional context (raw_data, mapped_data, etc.)
   */
  public function logProcessingError(string $provider, string $formId, int $integrationId, string $error, array $context = []): void
  {
    $logData = [
      'provider' => $provider,
      'form_id' => $formId,
      'integration_id' => $integrationId,
      'error' => $error,
    ];

    if (isset($context['raw_data'])) {
      $logData['raw_payload'] = json_encode($context['raw_data']);
    }

    if (isset($context['mapped_data'])) {
      $logData['mapped_data'] = json_encode($context['mapped_data']);
    }

    if (isset($context['stage'])) {
      $logData['stage'] = $context['stage'];
    }

    $this->logError('Form processing failed', $logData);
  }

  /**
   * Log validation error
   *
   * @param string $provider Provider key
   * @param string $formId Form ID
   * @param int $integrationId Integration ID
   * @param string $error Error message
   * @param array $data Data that failed validation
   */
  public function logValidationError(string $provider, string $formId, int $integrationId, string $error, array $data): void
  {
    $this->logError('Data validation failed', [
      'provider' => $provider,
      'form_id' => $formId,
      'integration_id' => $integrationId,
      'error' => $error,
      'data' => $data,
    ]);
  }

  /**
   * Log field mapping error
   *
   * @param string $provider Provider key
   * @param string $formId Form ID
   * @param int $integrationId Integration ID
   * @param string $error Error message
   * @param array $rawData Raw data
   */
  public function logMappingError(string $provider, string $formId, int $integrationId, string $error, array $rawData): void
  {
    $this->logError('Field mapping failed', [
      'provider' => $provider,
      'form_id' => $formId,
      'integration_id' => $integrationId,
      'error' => $error,
      'raw_data' => $rawData,
    ]);
  }

  /**
   * Log provider error
   *
   * @param string $provider Provider key
   * @param string $formId Form ID
   * @param string $error Error message
   */
  public function logProviderError(string $provider, string $formId, string $error): void
  {
    $this->logError('Provider error', [
      'provider' => $provider,
      'form_id' => $formId,
      'error' => $error,
    ]);
  }

  /**
   * Log integration not found
   *
   * @param string $provider Provider key
   * @param string $formId Form ID
   */
  public function logIntegrationNotFound(string $provider, string $formId): void
  {
    $this->logWarning('No active integration found', [
      'provider' => $provider,
      'form_id' => $formId,
    ]);
  }

  /**
   * Log CRM provider error
   *
   * @param string $provider Provider key
   * @param string $formId Form ID
   * @param int $integrationId Integration ID
   * @param string $error Error message
   */
  public function logCrmError(string $provider, string $formId, int $integrationId, string $error): void
  {
    $this->logError('CRM operation failed', [
      'provider' => $provider,
      'form_id' => $formId,
      'integration_id' => $integrationId,
      'error' => $error,
    ]);
  }

  /**
   * Log unmapped fields detected
   *
   * @param string $provider Provider key
   * @param string $formId Form ID
   * @param int $integrationId Integration ID
   * @param array $unmappedFields Array of unmapped field keys
   */
  public function logUnmappedFields(string $provider, string $formId, int $integrationId, array $unmappedFields): void
  {
    $this->logWarning('Unmapped fields detected in form submission', [
      'provider' => $provider,
      'form_id' => $formId,
      'integration_id' => $integrationId,
      'unmapped_fields' => $unmappedFields,
      'message' => 'Form fields may have changed. Review integration field mappings.',
    ]);
  }

  public function info(string $message, array $context = []): void
  {
    $this->logInfo($message, $context);
  }

  public function warning(string $message, array $context = []): void
  {
    $this->logWarning($message, $context);
  }

  public function error(string $message, array $context = []): void
  {
    $this->logError($message, $context);
  }
}
