<?php

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

class List2Play_Servers_Styles {
    
    public function enqueue_styles() {
        // Flag Icons CSS - using local file to comply with WordPress.org requirements
        $flag_icons_path = LIST2PLAY_SERVERS_PLUGIN_DIR . 'public/assets/css/flag-icons.min.css';
        if (file_exists($flag_icons_path)) {
            wp_enqueue_style(
                'flag-icons',
                LIST2PLAY_SERVERS_PLUGIN_URL . 'public/assets/css/flag-icons.min.css',
                [],
                '7.0.0'
            );
        }
        
        // Plugin styles - with cache busting
        wp_enqueue_style(
            'list2play-servers',
            LIST2PLAY_SERVERS_PLUGIN_URL . 'public/assets/css/list2play-servers.css',
            file_exists($flag_icons_path) ? ['flag-icons'] : [],
            LIST2PLAY_SERVERS_VERSION . '.' . filemtime(LIST2PLAY_SERVERS_PLUGIN_DIR . 'public/assets/css/list2play-servers.css')
        );
        
        // Chart.js library for players history charts
        // Using local file to comply with WordPress.org requirements (no external CDN)
        // Chart.js is optional - if not available, charts will not be displayed
        $chart_js_path = LIST2PLAY_SERVERS_PLUGIN_DIR . 'public/assets/js/chart.umd.min.js';
        $chart_js_dependencies = ['jquery'];
        if (file_exists($chart_js_path) && filesize($chart_js_path) > 1000) { // Check if file is not just placeholder
            wp_enqueue_script(
                'chart-js',
                LIST2PLAY_SERVERS_PLUGIN_URL . 'public/assets/js/chart.umd.min.js',
                [],
                '4.5.1',
                true
            );
            $chart_js_dependencies[] = 'chart-js';
        }
        
        wp_enqueue_script(
            'list2play-servers',
            LIST2PLAY_SERVERS_PLUGIN_URL . 'public/assets/js/list2play-servers.js',
            $chart_js_dependencies,
            LIST2PLAY_SERVERS_VERSION,
            true
        );
        
        // Pass API base URL to JavaScript
        $list2play_api_base_url = get_option('list2play_api_base_url', 'https://list2play.net/api');
        wp_localize_script('list2play-servers', 'list2playServers', [
            'apiBaseUrl' => esc_url_raw($list2play_api_base_url),
            'chartJsAvailable' => file_exists($chart_js_path) && filesize($chart_js_path) > 1000,
        ]);
    }
}

