<?php
declare(strict_types=1);
// ==================================================
// Nuclear Fancy Pants Edition™ -- NUCLEAR+++
// File: /home/ploi/automarketer-ai.com/public/wp-content/plugins/automarketer-ai/modules/analytics-ga4.php
// Description: Google Analytics 4 module -- connects GA4 property, syncs per-page metrics - NUCLEAR+++
// Last Edited: 2026-05-18
// ==================================================

namespace AutoMarketerAI\Modules;

defined('ABSPATH') || exit;

use AutoMarketerAI\ModuleInterface;
use AutoMarketerAI\Ga4Auth;
use AutoMarketerAI\Ga4Sync;

class AnalyticsGa4 implements ModuleInterface
{
    public function get_id(): string
    {
        return 'analytics-ga4';
    }

    public function get_name(): string
    {
        return 'Google Analytics 4';
    }

    public function get_description(): string
    {
        return 'Connect your GA4 property to see real traffic data for every post -- sessions, pageviews, and trending direction. Powers content decay alerts and post performance insights.';
    }

    public function get_logo_url(): string
    {
        return AMAI_PLUGIN_URL . 'assets/img/modules/analytics-ga4.svg';
    }

    public function get_category(): string
    {
        return 'analytics';
    }

    public function get_model_count(): int
    {
        return 0;
    }

    public function get_available_models(): array
    {
        return [];
    }

    public function get_single_model_notice(): string
    {
        return '';
    }

    public function get_api_key_prefix(): string
    {
        return '';
    }

    public function validate_api_key(string $api_key): bool
    {
        return true;
    }

    public function register(): void
    {
        // Register OAuth callback route
        add_action('init', static function (): void {
            add_rewrite_rule('^ga4/callback/?$', 'index.php?amai_ga4_callback=1', 'top');
        });

        add_filter('query_vars', static function (array $vars): array {
            $vars[] = 'amai_ga4_callback';
            return $vars;
        });

        add_action('template_redirect', static function (): void {
            if ((string) get_query_var('amai_ga4_callback') !== '1') {
                return;
            }
            Ga4Auth::handleCallback();
        });

        // Register cron hooks
        add_action(Ga4Auth::CRON_HOOK,  [Ga4Auth::class, 'refreshAccessToken']);
        add_action(Ga4Sync::CRON_HOOK,  [Ga4Sync::class, 'run']);

        // Register AJAX disconnect handler
        add_action('wp_ajax_amai_ga4_disconnect', static function (): void {
            check_ajax_referer('amai_ga4_disconnect', 'nonce');
            if (!current_user_can('manage_automarketer_ai')) {
                wp_send_json_error(['message' => 'Unauthorized.'], 403);
            }
            Ga4Auth::disconnect();
            Ga4Sync::unscheduleCron();
            wp_send_json_success(['disconnected' => true]);
        });

        // Register AJAX manual sync handler
        add_action('wp_ajax_amai_ga4_sync_now', static function (): void {
            check_ajax_referer('amai_ga4_sync', 'nonce');
            if (!current_user_can('manage_automarketer_ai')) {
                wp_send_json_error(['message' => 'Unauthorized.'], 403);
            }
            Ga4Sync::run();
            wp_send_json_success(['synced' => true]);
        });
    }

    public function generate_raw(string $prompt, string $api_key, string $model, int $max_tokens = 64): string
    {
        return '';
    }
}