<?php
declare(strict_types=1);


/**
 * Core Web Vitals Dashboard Page
 *
 * @package ProRank\Admin\Pages
 */

namespace ProRank\SEO\Admin\Pages;

use ProRank\SEO\Core\MenuRegistry;

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


/**
 * CWV Dashboard page loader
 */


class CwvDashboardPage {
    
    /**
     * Register page
     *
     * @param MenuRegistry $menu_registry
     * @return void
     */
    public function register(MenuRegistry $menu_registry): void {
        // Page is already registered in MenuHandler
        // This method is kept for interface compatibility
    }
    
    /**
     * Render page
     *
     * @return void
     */
    public function render(): void {
        ?>
        <div id="prorank-cwv-dashboard-root" class="wrap"></div>
        <?php
    }
    
    /**
     * Enqueue assets
     *
     * @param string $hook_suffix
     * @return void
     */
    public function enqueue_assets(string $hook_suffix): void {
        if (!str_contains($hook_suffix, 'prorank-cwv-history')) {
            return;
        }
        
        // Enqueue React app
        $asset_file = PRORANK_PLUGIN_DIR . 'build/cwv-dashboard.asset.php';
        
        if (!file_exists($asset_file)) {
            return;
        }
        
        $asset = require $asset_file;
        
        wp_enqueue_script(
            'prorank-cwv-dashboard',
            PRORANK_PLUGIN_URL . 'build/cwv-dashboard.js',
            $asset['dependencies'],
            $asset['version'],
            true
        );
        
        // Enqueue Chart.js (local vendor bundle for WP.org compliance)
        wp_enqueue_script(
            'chart-js',
            PRORANK_PLUGIN_URL . 'assets/js/vendor/chart.min.js',
            [],
            '4.4.0',
            true
        );
        
        wp_enqueue_script(
            'chartjs-adapter-date-fns',
            PRORANK_PLUGIN_URL . 'assets/js/vendor/chartjs-adapter-date-fns.min.js',
            ['chart-js'],
            '3.0.0',
            true
        );
        
        wp_localize_script('prorank-cwv-dashboard', 'prorankCwv', [
            'apiUrl' => rest_url('prorank/v1'),
            'nonce' => wp_create_nonce('wp_rest'),
            'license' => [
                'status' => 'inactive',
                'tier' => 'free',
            ],
        ]);
        
        wp_enqueue_style(
            'prorank-cwv-dashboard',
            PRORANK_PLUGIN_URL . 'build/cwv-dashboard.css',
            ['wp-components'],
            $asset['version']
        );
    }
}
