<?php

namespace PloverCRM\Modules\Integrations\Services;

use PloverCRM\Common\BaseLogger;

/**
 * Ads Integration Logger
 * 
 * Handles logging for ads-based integrations (Meta Lead Ads, Google Ads, etc.)
 * Similar to AutomationsLogger - logs important data as JSON
 */
class AdsLogger extends BaseLogger
{
  /**
   * Constructor
   */
  public function __construct()
  {
    parent::__construct(
      'data_sources_ads',
      'data-sources/ads',
      'ads',
      [
        'module' => 'integrations',
        'feature' => 'data-sources',
      ]
    );
  }

  /**
   * Log lead received from ad platform
   *
   * @param string $provider Provider key (meta_lead_ads, etc.)
   * @param string $formId Lead form ID
   * @param string $leadgenId Lead ID
   * @param array $rawData Raw webhook payload (contains all lead data)
   */
  public function logLeadReceived(string $provider, string $formId, string $leadgenId, array $rawData): void
  {
    $this->logInfo('Lead received from ad platform', [
      'provider' => $provider,
      'form_id' => $formId,
      'leadgen_id' => $leadgenId,
      'raw_payload' => json_encode($rawData),
    ]);
  }

  /**
   * 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 (as JSON string)
   * @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 (as JSON string)
   * @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
   *
   * @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,
    ];

    // Add context data as JSON if present
    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('Lead processing failed', $logData);
  }

  /**
   * Log field mapping info
   *
   * @param string $provider Provider key
   * @param string $formId Form ID
   * @param int $integrationId Integration ID
   * @param array $mappedFields Mapped fields (source => target) as JSON
   */
  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' => json_encode($mappedFields),
    ]);
  }

  /**
   * 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
   * @param array $rawData Raw data for reference
   */
  public function logUnmappedFields(string $provider, string $formId, int $integrationId, array $unmappedFields, array $rawData): void
  {
    $this->logWarning('Unmapped fields detected', [
      'provider' => $provider,
      'form_id' => $formId,
      'integration_id' => $integrationId,
      'unmapped_fields' => json_encode($unmappedFields),
      'available_fields' => json_encode(array_keys($rawData)),
    ]);
  }

  /**
   * Log webhook received
   *
   * @param string $provider Provider key
   * @param string $pageId Page ID that received the lead
   * @param int $entriesCount Number of entries in webhook
   */
  public function logWebhookReceived(string $provider, string $pageId, int $entriesCount): void
  {
    $this->logInfo('Webhook received', [
      'provider' => $provider,
      'page_id' => $pageId,
      'entries_count' => $entriesCount,
    ]);
  }

  /**
   * Log signature validation failed
   *
   * @param string $provider Provider key
   */
  public function logSignatureValidationFailed(string $provider): void
  {
    $this->logError('Signature validation failed', [
      'provider' => $provider,
    ]);
  }

  /**
   * Log test lead received
   *
   * @param string $provider Provider key
   * @param string $formId Form ID
   * @param int $fieldsCount Number of fields in test lead
   * @param array $fieldKeys Array of field keys
   */
  public function logTestLeadReceived(string $provider, string $formId, int $fieldsCount, array $fieldKeys): void
  {
    $this->logInfo('Test lead received', [
      'provider' => $provider,
      'form_id' => $formId,
      'fields_count' => $fieldsCount,
      'field_keys' => json_encode($fieldKeys),
    ]);
  }

  /**
   * Log info message (public wrapper)
   *
   * @param string $message
   * @param array $context
   */
  public function info(string $message, array $context = []): void
  {
    $this->logInfo($message, $context);
  }

  /**
   * Log warning message (public wrapper)
   *
   * @param string $message
   * @param array $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);
  }
}
