<?php
/**
 * Image Boost — Admin Page Registration
 */

if (!defined('ABSPATH')) {
    exit;
}

class ImgBoost_Admin {

    /**
     * Initialize admin hooks.
     */
    public static function init() {
        add_action('admin_menu', [__CLASS__, 'register_pages']);
        add_action('admin_enqueue_scripts', [__CLASS__, 'enqueue_assets']);
    }

    /**
     * Register admin menu pages.
     */
    public static function register_pages() {
        add_menu_page(
            __('Image Boost', 'image-boost'),
            __('Image Boost', 'image-boost'),
            'manage_options',
            'image-boost',
            [__CLASS__, 'render_dashboard'],
            'dashicons-images-alt2',
            56
        );

        add_submenu_page(
            'image-boost',
            __('Dashboard', 'image-boost'),
            __('Dashboard', 'image-boost'),
            'manage_options',
            'image-boost',
            [__CLASS__, 'render_dashboard']
        );

        add_submenu_page(
            'image-boost',
            __('Bulk Optimize', 'image-boost'),
            __('Bulk Optimize', 'image-boost'),
            'manage_options',
            'image-boost-bulk',
            [__CLASS__, 'render_bulk']
        );

        add_submenu_page(
            'image-boost',
            __('Settings', 'image-boost'),
            __('Settings', 'image-boost'),
            'manage_options',
            'image-boost-settings',
            [__CLASS__, 'render_settings']
        );

        add_submenu_page(
            'image-boost',
            __('Documentation', 'image-boost'),
            __('Documentation', 'image-boost'),
            'manage_options',
            'image-boost-docs',
            [__CLASS__, 'render_docs']
        );

        add_submenu_page(
            'image-boost',
            __('Support', 'image-boost'),
            __('Support', 'image-boost'),
            'manage_options',
            'image-boost-support',
            [__CLASS__, 'render_support']
        );
    }

    /**
     * Enqueue admin assets.
     */
    public static function enqueue_assets($hook) {
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        $page = isset($_GET['page']) ? sanitize_text_field(wp_unslash($_GET['page'])) : '';

        if (strpos($page, 'image-boost') !== 0 && $hook !== 'upload.php') {
            return;
        }

        wp_enqueue_style(
            'imgboost-admin',
            IMGBOOST_URL . 'admin/assets/css/admin-style.css',
            [],
            IMGBOOST_VERSION
        );

        wp_enqueue_script(
            'imgboost-admin',
            IMGBOOST_URL . 'admin/assets/js/admin-script.js',
            ['jquery'],
            IMGBOOST_VERSION,
            true
        );

        // Chart.js for analytics dashboard
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        if (strpos($page, 'image-boost') === 0 && isset($_GET['page']) && sanitize_text_field(wp_unslash($_GET['page'])) === 'image-boost') {
            wp_enqueue_script(
                'imgboost-chartjs',
                IMGBOOST_URL . 'admin/assets/js/chart.min.js',
                [],
                '4.5.1',
                true
            );
            $chart_data = ImgBoost_Stats::get_chart_data();
            wp_localize_script('imgboost-chartjs', 'imgboostChartData', $chart_data);
        }

        wp_localize_script('imgboost-admin', 'imgboost', [
            'ajaxurl'   => admin_url('admin-ajax.php'),
            'nonce'     => wp_create_nonce('imgboost_nonce'),
            'strings'   => [
                'optimizing'     => __('Optimizing...', 'image-boost'),
                'complete'       => __('Optimization Complete!', 'image-boost'),
                'error'          => __('Error', 'image-boost'),
                'confirm_restore'=> __('⚠️ Confirm?', 'image-boost'),
                'restored'       => __('Image restored successfully.', 'image-boost'),
                'scanning'       => __('Scanning media library...', 'image-boost'),
                'processing'     => __('Processing batch...', 'image-boost'),
                'paused'         => __('Paused', 'image-boost'),
                'saved'          => __('Settings saved!', 'image-boost'),
                'server_error'   => __('Server overloaded (5xx). Retrying ', 'image-boost'),
                'network_error'  => __('Network error', 'image-boost'),
                'label_optimized'=> __('✅ Optimized', 'image-boost'),
                'label_saved'    => __('saved', 'image-boost'),
                'btn_restore'    => __('↩ Restore', 'image-boost'),
                'btn_optimize'   => __('⚡ Optimize', 'image-boost'),
                'label_done'     => __('✅ Done', 'image-boost'),
                'label_not_opt'  => __('⏳ Not Optimized', 'image-boost'),
                /* translators: 1: number of images, 2: total size saved */
                'compressed_summary' => __('You have compressed %1$d images saving %2$s of space.', 'image-boost'),
                'all_optimized'  => __('All Images Are Optimized!', 'image-boost'),
            ],
        ]);
    }

    /**
     * Render dashboard page.
     */
    public static function render_dashboard() {
        include IMGBOOST_PATH . 'admin/views/dashboard.php';
    }

    /**
     * Render bulk optimize page.
     */
    public static function render_bulk() {
        include IMGBOOST_PATH . 'admin/views/bulk-optimize.php';
    }

    /**
     * Render settings page.
     */
    public static function render_settings() {
        include IMGBOOST_PATH . 'admin/views/settings.php';
    }

    /**
     * Render documentation page.
     */
    public static function render_docs() {
        include IMGBOOST_PATH . 'admin/views/documentation.php';
    }

    /**
     * Render support page.
     */
    public static function render_support() {
        require_once IMGBOOST_PATH . 'admin/views/support.php';
    }
}
