<?php
declare(strict_types=1);


/**
 * Support Page
 *
 * @package ProRank\Admin\Pages
 */

namespace ProRank\SEO\Admin\Pages;

use ProRank\SEO\Core\MenuRegistry;

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


/**
 * Support page loader
 */


class SupportPage {
    /**
     * 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 {
        // Hide default WordPress admin notices for this page
        ?>
        <style>
            /* Hide WordPress admin notices that appear above our custom header */
            .prorank-seo_page_prorank-support .notice:not(.prorank-notice),
            .prorank-seo_page_prorank-support .error:not(.prorank-notice),
            .prorank-seo_page_prorank-support .updated:not(.prorank-notice),
            .prorank-seo_page_prorank-support .update-nag {
                display: none !important;
            }
            
            /* Ensure our React app takes full width */
            .prorank-seo_page_prorank-support #wpcontent {
                padding-left: 0;
            }
            
            .prorank-seo_page_prorank-support #wpbody-content {
                padding-bottom: 0;
            }
            
            .prorank-seo_page_prorank-support .wrap {
                margin: 0;
            }
        </style>
        <div
            id="prorank-admin-root"
            data-page="support"
            class="wrap prorank-admin-wrapper prorank-seo-admin-page prorank-page prorank-admin-page"
        ></div>
        <?php
    }
    
    /**
     * Enqueue assets
     *
     * @param string $hook_suffix
     * @return void
     */
    public function enqueue_assets(string $hook_suffix): void {
        if (!str_contains($hook_suffix, 'prorank-support')) {
            return;
        }
        
        // Enqueue React app
        $asset_file = PRORANK_PLUGIN_DIR . 'build/admin.asset.php';
        
        if (!file_exists($asset_file)) {
            return;
        }
        
        $asset = require $asset_file;
        $script_file = PRORANK_PLUGIN_DIR . 'build/admin.js';
        $script_version = file_exists($script_file) ? (string) filemtime($script_file) : (string) ($asset['version'] ?? PRORANK_VERSION);
        
        wp_enqueue_script(
            'prorank-support',
            PRORANK_PLUGIN_URL . 'build/admin.js',
            $asset['dependencies'],
            $script_version,
            true
        );

        // Enqueue the shared admin stylesheet used by the React admin shell.
        $main_css = PRORANK_PLUGIN_DIR . 'build/main.css';
        $index_css = PRORANK_PLUGIN_DIR . 'build/index.css';

        if (file_exists($main_css)) {
            wp_enqueue_style(
                'prorank-support',
                PRORANK_PLUGIN_URL . 'build/main.css',
                ['wp-components'],
                filemtime($main_css)
            );
        } elseif (file_exists($index_css)) {
            wp_enqueue_style(
                'prorank-support',
                PRORANK_PLUGIN_URL . 'build/index.css',
                ['wp-components'],
                filemtime($index_css)
            );
        }

        // Get build fingerprint
        $build_fingerprint = defined('PRORANK_BUILD_FINGERPRINT') ? PRORANK_BUILD_FINGERPRINT : '';
        
        $notices = [];

        wp_localize_script('prorank-support', 'prorankSupport', [
            'apiUrl' => rest_url('prorank/v1'),
            'nonce' => wp_create_nonce('wp_rest'),
            'pluginUrl' => PRORANK_PLUGIN_URL,
            'version' => PRORANK_VERSION,
            'buildFingerprint' => $build_fingerprint,
            'notices' => $notices,
            'supportLinks' => [
                'documentation' => 'https://prorank.io/docs/',
                'feedback' => admin_url( 'admin.php?page=prorank-support' ),
                'changelog' => 'https://wordpress.org/plugins/prorank-seo/#developers',
            ],
        ]);
    }
}
