<?php
if (!defined('ABSPATH')) die('Restricted Access');
if (!current_user_can('manage_options')) {
    wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'ai-flash-tune'));
}

$current_page = isset($_GET['page']) ? sanitize_text_field(wp_unslash($_GET['page'])) : '';
$ai_provider = get_option('aiflt_ai_provider', 'local');
$provider_labels = [
    'local'  => __('Local Mode', 'ai-flash-tune'),
    'openai' => __('OpenAI Connected', 'ai-flash-tune'),
    'zywrap' => __('Zywrap Connected', 'ai-flash-tune'),
];
$provider_label = $provider_labels[$ai_provider] ?? __('Local Mode', 'ai-flash-tune');

$growth_modules = [
    'aiftac' => class_exists('AIFTAC_Logic'),
    'aiftpo' => class_exists('AIFTPO_Logic'),
    'aiftas' => class_exists('AIFTAS_Logic'),
    'aiftal' => class_exists('AIFTAL_Logic'),
    'aiftrp' => class_exists('AIFTRP_Logic'),
    'aiftcr' => class_exists('AIFTCR_Logic'),
    'aiftco' => class_exists('AIFTCO_Logic'),
];
$active_growth_count = count(array_filter($growth_modules));
$total_growth_count = max(1, count($growth_modules));
$active_growth_percent = min(100, (int) round(($active_growth_count / $total_growth_count) * 100));
$reports_page = class_exists('AIFTRP_Logic') ? 'aiftrp-reports' : 'aiflt-ai-usage-reports';
$reports_pages = class_exists('AIFTRP_Logic') ? ['aiftrp-reports', 'aiflt-ai-usage-reports'] : ['aiflt-ai-usage-reports'];

$nav_sections = [
    [
        'label' => __('Overview', 'ai-flash-tune'),
        'items' => array_values(array_filter([
            ['title' => __('Dashboard', 'ai-flash-tune'), 'page' => 'ai_flash_tune', 'pages' => ['ai_flash_tune', ''], 'icon' => 'fas fa-chart-line'],
            ['title' => __('Funnel Analysis', 'ai-flash-tune'), 'page' => 'funnelanalysis', 'pages' => ['funnelanalysis'], 'icon' => 'fas fa-route'],
            ['title' => __('Products', 'ai-flash-tune'), 'page' => 'products', 'pages' => ['products', 'product-detail'], 'icon' => 'fas fa-box-open'],
            ['title' => __('Abandoned Carts', 'ai-flash-tune'), 'page' => 'abandonedcarts', 'pages' => ['abandonedcarts'], 'icon' => 'fas fa-shopping-cart'],
            class_exists('AIFTAC_Logic') ? ['title' => __('AI Action Center', 'ai-flash-tune'), 'page' => 'aiftac-action-center', 'pages' => ['aiftac-action-center'], 'icon' => 'fas fa-magic'] : null,
        ])),
    ],
    [
        'label' => __('Optimization', 'ai-flash-tune'),
        'items' => array_values(array_filter([
            class_exists('AIFTPO_Logic') ? ['title' => __('Product Optimizer', 'ai-flash-tune'), 'page' => 'aiftpo-product-optimizer', 'pages' => ['aiftpo-product-optimizer'], 'icon' => 'fas fa-pen-nib'] : null,
            class_exists('AIFTCR_Logic') ? ['title' => __('Cart Recovery', 'ai-flash-tune'), 'page' => 'aiftcr-recovery-center', 'pages' => ['aiftcr-recovery-center', 'aiftcr-email-log'], 'icon' => 'fas fa-envelope-open-text'] : null,
            class_exists('AIFTCO_Logic') ? ['title' => __('Checkout Optimizer', 'ai-flash-tune'), 'page' => 'aiftco-optimizer-dashboard', 'pages' => ['aiftco-optimizer-dashboard'], 'icon' => 'fas fa-cash-register'] : null,
        ])),
    ],
    [
        'label' => __('Intelligence', 'ai-flash-tune'),
        'items' => array_values(array_filter([
            ['title' => __('Reports', 'ai-flash-tune'), 'page' => $reports_page, 'pages' => $reports_pages, 'icon' => 'fas fa-file-alt'],
            ['title' => __('Automations', 'ai-flash-tune'), 'page' => 'aiflt-growth-tools', 'pages' => ['aiflt-growth-tools'], 'icon' => 'fas fa-magic'],
            class_exists('AIFTAS_Logic') ? ['title' => __('Segmentation', 'ai-flash-tune'), 'page' => 'aiftas-segmentation', 'pages' => ['aiftas-segmentation'], 'icon' => 'fas fa-layer-group'] : null,
            class_exists('AIFTAL_Logic') ? ['title' => __('Alert Center', 'ai-flash-tune'), 'page' => 'aiftal-alert-center', 'pages' => ['aiftal-alert-center'], 'icon' => 'fas fa-bell'] : null,
        ])),
    ],
    [
        'label' => __('Settings', 'ai-flash-tune'),
        'items' => [
            ['title' => __('Settings', 'ai-flash-tune'), 'page' => 'aiflt_settings', 'pages' => ['aiflt_settings'], 'icon' => 'fas fa-cog'],
            ['title' => __('Integrations', 'ai-flash-tune'), 'page' => 'addon-manager', 'pages' => ['addon-manager'], 'icon' => 'fas fa-puzzle-piece'],
            ['title' => __('Setup Wizard', 'ai-flash-tune'), 'page' => 'aiflt-setup-wizard', 'pages' => ['aiflt-setup-wizard'], 'icon' => 'fas fa-check-circle'],
            ['title' => __('Privacy Tools', 'ai-flash-tune'), 'page' => 'privacy-tools', 'pages' => ['privacy-tools'], 'icon' => 'fas fa-user-shield'],
        ],
    ],
    [
        'label' => __('Support', 'ai-flash-tune'),
        'items' => [
            ['title' => __('Help / Support', 'ai-flash-tune'), 'page' => 'aiflt-help', 'pages' => ['aiflt-help'], 'icon' => 'fas fa-life-ring'],
            ['title' => __('Upgrade to PRO', 'ai-flash-tune'), 'page' => 'upgrade-to-pro', 'pages' => ['upgrade-to-pro'], 'icon' => 'fas fa-rocket'],
        ],
    ],
];
?>
<button id="aift-sidebar-toggle" class="ai-flash-tune-sidebar-toggle" aria-label="<?php echo esc_attr__('Toggle sidebar', 'ai-flash-tune'); ?>">
    <span class="aiflt-sidebar-toggle-mark">☰</span>
</button>
<div class="ai-flash-tune-sidebar-overlay"></div>
<aside class="ai-flash-tune-sidebar" aria-label="<?php echo esc_attr__('AI Flash Tune navigation', 'ai-flash-tune'); ?>">
    <div class="ai-flash-tune-sidebar-header">
        <a title="<?php echo esc_attr__('AI Flash Tune Dashboard', 'ai-flash-tune'); ?>" href="<?php echo esc_url(admin_url('admin.php?page=ai_flash_tune')); ?>" class="ai-flash-tune-sidebar-brand">
            <span class="ai-flash-tune-sidebar-brand-logo ai-flash-tune-sidebar-brand-logo-wide">
                <img src="<?php echo esc_url(AIFLT_PLUGIN_URL . 'assets/images/site-menu-logo.png'); ?>" alt="<?php echo esc_attr__('AI Flash Tune', 'ai-flash-tune'); ?>" />
            </span>
        </a>
        <button type="button" id="aift-sidebar-collapse" class="ai-flash-tune-sidebar-desktop-toggle" aria-label="<?php echo esc_attr__('Collapse sidebar', 'ai-flash-tune'); ?>" aria-expanded="true">
            <i class="fas fa-angle-double-left" aria-hidden="true"></i>
        </button>
    </div>

    <div class="ai-flash-tune-sidebar-status">
        <span class="ai-flash-tune-sidebar-status-dot"></span>
        <span class="ai-flash-tune-sidebar-status-text"><?php echo esc_html($provider_label); ?></span>
    </div>

    <div class="ai-flash-tune-sidebar-scroll">
        <nav class="ai-flash-tune-sidebar-nav">
            <?php foreach ($nav_sections as $section) : ?>
                <?php if (empty($section['items'])) continue; ?>
                <div class="ai-flash-tune-nav-section">
                    <div class="ai-flash-tune-nav-section-label"><?php echo esc_html($section['label']); ?></div>
                    <?php foreach ($section['items'] as $item) :
                        $is_active = in_array($current_page, $item['pages'], true);
                        ?>
                        <a title="<?php echo esc_attr($item['title']); ?>" href="<?php echo esc_url(admin_url('admin.php?page=' . $item['page'])); ?>" class="ai-flash-tune-nav-link <?php echo $is_active ? esc_attr('active') : ''; ?>">
                            <span class="ai-flash-tune-nav-icon" aria-hidden="true"><i class="<?php echo esc_attr($item['icon']); ?>"></i></span>
                            <span class="ai-flash-tune-nav-text"><?php echo esc_html($item['title']); ?></span>
                        </a>
                    <?php endforeach; ?>
                </div>
            <?php endforeach; ?>
        </nav>
    </div>

    <div class="aiflt-sidebar-footer-compact" aria-label="<?php echo esc_attr__('AI Flash Tune module status', 'ai-flash-tune'); ?>">
        <div class="aiflt-sidebar-footer-top">
            <span><?php echo esc_html__('Modules', 'ai-flash-tune'); ?></span>
            <strong><?php echo esc_html((string) $active_growth_count); ?>/<?php echo esc_html((string) $total_growth_count); ?></strong>
        </div>
        <div class="aiflt-sidebar-footer-progress" aria-hidden="true">
            <span style="width: <?php echo esc_attr((string) $active_growth_percent); ?>%;"></span>
        </div>
        <a href="<?php echo esc_url(admin_url('admin.php?page=addon-manager')); ?>">
            <?php echo esc_html__('Manage modules', 'ai-flash-tune'); ?>
            <i class="fas fa-arrow-right" aria-hidden="true"></i>
        </a>
    </div>
</aside>
