<?php

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

class AI_eShop_Chat_Shortcode {

    
    private static $instance = null;

    
    public static function get_instance() {
        if (null === self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    
    private function __construct() {
        add_shortcode('aieo-chat', array($this, 'render_chat'));
        add_shortcode('aieo_chat', array($this, 'render_chat')); 
    }

    
    public function render_chat($atts) {
        
        if (get_option('aieo_chat_enabled', '0') !== '1') {
            return '<p>' . esc_html__('Chat is currently disabled.', 'ai-eshop-optimizer') . '</p>';
        }

        
        if (get_option('aieo_chat_require_login', '0') === '1' && !is_user_logged_in()) {
            return '<p>' . sprintf(
                
                wp_kses_post(__('Please <a href="%s">log in</a> to use the chat.', 'ai-eshop-optimizer')),
                esc_url(wp_login_url(get_permalink()))
            ) . '</p>';
        }

        
        $atts = shortcode_atts(array(
            'height' => '600px',
            'width' => '800px',
            'pictures' => 'enabled'
        ), $atts);

        
        $chat_id = 'aieo-chat-' . wp_rand(1000, 9999);

        
        wp_enqueue_style('aieo-chat-frontend');
        wp_enqueue_script('aieo-chat-frontend');

        
        $output = '<div class="aieo-chat-wrapper" id="' . esc_attr($chat_id) . '" ';
        $output .= 'data-height="' . esc_attr($atts['height']) . '" ';
        $output .= 'data-width="' . esc_attr($atts['width']) . '" ';
        $output .= 'data-pictures="' . esc_attr($atts['pictures']) . '">';
        $output .= '<div class="aieo-chat-loading">' . esc_html__('Loading chat...', 'ai-eshop-optimizer') . '</div>';
        $output .= '</div>';

        return $output;
    }
}
