<?php

namespace App\Services;

use App\Jobs\TrackContentSelectionJob;
use App\Jobs\TrackExportJob;
use App\Jobs\TrackSaveJob;
use App\Jobs\TrackSearchJob;
use App\Jobs\UpdateExportStatusJob;
use Carbon\Carbon;

class TrackingAnalyticsService
{
    /**
     * Track a search action with full context
     */
    public function trackSearchWithContext(
        string $deviceId,
        string $searchType,
        string $searchQuery,
        ?array $searchFilters = null,
        ?Carbon $actionAt = null,
        ?string $userAgent = null,
        ?string $ipAddress = null,
        string $platform = 'web'
    ): void {
        TrackSearchJob::dispatch(
            $deviceId,
            $searchType,
            $searchQuery,
            $searchFilters,
            $actionAt ?? now(),
            $userAgent,
            $ipAddress,
            $platform
        );
    }

    /**
     * Track a content selection action with full context
     */
    public function trackContentSelectionWithContext(
        string $deviceId,
        string $contentType,
        int $contentId,
        string $contentName,
        string $selectionMethod,
        ?Carbon $actionAt = null,
        ?string $userAgent = null,
        ?string $ipAddress = null,
        string $platform = 'web'
    ): void {
        TrackContentSelectionJob::dispatch(
            $deviceId,
            $contentType,
            $contentId,
            $contentName,
            $selectionMethod,
            $actionAt ?? now(),
            $userAgent,
            $ipAddress,
            $platform
        );
    }

    /**
     * Track an export action with full context
     */
    public function trackExportWithContext(
        string $deviceId,
        ?int $memeId,
        ?int $memeMediaId,
        ?int $backgroundMediaId,
        array $captionTexts,
        ?Carbon $actionAt = null,
        ?string $userAgent = null,
        ?string $ipAddress = null,
        string $platform = 'web'
    ): void {
        TrackExportJob::dispatch(
            $deviceId,
            $memeId,
            $memeMediaId,
            $backgroundMediaId,
            $captionTexts,
            $actionAt ?? now(),
            $userAgent,
            $ipAddress,
            $platform
        );
    }

    /**
     * Track a save action with full context
     */
    public function trackSaveWithContext(
        string $deviceId,
        ?int $memeId,
        ?int $memeMediaId,
        ?int $backgroundMediaId,
        array $captionTexts,
        bool $isPremiumExport,
        ?Carbon $actionAt = null,
        ?string $userAgent = null,
        ?string $ipAddress = null,
        string $platform = 'web'
    ): void {
        TrackSaveJob::dispatch(
            $deviceId,
            $memeId,
            $memeMediaId,
            $backgroundMediaId,
            $captionTexts,
            $isPremiumExport,
            $actionAt ?? now(),
            $userAgent,
            $ipAddress,
            $platform
        );
    }

    /**
     * Update export status
     */
    public function updateExportStatus(
        int $trackingExportId,
        string $status,
        ?string $errorMessage = null,
        ?Carbon $completedAt = null
    ): void {
        UpdateExportStatusJob::dispatch(
            $trackingExportId,
            $status,
            $errorMessage,
            $completedAt
        );
    }

    /**
     * Get device context from request
     */
    public function getDeviceContext(): array
    {
        $request = request();

        return [
            'user_agent' => $request->userAgent(),
            'ip_address' => get_current_ip(),
            'platform' => 'web', // Default for now, can be enhanced for mobile detection
        ];
    }

    /**
     * Generate a device ID from request
     */
    public function generateDeviceId(): string
    {
        $request = request();

        // Generate a consistent device ID based on session or create new one
        if ($request->session()->has('device_id')) {
            return $request->session()->get('device_id');
        }

        $deviceId = str()->uuid()->toString();
        $request->session()->put('device_id', $deviceId);

        return $deviceId;
    }

    /**
     * Track methods with auto device context
     */
    public function trackSearch(string $searchType, string $searchQuery, ?array $searchFilters = null): void
    {
        $context = $this->getDeviceContext();
        $deviceId = $this->generateDeviceId();

        $this->trackSearchWithContext(
            $deviceId,
            $searchType,
            $searchQuery,
            $searchFilters,
            null,
            $context['user_agent'],
            $context['ip_address'],
            $context['platform']
        );
    }

    public function trackContentSelection(string $contentType, int $contentId, string $contentName, string $selectionMethod): void
    {
        $context = $this->getDeviceContext();
        $deviceId = $this->generateDeviceId();

        $this->trackContentSelectionWithContext(
            $deviceId,
            $contentType,
            $contentId,
            $contentName,
            $selectionMethod,
            null,
            $context['user_agent'],
            $context['ip_address'],
            $context['platform']
        );
    }

    public function trackExport(?int $memeId, ?int $memeMediaId, ?int $backgroundMediaId, array $captionTexts): void
    {
        $context = $this->getDeviceContext();
        $deviceId = $this->generateDeviceId();

        $this->trackExportWithContext(
            $deviceId,
            $memeId,
            $memeMediaId,
            $backgroundMediaId,
            $captionTexts,
            null,
            $context['user_agent'],
            $context['ip_address'],
            $context['platform']
        );
    }

    public function trackSave(?int $memeId, ?int $memeMediaId, ?int $backgroundMediaId, array $captionTexts, bool $isPremiumExport = false): void
    {
        $context = $this->getDeviceContext();
        $deviceId = $this->generateDeviceId();

        $this->trackSaveWithContext(
            $deviceId,
            $memeId,
            $memeMediaId,
            $backgroundMediaId,
            $captionTexts,
            $isPremiumExport,
            null,
            $context['user_agent'],
            $context['ip_address'],
            $context['platform']
        );
    }
}
