<?php
/**
 * ==========================
 * 🔐 IGNY8 FILE RULE HEADER
 * ==========================
 * @file         : module-manager-class.php
 * @location     : /core/admin/module-manager-class.php
 * @type         : Function Library
 * @scope        : Global
 * @allowed      : Module management, class definitions, core functionality
 * @reusability  : Globally Reusable
 * @notes        : Module manager class for core functionality
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Igny8 Module Manager - Controls which modules are active
 */
class Igny8_Module_Manager {
    
    private static $instance = null;
    private $modules = [];
    
    public static function get_instance() {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    private function __construct() {
        $this->init_modules();
        add_action('admin_init', [$this, 'register_module_settings']);
    }
    
    /**
     * Initialize module definitions - main modules only
     */
    private function init_modules() {
        $this->modules = [
            'planner' => [
                'name' => 'Planner',
                'description' => 'Keyword research and content planning with clusters, ideas, and mapping tools.',
                'default' => true,
                'icon' => 'dashicons-search',
                'category' => 'main',
                'cron_jobs' => [
                    'igny8_auto_cluster_cron',
                    'igny8_auto_generate_ideas_cron',
                    'igny8_auto_queue_cron'
                ]
            ],
            'writer' => [
                'name' => 'Writer',
                'description' => 'AI-powered content generation with drafts and templates management.',
                'default' => false,
                'icon' => 'dashicons-edit',
                'category' => 'main',
                'cron_jobs' => [
                    'igny8_auto_generate_content_cron',
                    'igny8_auto_generate_images_cron',
                    'igny8_auto_publish_drafts_cron'
                ]
            ],
            'analytics' => [
                'name' => 'Analytics',
                'description' => 'Performance tracking and data analysis with comprehensive reporting.',
                'default' => false,
                'icon' => 'dashicons-chart-bar',
                'category' => 'admin',
                'cron_jobs' => [
                    'igny8_process_ai_queue_cron',
                    'igny8_auto_recalc_cron',
                    'igny8_health_check_cron'
                ]
            ],
            'schedules' => [
                'name' => 'Schedules',
                'description' => 'Content scheduling and automation with calendar management.',
                'default' => false,
                'icon' => 'dashicons-calendar-alt',
                'category' => 'admin'
            ],
            'thinker' => [
                'name' => 'AI Thinker',
                'description' => 'AI-powered content strategy, prompts, and intelligent content planning tools.',
                'default' => true,
                'icon' => 'dashicons-lightbulb',
                'category' => 'admin'
            ]
        ];
    }
    
    /**
     * Check if a module is enabled
     */
    public function is_module_enabled($module) {
        $settings = get_option('igny8_module_settings', []);
        return isset($settings[$module]) ? (bool) $settings[$module] : (isset($this->modules[$module]) ? $this->modules[$module]['default'] : false);
    }
    
    /**
     * Get all enabled modules
     */
    public function get_enabled_modules() {
        $enabled = [];
        foreach ($this->modules as $key => $module) {
            if ($this->is_module_enabled($key)) {
                $enabled[$key] = $module;
            }
        }
        return $enabled;
    }
    
    /**
     * Get all modules
     */
    public function get_modules() {
        return $this->modules;
    }
    
    /**
     * Register module settings
     */
    public function register_module_settings() {
        register_setting('igny8_module_settings', 'igny8_module_settings');
    }
    
    /**
     * Save module settings
     */
    public function save_module_settings() {
        if (!isset($_POST['igny8_module_nonce']) || !wp_verify_nonce($_POST['igny8_module_nonce'], 'igny8_module_settings')) {
            wp_die('Security check failed');
        }
        
        $settings = $_POST['igny8_module_settings'] ?? [];
        
        // Initialize all modules as disabled first
        $all_modules = $this->get_modules();
        $final_settings = [];
        foreach ($all_modules as $module_key => $module) {
            $final_settings[$module_key] = false; // Default to disabled
        }
        
        // Set enabled modules to true
        foreach ($settings as $key => $value) {
            if (isset($final_settings[$key])) {
                $final_settings[$key] = (bool) $value;
            }
        }
        
        update_option('igny8_module_settings', $final_settings);
        
        // Force page reload using JavaScript
        echo '<script>window.location.reload();</script>';
        exit;
    }
}

// Initialize the module manager
function igny8_module_manager() {
    return Igny8_Module_Manager::get_instance();
}

// Helper functions for easy access
function igny8_is_module_enabled($module) {
    return igny8_module_manager()->is_module_enabled($module);
}

function igny8_get_enabled_modules() {
    return igny8_module_manager()->get_enabled_modules();
}

function igny8_get_modules() {
    return igny8_module_manager()->get_modules();
}
