<?php
/**
 * Public - Enqueues chat widget on frontend
 *
 * @package ContentReply_AI
 */

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

class WPSC_Public {

    public function __construct() {
        add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) );
        add_action( 'wp_footer', array( $this, 'render_widget' ) );
    }

    /**
     * Enqueue frontend CSS and JS
     */
    public function enqueue_assets() {
        if ( ! $this->should_display() ) {
            return;
        }

        wp_enqueue_style( 'wpsc-widget', WPSC_PLUGIN_URL . 'public/chat-widget.css', array(), WPSC_VERSION );
        wp_enqueue_script( 'wpsc-widget', WPSC_PLUGIN_URL . 'public/chat-widget.js', array(), WPSC_VERSION, true );

        wp_localize_script( 'wpsc-widget', 'wpscConfig', array(
            'restUrl'       => esc_url_raw( rest_url( 'wpsc/v1/' ) ),
            'nonce'         => wp_create_nonce( 'wp_rest' ),
            'botName'       => get_option( 'wpsc_bot_name', 'Support Bot' ),
            'welcome'       => get_option( 'wpsc_welcome_message', 'Hi! How can I help you today?' ),
            'primaryColor'  => get_option( 'wpsc_primary_color', '#2e7d32' ),
            'position'      => get_option( 'wpsc_widget_position', 'bottom-right' ),
            'contactUrl'    => get_option( 'wpsc_contact_url', '/contact' ),
            'fallback'      => get_option( 'wpsc_fallback_message' ),
            'showOnMobile'  => get_option( 'wpsc_show_on_mobile', true ),
        ) );
    }

    /**
     * Render the chat widget HTML in footer
     */
    public function render_widget() {
        if ( ! $this->should_display() ) {
            return;
        }

        $position = get_option( 'wpsc_widget_position', 'bottom-right' );
        $bot_name = esc_attr( get_option( 'wpsc_bot_name', 'Support Bot' ) );
        ?>
        <div id="wpsc-chat-widget" class="wpsc-widget wpsc-<?php echo esc_attr( $position ); ?>" style="display:none;">
            <!-- Chat Toggle Button -->
            <button id="wpsc-toggle-btn" class="wpsc-toggle-btn" aria-label="Open chat">
                <img id="wpsc-icon-chat" src="<?php echo esc_url( WPSC_PLUGIN_URL . 'assets/bot-icon.png' ); ?>" alt="Chat" width="40" height="40" />
                <svg id="wpsc-icon-close" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display:none;">
                    <line x1="18" y1="6" x2="6" y2="18"></line>
                    <line x1="6" y1="6" x2="18" y2="18"></line>
                </svg>
            </button>

            <!-- Chat Window -->
            <div id="wpsc-chat-window" class="wpsc-chat-window" style="display:none;">
                <div class="wpsc-chat-header">
                    <div class="wpsc-header-info">
                        <div class="wpsc-bot-avatar">
                            <img src="<?php echo esc_url( WPSC_PLUGIN_URL . 'assets/bot-icon.png' ); ?>" alt="Bot" width="36" height="36" />
                        </div>
                        <div>
                            <div class="wpsc-bot-name"><?php echo esc_html( $bot_name ); ?></div>
                            <div class="wpsc-bot-status">Ask me anything about our site!</div>
                        </div>
                    </div>
                    <button id="wpsc-close-btn" class="wpsc-close-btn" aria-label="Close chat">&times;</button>
                </div>
                <div id="wpsc-messages" class="wpsc-messages"></div>
                <div class="wpsc-input-area">
                    <input type="text" id="wpsc-input" class="wpsc-input" placeholder="Type your message..." autocomplete="off" />
                    <button id="wpsc-send-btn" class="wpsc-send-btn" aria-label="Send message">
                        <svg width="18" height="18" viewBox="0 0 24 24" fill="currentColor">
                            <path d="M2.01 21L23 12 2.01 3 2 10l15 2-15 2z"></path>
                        </svg>
                    </button>
                </div>
            </div>
        </div>
        <?php
    }

    /**
     * Check if widget should be displayed
     */
    private function should_display() {
        // Don't show in admin
        if ( is_admin() ) {
            return false;
        }

        // Check if enabled
        if ( ! get_option( 'wpsc_enabled', true ) ) {
            return false;
        }

        // Allow widget in demo mode even without API key
        if ( get_option( 'wpsc_demo_mode', false ) ) {
            return true;
        }

        // Check if API key is set
        if ( empty( get_option( 'wpsc_api_key', '' ) ) ) {
            return false;
        }

        return true;
    }
}
