<?php
/**
 * ==========================
 * 🔐 IGNY8 FILE RULE HEADER
 * ==========================
 * @file         : menu.php
 * @location     : /core/admin/menu.php
 * @type         : Admin Menu Handler
 * @scope        : Global
 * @allowed      : WordPress admin menu registration, navigation helpers, layout functions
 * @reusability  : Globally Reusable
 * @notes        : Registers admin menus and provides breadcrumb/submenu rendering functions
 */

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

/**
 * Render breadcrumb navigation
 */
function igny8_render_breadcrumb() {
    $current_page = $_GET['page'] ?? '';
    $sm = $_GET['sm'] ?? '';
    $breadcrumb = '<nav class="igny8-breadcrumb-nav">';
    $breadcrumb .= '<span class="igny8-breadcrumb-item"><a href="' . admin_url('admin.php?page=igny8-home') . '">Igny8 Home</a></span>';
    
    if ($current_page === 'igny8-planner') {
        $breadcrumb .= '<span class="igny8-breadcrumb-separator">›</span>';
        $breadcrumb .= '<span class="igny8-breadcrumb-item"><a href="' . admin_url('admin.php?page=igny8-planner') . '">Planner</a></span>';
        
        if ($sm === 'keywords') {
            $breadcrumb .= '<span class="igny8-breadcrumb-separator">›</span>';
            $breadcrumb .= '<span class="igny8-breadcrumb-item active">Keywords</span>';
        } elseif ($sm === 'clusters') {
            $breadcrumb .= '<span class="igny8-breadcrumb-separator">›</span>';
            $breadcrumb .= '<span class="igny8-breadcrumb-item active">Clusters</span>';
        } elseif ($sm === 'ideas') {
            $breadcrumb .= '<span class="igny8-breadcrumb-separator">›</span>';
            $breadcrumb .= '<span class="igny8-breadcrumb-item active">Ideas</span>';
        } elseif ($sm === 'mapping') {
            $breadcrumb .= '<span class="igny8-breadcrumb-separator">›</span>';
            $breadcrumb .= '<span class="igny8-breadcrumb-item active">Mapping</span>';
        }
    } elseif ($current_page === 'igny8-writer') {
        $breadcrumb .= '<span class="igny8-breadcrumb-separator">›</span>';
        $breadcrumb .= '<span class="igny8-breadcrumb-item"><a href="' . admin_url('admin.php?page=igny8-writer') . '">Writer</a></span>';
        
        if ($sm === 'drafts') {
            $breadcrumb .= '<span class="igny8-breadcrumb-separator">›</span>';
            $breadcrumb .= '<span class="igny8-breadcrumb-item active">Drafts</span>';
        } elseif ($sm === 'templates') {
            $breadcrumb .= '<span class="igny8-breadcrumb-separator">›</span>';
            $breadcrumb .= '<span class="igny8-breadcrumb-item active">Templates</span>';
        }
    } elseif ($current_page === 'igny8-optimizer') {
        $breadcrumb .= '<span class="igny8-breadcrumb-separator">›</span>';
        $breadcrumb .= '<span class="igny8-breadcrumb-item"><a href="' . admin_url('admin.php?page=igny8-optimizer') . '">Optimizer</a></span>';
        
        if ($sm === 'audits') {
            $breadcrumb .= '<span class="igny8-breadcrumb-separator">›</span>';
            $breadcrumb .= '<span class="igny8-breadcrumb-item active">Audits</span>';
        } elseif ($sm === 'suggestions') {
            $breadcrumb .= '<span class="igny8-breadcrumb-separator">›</span>';
            $breadcrumb .= '<span class="igny8-breadcrumb-item active">Suggestions</span>';
        }
    } elseif ($current_page === 'igny8-linker') {
        $breadcrumb .= '<span class="igny8-breadcrumb-separator">›</span>';
        $breadcrumb .= '<span class="igny8-breadcrumb-item"><a href="' . admin_url('admin.php?page=igny8-linker') . '">Linker</a></span>';
        
        if ($sm === 'backlinks') {
            $breadcrumb .= '<span class="igny8-breadcrumb-separator">›</span>';
            $breadcrumb .= '<span class="igny8-breadcrumb-item active">Backlinks</span>';
        } elseif ($sm === 'campaigns') {
            $breadcrumb .= '<span class="igny8-breadcrumb-separator">›</span>';
            $breadcrumb .= '<span class="igny8-breadcrumb-item active">Campaigns</span>';
        }
    } elseif ($current_page === 'igny8-personalize') {
        $breadcrumb .= '<span class="igny8-breadcrumb-separator">›</span>';
        $breadcrumb .= '<span class="igny8-breadcrumb-item"><a href="' . admin_url('admin.php?page=igny8-personalize') . '">Personalize</a></span>';
        
        if ($sm === 'settings') {
            $breadcrumb .= '<span class="igny8-breadcrumb-separator">›</span>';
            $breadcrumb .= '<span class="igny8-breadcrumb-item active">Settings</span>';
        } elseif ($sm === 'content-generation') {
            $breadcrumb .= '<span class="igny8-breadcrumb-separator">›</span>';
            $breadcrumb .= '<span class="igny8-breadcrumb-item active">Content Generation</span>';
        } elseif ($sm === 'rewrites') {
            $breadcrumb .= '<span class="igny8-breadcrumb-separator">›</span>';
            $breadcrumb .= '<span class="igny8-breadcrumb-item active">Rewrites</span>';
        } elseif ($sm === 'front-end') {
            $breadcrumb .= '<span class="igny8-breadcrumb-separator">›</span>';
            $breadcrumb .= '<span class="igny8-breadcrumb-item active">Front-end</span>';
        }
    } elseif (strpos($current_page, 'igny8-analytics') !== false) {
        $breadcrumb .= '<span class="igny8-breadcrumb-separator">›</span>';
        $breadcrumb .= '<span class="igny8-breadcrumb-item active">Analytics</span>';
    } elseif (strpos($current_page, 'igny8-schedules') !== false) {
        $breadcrumb .= '<span class="igny8-breadcrumb-separator">›</span>';
        $breadcrumb .= '<span class="igny8-breadcrumb-item active">Schedules</span>';
    } elseif (strpos($current_page, 'igny8-settings') !== false) {
        $breadcrumb .= '<span class="igny8-breadcrumb-separator">›</span>';
        $breadcrumb .= '<span class="igny8-breadcrumb-item active">Settings</span>';
    } elseif (strpos($current_page, 'igny8-help') !== false) {
        $breadcrumb .= '<span class="igny8-breadcrumb-separator">›</span>';
        $breadcrumb .= '<span class="igny8-breadcrumb-item active">Help</span>';
    }
    
    $breadcrumb .= '</nav>';
    return $breadcrumb;
}

/**
 * Render submenu navigation
 */
function igny8_render_submenu() {
    $current_page = $_GET['page'] ?? '';
    $sm = $_GET['sm'] ?? '';
    $submenu = '';
    
    if ($current_page === 'igny8-planner') {
        $submenu .= '<a href="' . admin_url('admin.php?page=igny8-planner&sm=home') . '" class="igny8-btn igny8-btn-sm igny8-btn-success igny8-btn-submenu' . ($sm === 'home' || $sm === '' ? ' active' : '') . '">Dashboard</a>';
        $submenu .= '<a href="' . admin_url('admin.php?page=igny8-planner&sm=keywords') . '" class="igny8-btn igny8-btn-sm igny8-btn-success igny8-btn-submenu' . ($sm === 'keywords' ? ' active' : '') . '">Keywords</a>';
        $submenu .= '<a href="' . admin_url('admin.php?page=igny8-planner&sm=clusters') . '" class="igny8-btn igny8-btn-sm igny8-btn-success igny8-btn-submenu' . ($sm === 'clusters' ? ' active' : '') . '">Clusters</a>';
        $submenu .= '<a href="' . admin_url('admin.php?page=igny8-planner&sm=ideas') . '" class="igny8-btn igny8-btn-sm igny8-btn-success igny8-btn-submenu' . ($sm === 'ideas' ? ' active' : '') . '">Ideas</a>';
    } elseif ($current_page === 'igny8-writer') {
        $submenu .= '<a href="' . admin_url('admin.php?page=igny8-writer&sm=home') . '" class="igny8-btn igny8-btn-sm igny8-btn-primary igny8-btn-submenu' . ($sm === 'home' || $sm === '' ? ' active' : '') . '">Dashboard</a>';
        $submenu .= '<a href="' . admin_url('admin.php?page=igny8-writer&sm=tasks') . '" class="igny8-btn igny8-btn-sm igny8-btn-primary igny8-btn-submenu' . ($sm === 'tasks' ? ' active' : '') . '">Tasks</a>';
        $submenu .= '<a href="' . admin_url('admin.php?page=igny8-writer&sm=drafts') . '" class="igny8-btn igny8-btn-sm igny8-btn-primary igny8-btn-submenu' . ($sm === 'drafts' ? ' active' : '') . '">Drafts</a>';
        $submenu .= '<a href="' . admin_url('admin.php?page=igny8-writer&sm=published') . '" class="igny8-btn igny8-btn-sm igny8-btn-primary igny8-btn-submenu' . ($sm === 'published' ? ' active' : '') . '">Published</a>';
    } elseif ($current_page === 'igny8-thinker') {
        $sp = $_GET['sp'] ?? 'main';
        $submenu .= '<a href="' . admin_url('admin.php?page=igny8-thinker&sp=main') . '" class="igny8-btn igny8-btn-sm igny8-btn-outline igny8-btn-submenu' . ($sp === 'main' ? ' active' : '') . '">Dashboard</a>';
        $submenu .= '<a href="' . admin_url('admin.php?page=igny8-thinker&sp=prompts') . '" class="igny8-btn igny8-btn-sm igny8-btn-outline igny8-btn-submenu' . ($sp === 'prompts' ? ' active' : '') . '">Prompts</a>';
        $submenu .= '<a href="' . admin_url('admin.php?page=igny8-thinker&sp=profile') . '" class="igny8-btn igny8-btn-sm igny8-btn-outline igny8-btn-submenu' . ($sp === 'profile' ? ' active' : '') . '">Profile</a>';
        $submenu .= '<a href="' . admin_url('admin.php?page=igny8-thinker&sp=strategies') . '" class="igny8-btn igny8-btn-sm igny8-btn-outline igny8-btn-submenu' . ($sp === 'strategies' ? ' active' : '') . '">Strategies</a>';
        $submenu .= '<a href="' . admin_url('admin.php?page=igny8-thinker&sp=image-testing') . '" class="igny8-btn igny8-btn-sm igny8-btn-outline igny8-btn-submenu' . ($sp === 'image-testing' ? ' active' : '') . '">Image Testing</a>';
    } elseif ($current_page === 'igny8-optimizer') {
        $submenu .= '<a href="' . admin_url('admin.php?page=igny8-optimizer') . '" class="igny8-btn igny8-btn-sm igny8-btn-warning igny8-btn-submenu' . ($sm === '' ? ' active' : '') . '">Dashboard</a>';
        $submenu .= '<a href="' . admin_url('admin.php?page=igny8-optimizer&sm=audits') . '" class="igny8-btn igny8-btn-sm igny8-btn-warning igny8-btn-submenu' . ($sm === 'audits' ? ' active' : '') . '">Audits</a>';
        $submenu .= '<a href="' . admin_url('admin.php?page=igny8-optimizer&sm=suggestions') . '" class="igny8-btn igny8-btn-sm igny8-btn-warning igny8-btn-submenu' . ($sm === 'suggestions' ? ' active' : '') . '">Suggestions</a>';
    } elseif ($current_page === 'igny8-linker') {
        $submenu .= '<a href="' . admin_url('admin.php?page=igny8-linker') . '" class="igny8-btn igny8-btn-sm igny8-btn-info igny8-btn-submenu' . ($sm === '' ? ' active' : '') . '">Dashboard</a>';
        $submenu .= '<a href="' . admin_url('admin.php?page=igny8-linker&sm=backlinks') . '" class="igny8-btn igny8-btn-sm igny8-btn-info igny8-btn-submenu' . ($sm === 'backlinks' ? ' active' : '') . '">Backlinks</a>';
        $submenu .= '<a href="' . admin_url('admin.php?page=igny8-linker&sm=campaigns') . '" class="igny8-btn igny8-btn-sm igny8-btn-info igny8-btn-submenu' . ($sm === 'campaigns' ? ' active' : '') . '">Campaigns</a>';
    } elseif ($current_page === 'igny8-personalize') {
        $submenu .= '<a href="' . admin_url('admin.php?page=igny8-personalize') . '" class="igny8-btn igny8-btn-sm igny8-btn-secondary igny8-btn-submenu' . ($sm === '' ? ' active' : '') . '">Dashboard</a>';
        $submenu .= '<a href="' . admin_url('admin.php?page=igny8-personalize&sm=settings') . '" class="igny8-btn igny8-btn-sm igny8-btn-secondary igny8-btn-submenu' . ($sm === 'settings' ? ' active' : '') . '">Settings</a>';
        $submenu .= '<a href="' . admin_url('admin.php?page=igny8-personalize&sm=content-generation') . '" class="igny8-btn igny8-btn-sm igny8-btn-secondary igny8-btn-submenu' . ($sm === 'content-generation' ? ' active' : '') . '">Content Generation</a>';
        $submenu .= '<a href="' . admin_url('admin.php?page=igny8-personalize&sm=rewrites') . '" class="igny8-btn igny8-btn-sm igny8-btn-secondary igny8-btn-submenu' . ($sm === 'rewrites' ? ' active' : '') . '">Rewrites</a>';
        $submenu .= '<a href="' . admin_url('admin.php?page=igny8-personalize&sm=front-end') . '" class="igny8-btn igny8-btn-sm igny8-btn-secondary igny8-btn-submenu' . ($sm === 'front-end' ? ' active' : '') . '">Front-end</a>';
    } elseif ($current_page === 'igny8-settings') {
        $sp = $_GET['sp'] ?? 'general';
        $submenu .= '<a href="' . admin_url('admin.php?page=igny8-settings&sp=general') . '" class="igny8-btn igny8-btn-sm igny8-btn-outline igny8-btn-submenu' . ($sp === 'general' ? ' active' : '') . '">Settings</a>';
        $submenu .= '<a href="' . admin_url('admin.php?page=igny8-settings&sp=status') . '" class="igny8-btn igny8-btn-sm igny8-btn-outline igny8-btn-submenu' . ($sp === 'status' ? ' active' : '') . '">Status</a>';
        $submenu .= '<a href="' . admin_url('admin.php?page=igny8-settings&sp=integration') . '" class="igny8-btn igny8-btn-sm igny8-btn-outline igny8-btn-submenu' . ($sp === 'integration' ? ' active' : '') . '">Integration</a>';
        $submenu .= '<a href="' . admin_url('admin.php?page=igny8-settings&sp=import-export') . '" class="igny8-btn igny8-btn-sm igny8-btn-outline igny8-btn-submenu' . ($sp === 'import-export' ? ' active' : '') . '">Import/Export</a>';
    } elseif ($current_page === 'igny8-help') {
        $sp = $_GET['sp'] ?? 'help';
        $submenu .= '<a href="' . admin_url('admin.php?page=igny8-help&sp=help') . '" class="igny8-btn igny8-btn-sm igny8-btn-outline igny8-btn-submenu' . ($sp === 'help' ? ' active' : '') . '">Help & Support</a>';
        $submenu .= '<a href="' . admin_url('admin.php?page=igny8-help&sp=docs') . '" class="igny8-btn igny8-btn-sm igny8-btn-outline igny8-btn-submenu' . ($sp === 'docs' ? ' active' : '') . '">Documentation</a>';
        $submenu .= '<a href="' . admin_url('admin.php?page=igny8-help&sp=system-testing') . '" class="igny8-btn igny8-btn-sm igny8-btn-outline igny8-btn-submenu' . ($sp === 'system-testing' ? ' active' : '') . '">System Testing</a>';
        $submenu .= '<a href="' . admin_url('admin.php?page=igny8-help&sp=function-testing') . '" class="igny8-btn igny8-btn-sm igny8-btn-outline igny8-btn-submenu' . ($sp === 'function-testing' ? ' active' : '') . '">Function Testing</a>';
    }
    
    return $submenu;
}

/**
 * Register admin menu pages
 */
function igny8_register_admin_menu() {
    // Ensure module manager is available
    if (!function_exists('igny8_is_module_enabled')) {
        return;
    }
    // Main menu page
    add_menu_page(
        'Igny8 AI SEO',           // Page title
        'Igny8 AI SEO',           // Menu title
        'manage_options',         // Capability
        'igny8-home',            // Menu slug
        'igny8_home_page',       // Callback function
        'dashicons-chart-line',  // Icon
        30                       // Position
    );

    // Home page
    add_submenu_page(
        'igny8-home',            // Parent slug
        'Dashboard',             // Page title
        'Dashboard',             // Menu title
        'manage_options',        // Capability
        'igny8-home',           // Menu slug
        'igny8_home_page'       // Callback function
    );

    // Module submenus (only if enabled)
    if (igny8_is_module_enabled('planner')) {
        add_submenu_page(
            'igny8-home',
            'Content Planner',
            'Planner',
            'manage_options',
            'igny8-planner',
            'igny8_planner_page'
        );
    }

    if (igny8_is_module_enabled('writer')) {
        add_submenu_page(
            'igny8-home',
            'Content Writer',
            'Writer',
            'manage_options',
            'igny8-writer',
            'igny8_writer_page'
        );
    }

    if (igny8_is_module_enabled('thinker')) {
        add_submenu_page(
            'igny8-home',
            'AI Thinker',
            'Thinker',
            'manage_options',
            'igny8-thinker',
            'igny8_thinker_page'
        );
        
        // Prompts subpage under Thinker
        add_submenu_page(
            'igny8-thinker',
            'AI Prompts',
            'Prompts',
            'manage_options',
            'igny8-thinker&sp=prompts',
            'igny8_thinker_page'
        );
    }

    if (igny8_is_module_enabled('schedules')) {
        add_submenu_page(
            'igny8-home',
            'Smart Automation Schedules',
            'Schedules',
            'manage_options',
            'igny8-schedules',
            'igny8_schedules_page'
        );
    }


    // Analytics before Settings (only if enabled)
    if (igny8_is_module_enabled('analytics')) {
        add_submenu_page(
            'igny8-home',
            'Analytics',
            'Analytics',
            'manage_options',
            'igny8-analytics',
            'igny8_analytics_page'
        );
    }
    
    // Cron Health page

    // Settings page
    add_submenu_page(
        'igny8-home',
        'Settings',
        'Settings',
        'manage_options',
        'igny8-settings',
        'igny8_settings_page'
    );


    // Help page
    add_submenu_page(
        'igny8-home',
        'Help',
        'Help',
        'manage_options',
        'igny8-help',
        'igny8_help_page'
    );
    
    // Documentation subpage under Help
    add_submenu_page(
        'igny8-help',
        'Documentation',
        'Documentation',
        'manage_options',
        'igny8-help&sp=docs',
        'igny8_help_page'
    );
    
    // System Testing subpage under Help
    add_submenu_page(
        'igny8-help',
        'System Testing',
        'System Testing',
        'manage_options',
        'igny8-help&sp=system-testing',
        'igny8_help_page'
    );
    
    // Function Testing subpage under Help
    add_submenu_page(
        'igny8-help',
        'Function Testing',
        'Function Testing',
        'manage_options',
        'igny8-help&sp=function-testing',
        'igny8_help_page'
    );

}

// Static page wrapper functions - each page handles its own layout
function igny8_home_page() {
    include_once plugin_dir_path(__FILE__) . '../../modules/home.php';
}

function igny8_planner_page() {
    include_once plugin_dir_path(__FILE__) . '../../modules/planner/planner.php';
}

function igny8_writer_page() {
    include_once plugin_dir_path(__FILE__) . '../../modules/writer/writer.php';
}

function igny8_thinker_page() {
    include_once plugin_dir_path(__FILE__) . '../../modules/thinker/thinker.php';
}

function igny8_settings_page() {
    include_once plugin_dir_path(__FILE__) . '../../modules/settings/general-settings.php';
}

function igny8_analytics_page() {
    include_once plugin_dir_path(__FILE__) . '../../modules/analytics/analytics.php';
}

function igny8_schedules_page() {
    include_once plugin_dir_path(__FILE__) . '../../modules/settings/schedules.php';
}

function igny8_help_page() {
    include_once plugin_dir_path(__FILE__) . '../../modules/help/help.php';
}

// Hook into admin_menu
add_action('admin_menu', 'igny8_register_admin_menu');
