<?php
class SeekMake_Client_Dashboard {
    
    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_client_dashboard', 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/client-dashboard/class-client-dashboard-elementor.php';
        $widgets_manager->register(new SeekMake_Client_Dashboard_Elementor());
    }

    public function enqueue_assets() {
        wp_enqueue_style('seekmake-client-dashboard', SEEKMAKE_PLUGIN_URL . 'assets/client-dashboard/css/client-dashboard.css', array(), $this->version);
        wp_enqueue_script('seekmake-client-dashboard', SEEKMAKE_PLUGIN_URL . 'assets/client-dashboard/js/client-dashboard.js', array('jquery'), $this->version, true);
    }

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

        $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);
        }
        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 . '/dashboard/client?isEmbed=true&manufacturer=' . esc_attr($manufacturer_id) . '&lang=' . esc_attr($lang);
        ob_start();
        ?>
        <section id="seekmake-dashboard-container" class="seekmake-dashboard-container">
            <iframe sandbox="allow-downloads allow-scripts allow-same-origin allow-forms allow-popups" id="seekmake-embed-dashboard" class="seekmake-embed-dashboard" 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="client-dashboard" 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();
    }
}
