<?php

namespace WPUltimateSecurity\UltimateSecurity\Presentation\Admin;

use WPUltimateSecurity\UltimateSecurity\Support\Validation\ProValidation;

class AdminMenu
{
    public static function register()
    {
        add_action('admin_menu', [self::class, 'ultimate_security_admin_menu']);
    }

    public static function ultimate_security_admin_menu()
    {
        $pageTitle = __('Ultimate Security', 'ultimate-security');
        if (ProValidation::ultimate_security_is_pro()) {
            $pageTitle .= ' ' . __('Pro', 'ultimate-security');
        }
        // var_dump(ULTIMATE_SECURITY_PRO);
        add_menu_page(
            esc_html($pageTitle),
            esc_html($pageTitle),
            'manage_options',
            'ultimate-security',
            [self::class, 'ultimate_security_dashboard_page_html'],
            ULTIMATE_SECURITY_URL . 'assets/admin/images/menu-icon.svg',
            57
        );

        // Overwrite the auto-added submenu by adding one with an empty title
        add_submenu_page(
            'ultimate-security',
            '', // ← empty title hides it
            '', // ← hide from display
            'manage_options',
            'ultimate-security',
            '__return_null' // dummy callback
        );


        // ✅ Always remove the auto-added duplicate submenu
        remove_submenu_page('ultimate-security', 'ultimate-security');

        add_submenu_page(
            'ultimate-security',
            esc_html__('Dashboard', 'ultimate-security'),
            esc_html__('Dashboard', 'ultimate-security'),
            'manage_options',
            'ultimate-security' . '#/',
            [self::class, 'ultimate_security_dashboard_page_html']
        );

        $tabs = self::ultimate_security_get_tabs();

        if (!empty($tabs)) {
            foreach ($tabs as $key => $tab) {
                $slug = ltrim($tab['slug'], '/');
                add_submenu_page(
                    'ultimate-security',
                    esc_html($tab['name']),
                    esc_html($tab['name']),
                    'manage_options',
                    'ultimate-security' . '#/' . $slug,
                    [self::class, 'ultimate_security_dashboard_page_html']
                );
                self::ultimate_security_generate_submenu($tab);
            }
        }
        add_submenu_page(
            'ultimate-security',          // Parent menu slug (e.g., Settings menu)
            __('Contact', 'ultimate-security'),              // Page title
            __('Contact', 'ultimate-security'),              // Menu title
            'manage_options',               // Capability
            'ultimate-security-contact',    // Menu slug (just a placeholder)
            '__return_null'                 // Callback (not used, since it's external)
        );

    }

    public static function ultimate_security_dashboard_page_html()
    {
        if (!current_user_can('manage_options')) {
            return;
        } ?>
        <div class="ultimate-security-settings-wrapper">
            <div id="ultimate-security-settings"></div>
        </div>

        <?php
    }

    public static function ultimate_security_get_tabs()
    {
        $ultimate_security_tabs = [
            // 1. Login & Authentication - Most familiar to users
            'login' => [
                'slug' => 'settings/login/2fa/overview',
                'name' => __('Login & Authentication', 'ultimate-security'),
            ],

            // 2. Threat Protection - Active defense against attacks
            'threat' => [
                'slug' => 'settings/threat/anti-bot/re-captcha',
                'name' => __('Threat Protection', 'ultimate-security'),
            ],

            // 3. Site Hardening - Security configuration
            'hardening' => [
                'slug' => 'settings/hardening/data-security/overview',
                'name' => __('Site Hardening', 'ultimate-security'),
            ],

            // REMOVED: Access Restrictions - server-level features

            // 4. File Security Scanner - Detect threats
            'ai-malware-scanner' => [
                'slug' => '/ai-malware-scanner',
                'name' => __('File Security Scanner', 'ultimate-security'),
            ],

            // 5. Vulnerability Scanner - Check for known vulnerabilities
            'vulnerability-scanner' => [
                'slug' => '/vulnerability-scanner',
                'name' => __('Vulnerability Scanner', 'ultimate-security'),
            ],

            // 6. Activity Monitoring - Track everything
            'activity_logs' => [
                'slug' => '/activity-logs',
                'name' => __('Activity & Monitoring', 'ultimate-security'),
            ],

            // 7. Alerts & Notifications - Email alerts and webhook integrations
            'alert' => [
                'slug' => 'alert/alert-management',
                'name' => __('Alerts & Notifications', 'ultimate-security'),
            ],

            // 8. Monitoring & Diagnostics - Site health and testing
            'monitoring' => [
                'slug' => 'settings/monitoring/site-health-info',
                'name' => __('Monitoring & Diagnostics', 'ultimate-security'),
            ],

            // 9. Maintenance & Tools - Backups, cleanup, utilities
            'maintenance' => [
                'slug' => 'settings/maintenance/comments-cleaner',
                'name' => __('Maintenance & Tools', 'ultimate-security'),
            ],

            // 10. Help & Support - Get assistance
            'feedback' => [
                'slug' => '/contact-form',
                'name' => __('Help & Support', 'ultimate-security'),
            ],
        ];

        if (ProValidation::ultimate_security_is_pro()) {
            $tabs_with_pro_features = [];
            foreach ($ultimate_security_tabs as $key => $value) {
                $tabs_with_pro_features[$key] = $value;
            }
            $tabs_with_pro_features['license'] = [
                'slug' => '/license',
                'name' => __('License', 'ultimate-security'),
            ];
            $ultimate_security_tabs = $tabs_with_pro_features;
        }

        // Allow filter modifications
        $ultimate_security_tabs = apply_filters('ultimate_security_tabs_modify', $ultimate_security_tabs);

        return $ultimate_security_tabs;
    }

    public static function ultimate_security_generate_submenu($tab)
    {
        if (isset($tab['sub']) && $tab['sub']) {
            foreach ($tab['sub'] as $k => $subtab) {
                add_submenu_page(
                    'admin.php?page=' . $tab['url'],
                    esc_html($subtab['name']),
                    esc_html($subtab['name']),
                    'manage_options',
                    $subtab['url'],
                    array(self::class, 'ultimate_security_dashboard_page_html')
                );
                self::ultimate_security_generate_submenu($subtab);
            }
        }
    }


}
