<?php
/**
 * Main plugin class
 *
 * @package LLMagnet_AI_SEO_Optimizer
 */

namespace LLMagnet_AI_SEO_Optimizer;

// Explicitly require classes that might not be autoloaded correctly
require_once LLMAGNET_AISEO_PLUGIN_DIR . 'includes/class-analytics.php';
require_once LLMAGNET_AISEO_PLUGIN_DIR . 'includes/class-email-reports.php';
require_once LLMAGNET_AISEO_PLUGIN_DIR . 'includes/class-visibility-score.php';
require_once LLMAGNET_AISEO_PLUGIN_DIR . 'includes/class-attribution.php';
require_once LLMAGNET_AISEO_PLUGIN_DIR . 'includes/class-product-analytics.php';
require_once LLMAGNET_AISEO_PLUGIN_DIR . 'includes/class-woocommerce.php';
require_once LLMAGNET_AISEO_PLUGIN_DIR . 'includes/class-robots-txt.php';
require_once LLMAGNET_AISEO_PLUGIN_DIR . 'includes/class-mcp.php';
require_once LLMAGNET_AISEO_PLUGIN_DIR . 'includes/class-page-details.php';
require_once LLMAGNET_AISEO_PLUGIN_DIR . 'includes/class-schema-jsonld.php';
require_once LLMAGNET_AISEO_PLUGIN_DIR . 'includes/class-news-feed.php';

// Lifecycle emails classes
require_once LLMAGNET_AISEO_PLUGIN_DIR . 'includes/class-lifecycle-helpers.php';
require_once LLMAGNET_AISEO_PLUGIN_DIR . 'includes/class-brevo-key-store.php';
require_once LLMAGNET_AISEO_PLUGIN_DIR . 'includes/class-brevo-client.php';
require_once LLMAGNET_AISEO_PLUGIN_DIR . 'includes/class-filesystem-helper.php';
require_once LLMAGNET_AISEO_PLUGIN_DIR . 'includes/class-email-state.php';
require_once LLMAGNET_AISEO_PLUGIN_DIR . 'includes/class-email-log.php';
require_once LLMAGNET_AISEO_PLUGIN_DIR . 'includes/class-lifecycle-emails.php';

/**
 * Main plugin class
 */
class Main {
    /**
     * Admin instance
     *
     * @var Admin
     */
    private $admin;

    /**
     * Generator instance
     *
     * @var Generator
     */
    private $generator;

    /**
     * Cron instance
     *
     * @var Cron
     */
    private $cron;
    
    /**
     * Analytics instance
     *
     * @var Analytics
     */
    private $analytics;
    
    /**
     * Email Reports instance
     *
     * @var Email_Reports
     */
    private $email_reports;

    /**
     * Visibility Score instance
     *
     * @var Visibility_Score
     */
    private $visibility_score;

    /**
     * WooCommerce integration instance
     *
     * @var WooCommerce
     */
    private $woocommerce;

    /**
     * Robots_Txt instance
     *
     * @var Robots_Txt
     */
    private $robots_txt;

    /**
     * MCP server instance
     *
     * @var MCP
     */
    private $mcp;

    /**
     * Page Details instance
     *
     * @var Page_Details
     */
    private $page_details;

    /**
     * Schema JSON-LD instance
     *
     * @var Schema_Jsonld
     */
    private $schema_jsonld;

    /**
     * News feed instance
     *
     * @var News_Feed
     */
    private $news_feed;

    /**
     * Lifecycle Emails instance
     *
     * @var \LLMagnet\Lifecycle\Lifecycle_Emails
     */
    private $lifecycle_emails;

    /**
     * Email State instance
     *
     * @var \LLMagnet\Lifecycle\Email_State
     */
    private $email_state;

    /**
     * Brevo Client instance
     *
     * @var \LLMagnet\Lifecycle\Brevo_Client
     */
    private $brevo_client;

    /**
     * Initialize the plugin
     *
     * @return void
     */
    public function init() {
        // Initialize components
        $this->init_components();

        // Setup hooks
        $this->setup_hooks();
    }



    /**
     * Initialize plugin components
     *
     * @return void
     */
    private function init_components() {
        Brevo_Key_Store::maybe_migrate_from_constant();

        // Initialize generator
        $this->generator = new Generator();
        
        // Initialize analytics first so it can be used by Admin
        $this->analytics = new Analytics();
        $this->analytics->init();
        
        // Initialize admin with the analytics instance
        // Always initialize Admin to ensure REST routes are registered
        $this->admin = new Admin($this->generator, $this->analytics);

        // Initialize cron with visibility score instance
        $this->visibility_score = new Visibility_Score();
        $this->visibility_score->init();

        // Initialize email reports before cron so scheduled report callbacks can use the shared instance
        $this->email_reports = new Email_Reports($this->analytics);

        // Initialize cron with generator, visibility score, and email reports
        $this->cron = new Cron($this->generator, $this->visibility_score, $this->email_reports);
        
        // Initialize lifecycle emails components
        $this->brevo_client = new \LLMagnet\Lifecycle\Brevo_Client();
        $this->email_state = new \LLMagnet\Lifecycle\Email_State();
        $email_log = new \LLMagnet\Lifecycle\Email_Log();
        
        $this->lifecycle_emails = new \LLMagnet\Lifecycle\Lifecycle_Emails(
            $this->brevo_client,
            $this->email_state,
            $email_log,
            $this->analytics,
            $this->visibility_score
        );
        $this->lifecycle_emails->init();
        
        // Make the email reports instance globally accessible
        global $llmagnet_email_reports;
        $llmagnet_email_reports = $this->email_reports;
        
        // Make the main plugin instance globally accessible
        global $llmagnet_plugin;
        $llmagnet_plugin = $this;
        
        // Make the visibility score instance globally accessible
        global $llmagnet_visibility_score;
        $llmagnet_visibility_score = $this->visibility_score;

        // Initialize robots.txt integration
        $this->robots_txt = new Robots_Txt();
        $this->robots_txt->init();

        // Initialize WooCommerce integration
        $this->woocommerce = new WooCommerce();
        $this->woocommerce->init();

        // Initialize MCP server
        $this->mcp = new MCP( $this->analytics, $this->visibility_score );
        $this->mcp->init();

        // Initialize page details (for page/post scoring and drawer)
        $this->page_details = new Page_Details();
        add_action('rest_api_init', [$this->page_details, 'register_rest_routes']);

        $this->schema_jsonld = new Schema_Jsonld();
        $this->schema_jsonld->init();

        $this->news_feed = new News_Feed();
        add_action('rest_api_init', [$this->news_feed, 'register_rest_routes']);

        // Make the WooCommerce instance globally accessible
        global $llmagnet_woocommerce;
        $llmagnet_woocommerce = $this->woocommerce;
    }

    /**
     * Setup plugin hooks
     *
     * @return void
     */
    private function setup_hooks() {
        // Post save hook covers create + update; avoid duplicate post_updated firing.
        add_action('save_post', [$this->generator, 'maybe_regenerate'], 10, 2);
        
        // Add llms.txt link tag to wp_head for discoverability
        add_action('wp_head', [$this, 'add_llms_txt_link_tag']);
        
        // Register WP-CLI commands if available
        if (defined('WP_CLI') && WP_CLI) {
            $this->register_cli_commands();
        }
    }

    /**
     * Add llms.txt link tag to the head section
     *
     * @return void
     */
    public function add_llms_txt_link_tag() {
        echo '<link rel="alternate" type="text/plain" href="' . esc_url(home_url('/llms.txt')) . '">' . "\n";

        $llms_full_path = trailingslashit(ABSPATH) . 'llms-full.txt';
        if (file_exists($llms_full_path)) {
            echo '<link rel="alternate" type="text/plain" href="' . esc_url(home_url('/llms-full.txt')) . '">' . "\n";
        }
    }

    /**
     * Register WP-CLI commands
     *
     * @return void
     */
    private function register_cli_commands() {
        \WP_CLI::add_command('llmagnet-ai-seo', new CLI($this->generator, $this->lifecycle_emails));
    }
} 