<?php

namespace PloverCRM\Modules\Integrations\Services;

use PloverCRM\Common\BaseLogger;

/**
 * Automations Logger Service
 * 
 * Handles logging for Automations (Google Ads and Meta CAPI outgoing conversions)
 */
class AutomationsLogger extends BaseLogger
{
    public function __construct()
    {
        parent::__construct(
            'automations',           // Channel name
            'automations',           // Log directory name
            'automations',           // Log file prefix
            [
                'module' => 'automations',
                'feature' => 'automations',
            ]
        );
    }

    // ==========================================
    // Google Ads Logging
    // ==========================================

    /**
     * Log conversion sent to Google Ads
     *
     * @param string $conversionActionId Conversion action ID
     * @param string $customerId Google Ads customer ID
     * @param int $subscriberId Contact ID
     * @param string $requestBody JSON request payload
     * @param float $executionTimeMs Execution time in milliseconds
     * @param int $responseCode HTTP response code
     * @param string $responseBody Response body
     */
    public function logGoogleConversionSent(
        string $conversionActionId,
        string $customerId,
        int $subscriberId,
        string $requestBody,
        float $executionTimeMs,
        int $responseCode,
        string $responseBody
    ): void {
        $this->logInfo("Conversion sent to Google Ads", [
            'conversion_action_id' => $conversionActionId,
            'customer_id' => $customerId,
            'platform' => 'google',
            'subscriber_id' => $subscriberId,
            'request_body' => $requestBody,
            'execution_time_ms' => $executionTimeMs,
            'response_code' => $responseCode,
            'response_body' => $responseBody
        ]);
    }

    /**
     * Log conversion creation
     *
     * @param string $stage Stage name
     * @param string $conversionId Conversion action ID
     * @param bool $isNew Whether this is a new creation or reused existing
     */
    public function logConversionCreated(string $stage, string $conversionId, bool $isNew = true): void
    {
        $action = $isNew ? 'created' : 'reused';
        $this->logInfo("Conversion action {$action}", [
            'stage' => $stage,
            'conversion_id' => $conversionId,
            'is_new' => $isNew,
            'platform' => 'google'
        ]);
    }

    // ==========================================
    // Meta CAPI Logging
    // ==========================================

    /**
     * Log event sent to Meta CAPI
     *
     * @param string $stage Stage name
     * @param string $datasetId Meta dataset ID
     * @param array $context Additional context
     */
    public function logMetaEventSent(string $stage, string $datasetId, array $context = []): void
    {
        $this->logInfo("Event sent to Meta CAPI", array_merge([
            'stage' => $stage,
            'dataset_id' => $datasetId,
            'platform' => 'meta'
        ], $context));
    }

    /**
     * Log Meta API error
     *
     * @param string $stage Stage name
     * @param string $error Error message
     * @param array $context Additional context
     */
    public function logMetaApiError(string $stage, string $error, array $context = []): void
    {
        $this->logError("Meta CAPI error", array_merge([
            'stage' => $stage,
            'error' => $error,
            'platform' => 'meta'
        ], $context));
    }

    // ==========================================
    // Common Logging
    // ==========================================

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

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

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

    /**
     * Log API error (generic)
     *
     * @param string $stage Stage name
     * @param string $error Error message
     * @param array $context Additional context
     */
    public function logApiError(string $stage, string $error, array $context = []): void
    {
        $this->logError("API error", array_merge([
            'stage' => $stage,
            'error' => $error,
        ], $context));
    }

    /**
     * Log connection/disconnection
     *
     * @param string $action 'connected' or 'disconnected'
     * @param string $platform 'google' or 'meta'
     * @param string $id Connection ID
     * @param string $name Connection name
     */
    public function logConnection(string $action, string $platform, string $id, string $name): void
    {
        $this->logInfo("Account {$action}", [
            'platform' => $platform,
            'connection_id' => $id,
            'connection_name' => $name,
            '_activity' => [
                'module' => 'integrations',
                'feature' => 'connections',
            ],
        ]);
    }
}
