<?php

namespace PloverCRM\Modules\Integrations\Services;

use PloverCRM\Common\BaseLogger;

/**
 * Webhooks Integration Logger
 * 
 * Handles logging for incoming webhook integrations
 */
class WebhooksLogger extends BaseLogger
{
  public function __construct()
  {
    parent::__construct(
      'data_sources_webhooks',
      'data-sources/webhooks',
      'webhooks',
      [
        'module' => 'integrations',
        'feature' => 'data-sources',
      ]
    );
  }

  /**
   * Log webhook payload received
   */
  public function logPayloadReceived(string $webhookName, string $webhookId, int $fieldsCount, array $fieldKeys): void
  {
    $this->logInfo('Webhook payload received', [
      'webhook_name' => $webhookName,
      'webhook_id' => $webhookId,
      'fields_count' => $fieldsCount,
      'field_keys' => json_encode($fieldKeys),
    ]);
  }

  /**
   * Log contact created successfully
   */
  public function logContactCreated(
    string $provider,
    string $sourceId,
    int $integrationId,
    int $contactId,
    string $contactEmail,
    array $mappedData,
    int $executionTimeMs
  ): void {
    $this->logInfo('Contact created successfully', [
      'provider' => $provider,
      'webhook_id' => $sourceId,
      'integration_id' => $integrationId,
      'contact_id' => $contactId,
      'contact_email' => $contactEmail,
      'mapped_data' => json_encode($mappedData),
      'execution_time_ms' => $executionTimeMs,
    ]);
  }

  /**
   * Log contact updated successfully
   */
  public function logContactUpdated(
    string $provider,
    string $sourceId,
    int $integrationId,
    int $contactId,
    string $contactEmail,
    array $mappedData,
    int $executionTimeMs
  ): void {
    $this->logInfo('Contact updated successfully', [
      'provider' => $provider,
      'webhook_id' => $sourceId,
      'integration_id' => $integrationId,
      'contact_id' => $contactId,
      'contact_email' => $contactEmail,
      'mapped_data' => json_encode($mappedData),
      'execution_time_ms' => $executionTimeMs,
    ]);
  }

  /**
   * Log processing error
   */
  public function logProcessingError(string $provider, string $sourceId, int $integrationId, string $error, array $context = []): void
  {
    $logData = [
      'provider' => $provider,
      'webhook_id' => $sourceId,
      '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('Webhook processing failed', $logData);
  }

  /**
   * Log unmapped fields detected
   */
  public function logUnmappedFields(string $provider, string $sourceId, int $integrationId, array $unmappedFields, array $rawData): void
  {
    $this->logWarning('Unmapped fields detected', [
      'provider' => $provider,
      'webhook_id' => $sourceId,
      'integration_id' => $integrationId,
      'unmapped_fields' => json_encode($unmappedFields),
    ]);
  }

  /**
   * Log info message (public wrapper)
   */
  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);
  }
}
