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

class VS_Admin_Pages {

    public static function init() {
        add_action('admin_menu', [__CLASS__, 'register_pages']);
        add_action('admin_enqueue_scripts', [__CLASS__, 'enqueue_assets']);
    }

    public static function register_pages() {
        add_menu_page(
            __('Vendora', 'vendor-space'),
            __('Vendora', 'vendor-space'),
            'manage_options',
            'vs-admin-dashboard',
            [__CLASS__, 'render_dashboard_page'],
            'dashicons-store',
            26
        );
        add_submenu_page(
            'vs-admin-dashboard',
            __('Request Account', 'vendor-space'),
            __('Request Account', 'vendor-space'),
            'manage_options',
            'vs-admin-request-account',
            [__CLASS__, 'render_request_account_page']
        );

        add_submenu_page(
            'vs-admin-dashboard',
            __('Request Products', 'vendor-space'),
            __('Request Products', 'vendor-space'),
            'manage_options',
            'vs-admin-request-products',
            [__CLASS__, 'render_request_products_page']
        );

        add_submenu_page(
            'vs-admin-dashboard',
            __('Products', 'vendor-space'),
            __('Products', 'vendor-space'),
            'manage_options',
            'vs-admin-products',
            [__CLASS__, 'render_products_page']
        );

        add_submenu_page(
            'vs-admin-dashboard',
            __('Reports', 'vendor-space'),
            __('Reports', 'vendor-space'),
            'manage_options',
            'vs-admin-reports',
            [__CLASS__, 'render_reports_page']
        );
    }

    public static function enqueue_assets($hook_suffix) {
        if (strpos($hook_suffix, 'vs-admin') === false) {
            return;
        }

        $css_path = VS_PATH . 'assets/admin.css';
        $js_path  = VS_PATH . 'assets/admin.js';

        $css_ver = file_exists($css_path) ? (string) filemtime($css_path) : '1.0';
        $js_ver  = file_exists($js_path) ? (string) filemtime($js_path) : '1.0';

        wp_enqueue_style('vs-admin-css', VS_URL . 'assets/admin.css', [], $css_ver);
        wp_enqueue_script('vs-admin-js', VS_URL . 'assets/admin.js', ['jquery'], $js_ver, true);

        wp_localize_script('vs-admin-js', 'vs_admin_vars', [
            'ajaxurl' => admin_url('admin-ajax.php'),
            'nonce'   => wp_create_nonce('vs_admin_nonce'),
        ]);
    }

    public static function render_dashboard_page() {
        if (!current_user_can('manage_options')) {
            wp_die(__('You are not allowed to view this page.', 'vendor-space'));
        }

        echo '<div class="wrap">';
        echo '<h1>' . esc_html__('Vendora', 'vendor-space') . '</h1>';
        echo '<p>' . esc_html__('Use the submenu to access Vendor tools.', 'vendor-space') . '</p>';
        echo '</div>';
    }

    public static function render_reports_page() {
        if (!current_user_can('manage_options')) {
            wp_die(__('You are not allowed to view this page.', 'vendor-space'));
        }

        echo '<div class="wrap vs-admin-reports-page" style="padding:0">';

        if (class_exists('VS_Reports') && method_exists('VS_Reports', 'render_front_tab')) {
            // Reuse the existing reports render (safe if present)
            VS_Reports::render_front_tab();
        } else {
            echo '<h2>' . esc_html__('Reports', 'vendor-space') . '</h2>';
            echo '<p>' . esc_html__('Reports module is not available.', 'vendor-space') . '</p>';
        }

        echo '</div>';
    }

    public static function render_request_account_page() {
        if (!current_user_can('manage_options')) {
            wp_die(__('You are not allowed to view this page.', 'vendor-space'));
        }

        echo '<div class="wrap vs-admin-request-account">';
        echo '<h1>' . esc_html__('Request Account', 'vendor-space') . '</h1>';
        echo '<p>' . esc_html__('Here you can view and manage account requests from vendors.', 'vendor-space') . '</p>';

        // Simple placeholder table
        echo '<table class="wp-list-table widefat fixed striped">';
        echo '<thead><tr><th>' . esc_html__('ID', 'vendor-space') . '</th><th>' . esc_html__('Name', 'vendor-space') . '</th><th>' . esc_html__('Email', 'vendor-space') . '</th><th>' . esc_html__('Status', 'vendor-space') . '</th></tr></thead>';
        echo '<tbody><tr><td colspan="4">' . esc_html__('No requests yet.', 'vendor-space') . '</td></tr></tbody>';
        echo '</table>';

        echo '</div>';
    }

    public static function render_request_products_page() {
        if (!current_user_can('manage_options')) {
            wp_die(__('You are not allowed to view this page.', 'vendor-space'));
        }

        echo '<div class="wrap vs-admin-request-products">';
        echo '<h1>' . esc_html__('Request Products', 'vendor-space') . '</h1>';
        echo '<p>' . esc_html__('Here you can view product requests submitted by vendors.', 'vendor-space') . '</p>';

        echo '<table class="wp-list-table widefat fixed striped">';
        echo '<thead><tr><th>' . esc_html__('ID', 'vendor-space') . '</th><th>' . esc_html__('Product', 'vendor-space') . '</th><th>' . esc_html__('Vendor', 'vendor-space') . '</th><th>' . esc_html__('Status', 'vendor-space') . '</th></tr></thead>';
        echo '<tbody><tr><td colspan="4">' . esc_html__('No product requests yet.', 'vendor-space') . '</td></tr></tbody>';
        echo '</table>';

        echo '</div>';
    }

    public static function render_products_page() {
        if (!current_user_can('manage_options')) {
            wp_die(__('You are not allowed to view this page.', 'vendor-space'));
        }

        echo '<div class="wrap vs-admin-products">';
        echo '<h1>' . esc_html__('Products', 'vendor-space') . '</h1>';
        echo '<p>' . esc_html__('Manage vendor products here.', 'vendor-space') . '</p>';

        echo '<table class="wp-list-table widefat fixed striped">';
        echo '<thead><tr><th>' . esc_html__('ID', 'vendor-space') . '</th><th>' . esc_html__('Product', 'vendor-space') . '</th><th>' . esc_html__('Vendor', 'vendor-space') . '</th><th>' . esc_html__('Stock', 'vendor-space') . '</th></tr></thead>';
        echo '<tbody><tr><td colspan="4">' . esc_html__('No products to display.', 'vendor-space') . '</td></tr></tbody>';
        echo '</table>';

        echo '</div>';
    }
}

VS_Admin_Pages::init();
