<?php
use Elementor\Widget_Base;
use Elementor\Controls_Manager;

class SeekMake_Astra_AI_Elementor extends Widget_Base {

    public function get_name() {
        return 'seekmake_astra_ai';
    }

    public function get_title() {
        return __('SeekMake Astra AI', 'seekmake');
    }

    public function get_icon() {
        return 'eicon-text-area';
    }

    public function get_categories() {
        return ['general'];
    }

    public function get_keywords() {
        return ['seekmake', 'astra', 'ai', 'bot', 'chat'];
    }

    protected function register_controls() {
        $this->start_controls_section(
            'content_section',
            [
                'label' => __('Content', 'seekmake'),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'manufacturer_id',
            [
                'label' => __('Manufacturer ID', 'seekmake'),
                'type' => Controls_Manager::TEXT,
                'default' => get_option('seekmake_manufacturer_id', ''),
                'description' => __('Leave empty to use the default from settings', 'seekmake'),
            ]
        );

        $this->add_control(
            'language',
            [
                'label' => __('Language', 'seekmake'),
                'type' => Controls_Manager::SELECT,
                'default' => 'en',
                'options' => [
                    'en' => __('English', 'seekmake'),
                    'es' => __('Spanish', 'seekmake'),
                    'fr' => __('French', 'seekmake'),
                    'de' => __('German', 'seekmake'),
                    'ar' => __('Arabic', 'seekmake'),
                ],
            ]
        );

        $this->end_controls_section();

        $this->start_controls_section(
            'style_section',
            [
                'label' => __('Container Style', 'seekmake'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_responsive_control(
            'iframe_height',
            [
                'label' => __('Height', 'seekmake'),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', 'vh', 'rem'],
                'range' => [
                    'px' => [
                        'min' => 300,
                        'max' => 1200,
                        'step' => 10,
                    ],
                    'vh' => [
                        'min' => 10,
                        'max' => 100,
                    ],
                ],
                'default' => [
                    'unit' => 'px',
                    'size' => 600,
                ],
                'selectors' => [
                    '{{WRAPPER}} .seekmake-embed-bot' => 'height: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'custom_css_class',
            [
                'label' => __('Custom CSS Class', 'seekmake'),
                'type' => Controls_Manager::TEXT,
                'description' => __('Add custom CSS class for additional styling', 'seekmake'),
            ]
        );

        $this->end_controls_section();
    }

    protected function render() {
        $settings = $this->get_settings_for_display();
        
        $manufacturer_id = !empty($settings['manufacturer_id']) 
            ? $settings['manufacturer_id'] 
            : get_option('seekmake_manufacturer_id', '');
        
        $manufacturer_id = SeekMake_Security::sanitize_manufacturer_id($manufacturer_id);
        $lang = SeekMake_Security::sanitize_language($settings['language']);
        $custom_class = sanitize_html_class($settings['custom_css_class']);

        if (empty($manufacturer_id)) {
            echo '<p class="seekmake-error">' . esc_html__('Manufacturer ID is not configured.', 'seekmake') . '</p>';
            return;
        }

        $this->render_inline($manufacturer_id, $lang, $custom_class);
    }

    private function render_inline($manufacturer_id, $lang, $custom_class) {
        $iframe_url = SEEKMAKE_API_URL . '/astra-bot?isEmbed=true&manufacturer=' . esc_attr($manufacturer_id) . '&lang=' . esc_attr($lang);
        $escaped_class = esc_attr($custom_class);
        ?>
        <section id="seekmake-bot-widget" class="seekmake-astra-container <?php echo $escaped_class; ?>">
            <iframe
                id="seekmake-embed-bot"
                class="seekmake-embed-bot"
                src="<?php echo esc_url($iframe_url); ?>"
                sandbox="allow-downloads allow-scripts allow-same-origin allow-forms"
                allow="identity-credentials-get"
                style="width: 100%; border: 0px;"
                scrolling="no"
            ></iframe>
        </section>
        <script src="<?php echo esc_url(SEEKMAKE_API_URL . '/assets/public/iq-api-latest.js'); ?>"></script>
        <?php
    }
}
