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

class AdminAssets
{
    public static function init(): void
    {
        add_action('admin_enqueue_scripts', [self::class, 'enqueue']);
    }

    public static function enqueue(string $hook_suffix): void
    {
        $screen = function_exists('get_current_screen') ? get_current_screen() : null;
        $screen_id = $screen ? $screen->id : '';

        // $allowed = [
        //     'toplevel_page_zoddak',
        //     'rs-zoddak_page_rs-zoddak-settings',
        //     'settings_page_rs-zoddak',
        // ];

        // if ($screen_id && !in_array($screen_id, $allowed, true)) {
        //     return;
        // }

        // Si no estás seguro del screen id, usa hook_suffix temporalmente:
        // error_log("hook_suffix=$hook_suffix screen_id=$screen_id");

        wp_enqueue_style('wp-color-picker');
        wp_enqueue_script('wp-color-picker');
        wp_add_inline_script(
            'wp-color-picker',
            'jQuery(document).ready(function($){$(".my-color-field").wpColorPicker();});'
        );

        wp_enqueue_script(
            'zoddak-admin-js',
            ZODDAK_PLUGIN_URL . 'assets/js/custom-properties-display.js',
            ['jquery', 'wp-color-picker'],
            '1.0.0',
            true
        );

        wp_enqueue_script(
            'zoddak-custom-filter-card',
            ZODDAK_PLUGIN_URL . 'assets/js/custom-filter-card.js',
            ['jquery', 'wp-color-picker'],
            '1.0.0',
            true
        );

        wp_localize_script('zoddak-admin-js', 'zoddakVars', [
            'previewSelector' => '#property-preview'
        ]);

        $options = [
            'horizontalLineColor' => get_option('zoddak_color_horizontal_line', '#c61c1c'),
            'typeRefColor'        => get_option('zoddak_type_property_btn_color'),
            'currentPageColor'    => get_option('zoddak_current_page_color'),
            'cardBgColor'         => get_option('zoddak_card_bg_color'),
            'cardBorder'          => get_option('zoddak_border_rounded'),
        ];

        wp_enqueue_style(
            'zoddak-admin-style-base',
            ZODDAK_PLUGIN_URL . 'assets/css/style.css',
            [],
            '1.0.0'
        );

        $root = "
            :root{
                --line-hor: {$options['horizontalLineColor']};
                --ref-type-color: {$options['typeRefColor']};
                --current-page-color: {$options['currentPageColor']};
                --card-bg-color: {$options['cardBgColor']};
                --card-border: {$options['cardBorder']};
            }
        ";
        wp_add_inline_style('zoddak-admin-style-base', $root);

        $filterOpts = [
            'filterBg'    => get_option('zoddak-bg-color-search-menu', '#f5f5f5'),
            'filterBtn'   => get_option('zoddak-btn-color-search-menu', '#070100'),
            'filterReset' => get_option('zoddak-reset-btn-color', '#ffffff'),
        ];

        wp_enqueue_style(
            'zoddak-customize-filter',
            ZODDAK_PLUGIN_URL . 'assets/css/custom-filter-card.css',
            [],
            '1.0.0'
        );

        $rootFilter = "
            :root{
                --filter-bg: {$filterOpts['filterBg']};
                --filter-btn: {$filterOpts['filterBtn']};
                --filter-reset: {$filterOpts['filterReset']};
            }
        ";
        wp_add_inline_style('zoddak-customize-filter', $rootFilter);

        wp_enqueue_style(
            'zoddak-admin-style-detail',
            ZODDAK_PLUGIN_URL . 'assets/css/custom-details-page.css',
            ['dashicons'],
            '1.0.0'
        );

        $detailOpts = [
            'bgFeatureColor'    => get_option('zoddak-icons-color', '#110400'),
            'bgTxtFeatureColor' => get_option('zoddak-icons-txt-color'),
        ];

        $dynamicCss = "
            :root {
                --bg-features-color : {$detailOpts['bgFeatureColor']};
                --color-text-features : {$detailOpts['bgTxtFeatureColor']};
            }
        ";
        wp_add_inline_style('zoddak-admin-style-detail', $dynamicCss);

        // Mejor que FontAwesome sea global (un solo handle), pero si lo dejas aquí, ok:
        wp_enqueue_style(
            'zoddak-fontawesome',
            'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css',
            [],
            '6.5.0'
        );

        wp_enqueue_script(
            'zoddak-custom-details-interactions',
            ZODDAK_PLUGIN_URL . 'assets/js/custom-details-page.js',
            ['jquery'],
            '1.0.0',
            true
        );

        wp_enqueue_script(
            'zoddak-newdevs-admin',
            ZODDAK_PLUGIN_URL . 'assets/js/newdevs_admin_settings.js',
            ['jquery'],
            '1.0.0',
            true
        );

        wp_enqueue_style(
            'select2-css',
            'https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css'
        );
        wp_enqueue_script(
            'select2-js',
            'https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js',
            ['jquery'],
            null,
            true
        );

        wp_enqueue_script(
            'zoddak-admin-routes',
            ZODDAK_PLUGIN_URL . 'assets/js/newdevs-routes.js',
            [],
            '1.0.0'
        );

        wp_enqueue_style(
            'zoddak-admin-settings',
            ZODDAK_PLUGIN_URL . 'assets/css/newdevs-admin-settings.css',
            [],
            '1.0.0'
        );
        wp_enqueue_script(
            'zoddak-admin-settings',
            ZODDAK_PLUGIN_URL . 'assets/js/newdevs-admin-settings.js',
            [],
            '1.0.0'
        );
       
        wp_enqueue_style('dashicons');

        wp_enqueue_style(
            'property-details-fontawesome',
            'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css',
            [],
            '5.15.4'
        );
        wp_enqueue_style('leaflet-css', 'https://unpkg.com/leaflet@1.9.4/dist/leaflet.css');
        wp_enqueue_script('leaflet-js', 'https://unpkg.com/leaflet@1.9.4/dist/leaflet.js', [], null, true);
        wp_enqueue_style('select2', 'https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css');
        wp_enqueue_script('select2', 'https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js', ['jquery'], null, true);
        wp_add_inline_script('select2', "jQuery(document).ready(function($){ $('#area').select2({ placeholder: 'Select areas', width: '100%', allowClear: true }); });");
    }
}