<?php

declare(strict_types=1);

namespace ROITheme\Admin\Infrastructure\Services;

/**
 * Servicio para enqueue de assets del panel de administración
 *
 * Infrastructure - WordPress specific
 */
final class AdminAssetEnqueuer
{
    private const ADMIN_PAGE_SLUG = 'roi-theme-admin';

    public function __construct(
        private readonly string $themeUri
    ) {
    }

    /**
     * Registra los hooks de WordPress
     */
    public function register(): void
    {
        add_action('admin_enqueue_scripts', [$this, 'enqueueAssets']);
    }

    /**
     * Enqueue de assets solo en la página del dashboard
     *
     * @param string $hook Hook name de WordPress
     */
    public function enqueueAssets(string $hook): void
    {
        // Solo cargar en nuestra página de admin
        if (!$this->isAdminPage($hook)) {
            return;
        }

        $this->enqueueStyles();
        $this->enqueueScripts();
    }

    /**
     * Verifica si estamos en la página del dashboard
     *
     * @param string $hook Hook name
     * @return bool
     */
    private function isAdminPage(string $hook): bool
    {
        return strpos($hook, self::ADMIN_PAGE_SLUG) !== false;
    }

    /**
     * Enqueue de estilos CSS
     */
    private function enqueueStyles(): void
    {
        // Bootstrap 5 CSS
        wp_enqueue_style(
            'bootstrap',
            'https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css',
            [],
            '5.3.2'
        );

        // Bootstrap Icons
        wp_enqueue_style(
            'bootstrap-icons',
            'https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css',
            [],
            '1.11.3'
        );

        // Estilos del dashboard
        wp_enqueue_style(
            'roi-admin-dashboard',
            $this->themeUri . '/Admin/Infrastructure/Ui/Assets/Css/admin-dashboard.css',
            ['bootstrap', 'bootstrap-icons'],
            filemtime(get_template_directory() . '/Admin/Infrastructure/Ui/Assets/Css/admin-dashboard.css')
        );
    }

    /**
     * Enqueue de scripts JavaScript
     */
    private function enqueueScripts(): void
    {
        // Bootstrap 5 JS Bundle (incluye Popper)
        // IMPORTANTE: Cargar en header (false) para que esté disponible antes del contenido
        wp_enqueue_script(
            'bootstrap',
            'https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js',
            [],
            '5.3.2',
            false  // Load in header, not footer - required for Bootstrap tabs to work
        );

        // Script del dashboard
        wp_enqueue_script(
            'roi-admin-dashboard',
            $this->themeUri . '/Admin/Infrastructure/Ui/Assets/Js/admin-dashboard.js',
            ['bootstrap'],
            filemtime(get_template_directory() . '/Admin/Infrastructure/Ui/Assets/Js/admin-dashboard.js'),
            true
        );

        // Script de toggle para exclusiones (Plan 99.11)
        wp_enqueue_script(
            'roi-exclusion-toggle',
            $this->themeUri . '/Admin/Shared/Infrastructure/Ui/Assets/Js/exclusion-toggle.js',
            ['roi-admin-dashboard'],
            filemtime(get_template_directory() . '/Admin/Shared/Infrastructure/Ui/Assets/Js/exclusion-toggle.js'),
            true
        );

        // Pasar variables al JavaScript
        wp_localize_script(
            'roi-admin-dashboard',
            'roiAdminDashboard',
            [
                'nonce' => wp_create_nonce('roi_admin_dashboard'),
                'ajaxurl' => admin_url('admin-ajax.php')
            ]
        );
    }
}
