<?php
if (!defined('ABSPATH')) exit;

class StakMV_Admin_Menu {
    /**
     * Admin page hook suffix returned by add_submenu_page.
     *
     * @var string
     */
    private $page_hook = '';

    public function __construct() {
        add_action('admin_menu', [$this, 'add_menu']);
        add_action('admin_enqueue_scripts', [$this, 'enqueue_assets']);
    }

    public function add_menu() {
        $this->page_hook = add_menu_page(
            'Multivendor',
            'Multivendor',
            'manage_woocommerce',
            'stakmv-auctions',
            [$this, 'render_page'],
            'dashicons-store',
            56
        );
    }

    public function enqueue_assets($hook_suffix) {
        if ($hook_suffix !== $this->page_hook) {
            return;
        }

        wp_enqueue_style(
            'stakmv-admin-settings',
            STAKMV_PLUGIN_URL . 'assets/css/admin-settings.css',
            [],
            file_exists(STAKMV_PLUGIN_PATH . 'assets/css/admin-settings.css')
                ? (string) filemtime(STAKMV_PLUGIN_PATH . 'assets/css/admin-settings.css')
                : (defined('STAKMV_VERSION') ? STAKMV_VERSION : '1.0.0')
        );

        wp_enqueue_script(
            'stakmv-admin-settings',
            STAKMV_PLUGIN_URL . 'assets/js/admin-settings.js',
            array(),
            file_exists(STAKMV_PLUGIN_PATH . 'assets/js/admin-settings.js')
                ? (string) filemtime(STAKMV_PLUGIN_PATH . 'assets/js/admin-settings.js')
                : (defined('STAKMV_VERSION') ? STAKMV_VERSION : '1.0.0'),
            true
        );
    }

    public function render_page() {

        // ✅ ONE variable name, used everywhere
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only tab selection.
        $current_tab = isset($_GET['tab']) ? sanitize_key(wp_unslash($_GET['tab'])) : 'dashboard';
        ?>
        <div class="wrap wcmv-admin-wrap">
            <div class="wcmv-admin-shell">
                <aside class="wcmv-admin-sidebar">
                    <h1>
                        <img src="<?php echo esc_url(STAKMV_PLUGIN_URL . 'assets/images/icon.png'); ?>" alt="Stak Multivendor" class="wcmv-admin-logo">
                        <span>Stak Multivendor</span>
                    </h1>
                    <p class="wcmv-admin-sidebar-note">Marketplace settings and operations</p>

                    <h2 class="nav-tab-wrapper wcmv-admin-tabs">

                        <a href="?page=stakmv-auctions&tab=dashboard"
                           class="nav-tab <?php echo $current_tab === 'dashboard' ? 'nav-tab-active' : ''; ?>">
                            Dashboard
                        </a>

                        <a href="?page=stakmv-auctions&tab=general"
                           class="nav-tab <?php echo $current_tab === 'general' ? 'nav-tab-active' : ''; ?>">
                            General
                        </a>

                        <a href="?page=stakmv-auctions&tab=bidding"
                           class="nav-tab <?php echo $current_tab === 'bidding' ? 'nav-tab-active' : ''; ?>">
                            Bidding
                        </a>

                        <a href="?page=stakmv-auctions&tab=vendors"
                           class="nav-tab <?php echo $current_tab === 'vendors' ? 'nav-tab-active' : ''; ?>">
                            Vendors
                        </a>

                        <a href="?page=stakmv-auctions&tab=management"
                           class="nav-tab <?php echo $current_tab === 'management' ? 'nav-tab-active' : ''; ?>">
                            Management
                        </a>

                        <?php
                        // Add-on tabs hook.
                        do_action('stakmv_auctions_settings_tabs', $current_tab);
                        ?>
                    </h2>
                </aside>

                <main class="wcmv-admin-main">
                    <div class="wcmv-tab-content wcmv-settings-content">
                        <?php
                        // 🔌 Render tab content
                        do_action("stakmv_auctions_render_tab_{$current_tab}");
                        ?>
                    </div>
                </main>
            </div>
        </div>
        <?php
    }
}
