<?php
class SeekMake_Instant_Quote {

    protected $version;

    public function __construct() {
        $this->version = SEEKMAKE_VERSION;
    }

    public function init() {
        $this->load_dependencies();
        $this->define_hooks();
    }

    private function load_dependencies() {
    }

    private function define_hooks() {
        add_shortcode('seekmake_instant_quote', array($this, 'render_shortcode'));
        add_action('wp_enqueue_scripts', array($this, 'enqueue_assets'));
        add_action('elementor/widgets/register', array($this, 'register_elementor_widgets'));
    }

    public function register_elementor_widgets($widgets_manager) {
        require_once SEEKMAKE_PLUGIN_DIR . 'includes/features/instant-quote/class-instant-quote-elementor.php';
        $widgets_manager->register(new SeekMake_Instant_Quote_Elementor());
    }

    public function enqueue_assets() {
        wp_enqueue_style(
            'seekmake-instant-quote',
            SEEKMAKE_PLUGIN_URL . 'assets/instant-quote/css/instant-quote.css',
            array(),
            $this->version
        );

        wp_enqueue_script(
            'seekmake-instant-quote',
            SEEKMAKE_PLUGIN_URL . 'assets/instant-quote/js/instant-quote.js',
            array('jquery'),
            $this->version,
            true
        );
    }

    public function render_shortcode($atts) {
        $atts = shortcode_atts(array(
            'manufacturer_id' => get_option('seekmake_manufacturer_id', ''),
            'button_text' => __('Get Instant Quote', 'seekmake'),
            'style' => 'primary',
            'display' => 'inline',
            'lang' => 'en',
            'align' => 'left',
            'class' => ''
        ), $atts, 'seekmake_instant_quote');

        $manufacturer_id = SeekMake_Security::sanitize_manufacturer_id($atts['manufacturer_id']);
        $button_text = SeekMake_Security::sanitize_button_text($atts['button_text']);
        $style = SeekMake_Security::sanitize_button_style($atts['style']);
        $display = SeekMake_Security::sanitize_display_mode($atts['display']);
        $lang = SeekMake_Security::sanitize_language($atts['lang']);
        $align = sanitize_text_field($atts['align']);
        $custom_class = sanitize_text_field($atts['class']);

        if (!SeekMake_Security::validate_manufacturer_id($manufacturer_id, true)) {
            return '<p class="seekmake-error">' . esc_html__('Please authenticate your SeekMake account to use this feature.', 'seekmake') . '</p>';
        }

        if ($display === 'inline') {
            return $this->render_inline($manufacturer_id, $lang);
        } else {
            return $this->render_button($manufacturer_id, $button_text, $style, $lang, $align, $custom_class);
        }
    }

    private function render_inline($manufacturer_id, $lang) {
        $iframe_url = SEEKMAKE_API_URL . '/instant-quote/?isEmbed=true&manufacturer=' . esc_attr($manufacturer_id);
        
        ob_start();
        ?>
        <section id="seekmake-quote-container" class="seekmake-quote-container">
            <iframe
                sandbox="allow-downloads allow-scripts allow-same-origin allow-forms allow-popups"
                id="seekmake-embed-iq"
                class="seekmake-embed-iq"
                src="<?php echo esc_url($iframe_url); ?>"
                style="width: 100%; height: 100vh; border: 0px"
                scrolling="no"
            ></iframe>
        </section>
        <?php
        return ob_get_clean();
    }

    private function render_button($manufacturer_id, $button_text, $style, $lang, $align, $custom_class) {
        ob_start();
        ?>
        <div class="seekmake-widget-wrapper seekmake-widget-<?php echo esc_attr($align); ?>">
            <button 
                class="seekmake-widget-button seekmake-button-<?php echo esc_attr($style); ?> seekmake-modal-trigger <?php echo esc_attr($custom_class); ?>"
                data-widget-type="instant-quote"
                data-manufacturer-id="<?php echo esc_attr($manufacturer_id); ?>"
                data-lang="<?php echo esc_attr($lang); ?>"
                type="button"
            >
                <?php echo esc_html($button_text); ?>
            </button>
        </div>
        <?php
        return ob_get_clean();
    }
}
