<?php
namespace Lean_CSNS;

if (!defined('ABSPATH')) {
    exit;
}

// Trait must load before the class is declared (use Lifecycle resolves at compile time).
require_once LEAN_CSNS_PL_DIR . '/includes/trait-lifecycle.php';

// Early deps: functions.php for freemius-init; class-database.php so the
// activation hook (fires before plugins_loaded) has Database ready.
require_once LEAN_CSNS_PL_DIR . '/includes/functions.php';
require_once LEAN_CSNS_PL_DIR . '/includes/functions-theme-compat.php';
require_once LEAN_CSNS_PL_DIR . '/includes/class-database.php';

/**
 * Main Plugin Base Class
 * 
 * Handles core plugin initialization
 */
class Base {

    // Activation/deactivation/uninstall callbacks live in the Lifecycle trait.
    use Lifecycle;

    /**
     * Singleton instance
     */
    private static $instance = null;

    /**
     * Get singleton instance
     */
    public static function get_instance() {
        if (null === self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Bootstrap the plugin
     * Called from main plugin file before plugins_loaded action
     */
    public static function bootstrap($plugin_file) {
        /**
         * Fires early in bootstrap, before plugins_loaded action
         * Perfect for Freemius SDK initialization and other early pro setup
         * 
         * @param string $plugin_file Main plugin file path
         */
        do_action('lean_csns/bootstrap/early_init', $plugin_file);

        // Check if we should load pro features (respects LEAN_CSNS_MODE_SWITCH)
        $should_load_pro = self::should_load_pro();
        
        // Load pro-loader early if it exists and we're in pro mode
        if ($should_load_pro) {
            $pro_loader_path = LEAN_CSNS_PL_DIR . '/pro/pro-loader.php';
            if (file_exists($pro_loader_path)) {
                require_once $pro_loader_path;
            }
        }

        // Must run AFTER load_pro() so lean_csns_is_pro_active() reads the pro/ folder correctly.
        require_once LEAN_CSNS_PL_DIR . 'includes/freemius-init.php';
        
        // Register activation hook
        register_activation_hook($plugin_file, [__CLASS__, 'on_activation']);

        // Ensure only one version is active
        require_once LEAN_CSNS_PL_DIR . '/includes/class-dual-activation-preventer.php';
        Dual_Activation_Preventer::ensure_single_activation();

        register_deactivation_hook($plugin_file, [__CLASS__, 'on_deactivation']);

        // Uninstall wipe is handled by uninstall.php

        // Initialize on plugins_loaded
        add_action('plugins_loaded', [__CLASS__, 'plugins_loaded_cb'], 10);
    }

    /**
     * Check if pro features should be loaded
     * Respects LEANPL_MODE_SWITCH for development mode switching
     * 
     * @return bool True if pro mode is active
     */
    private static function should_load_pro() {
        // 1. Check if this is standalone PRO (folder has -pro suffix)
        if (strpos(plugin_basename(LEAN_CSNS_PL_FILE), '-pro/') !== false) {
            return true;
        }
        
        // 2. Development: Check mode switch
        if (defined('LEAN_CSNS_MODE_SWITCH') && LEAN_CSNS_MODE_SWITCH === 'pro') {
            return true;
        }
        
        // 3. Default: FREE mode
        return false;
    }

    /**
     * Callback for plugins_loaded action
     */
    public static function plugins_loaded_cb() {
        // Initialize base plugin
        self::get_instance();

        // Check for database updates
        lean_csns_check_database_version();

        // Load Pro features if available
        self::load_pro();
    }

    /**
     * Load Pro features if available
     */
    private static function load_pro() {
        if (self::should_load_pro() && file_exists(LEAN_CSNS_PL_DIR . '/pro/pro-loader.php')) {
            require_once LEAN_CSNS_PL_DIR . '/pro/pro-loader.php';
            
            if (function_exists('lean_csns_pro_init')) {
                lean_csns_pro_init();
            }
        }
    }

    /**
     * Constructor - Initialize plugin
     */
    private function __construct() {
        $this->load_textdomain();
        $this->load_dependencies();
    }

    /**
     * Load plugin text domain for translations
     */
    private function load_textdomain() {
        load_plugin_textdomain('lean-cart-share-and-save', false, LEAN_CSNS_PL_DIR . '/languages/');
    }

    /**
     * Load all required files
     */
    private function load_dependencies() {
        // Load classes
        require_once LEAN_CSNS_PL_DIR . '/includes/class-database.php';
        require_once LEAN_CSNS_PL_DIR . '/includes/class-wc-integration.php';
        require_once LEAN_CSNS_PL_DIR . '/includes/class-cart-handler.php';
        require_once LEAN_CSNS_PL_DIR . '/includes/class-url-handler.php';
        require_once LEAN_CSNS_PL_DIR . '/includes/class-block-cart-integration.php';
        
        require_once LEAN_CSNS_PL_DIR . '/includes/class-ajax-actions.php';
        require_once LEAN_CSNS_PL_DIR . '/includes/class-assets-manager.php';

        // Load admin files
        if (is_admin()) {
            require_once LEAN_CSNS_PL_DIR . '/includes/class-menu.php';
        }
    }
}