<?php
namespace EveAI\Chat;

class Assets implements Loadable {
    public function init() {
        add_action('wp_enqueue_scripts', [$this, 'register_assets']);
        add_action('wp_enqueue_scripts', [$this, 'maybe_load_assets']);
    }

    public function register_assets() {
        // Register Socket.IO (from CDN)
        wp_register_script(
            'socket-io',
            'https://cdn.socket.io/4.0.1/socket.io.min.js',
            [],
            '4.0.1',
            true
        );

        // Register Marked library for markdown (from CDN)
        wp_register_script(
            'marked',
            'https://cdn.jsdelivr.net/npm/marked/marked.min.js',
            [],
            '1.0.0',
            true
        );

        // Register EveAI core scripts
        wp_register_script(
            'eveai-token-manager',
            EVEAI_CHAT_PLUGIN_URL . 'assets/js/eveai-token-manager.js',
            ['socket-io'],
            EVEAI_CHAT_VERSION,
            true
        );

        wp_register_script(
            'eveai-sdk',
            EVEAI_CHAT_PLUGIN_URL . 'assets/js/eveai-sdk.js',
            ['eveai-token-manager'],
            EVEAI_CHAT_VERSION,
            true
        );

        wp_register_script(
            'eveai-chat-widget',
            EVEAI_CHAT_PLUGIN_URL . 'assets/js/eveai-chat-widget.js',
            ['eveai-sdk', 'marked'],
            EVEAI_CHAT_VERSION,
            true
        );

        // Register styles
        wp_register_style(
            'eveai-chat',
            EVEAI_CHAT_PLUGIN_URL . 'assets/css/eveai-chat-style.css',
            [],
            EVEAI_CHAT_VERSION
        );

        wp_register_style(
            'material-icons',
            'https://fonts.googleapis.com/icon?family=Material+Icons',
            [],
            EVEAI_CHAT_VERSION
        );
    }

    public function maybe_load_assets() {
        global $post;

        // Only load if shortcode is present
        if (is_a($post, 'WP_Post') && has_shortcode($post->post_content, 'eveai_chat')) {
            $this->load_assets();
        }
    }

    private function load_assets() {
        error_log('Starting to load EveAI assets');
        // Enqueue all required scripts
        wp_enqueue_script('socket-io');
        wp_enqueue_script('marked');
        wp_enqueue_script('eveai-token-manager');
        wp_enqueue_script('eveai-sdk');
        wp_enqueue_script('eveai-chat-widget');

        // Enqueue styles
        wp_enqueue_style('material-icons');
        wp_enqueue_style('eveai-chat');

        // Localize script with WordPress-specific data
        wp_localize_script('eveai-sdk', 'eveaiWP', [
            'nonce' => wp_create_nonce('wp_rest'),
            'settings' => $this->get_public_settings()
        ]);
        error_log('EveAI assets loaded');
    }

    private function get_public_settings() {
        $settings = get_option('eveai_chat_settings', []);

        return [
            'socket_url' => $settings['socket_url'] ?? 'http://localhost:5002',
            'auth_url' => $settings['auth_url'] ?? 'http://localhost:5001',
            'tenant_id' => $settings['tenant_id'] ?? '',
            'wpBaseUrl' => rest_url(),
        ];
    }
}