<?php

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

class AIEO_DMM_Waitlist extends AIEO_DMM_Module_Base {

    public function get_id()       { return 'waitlist'; }
    public function get_label()    { return __('Waitlist Subscribers', 'ai-eshop-optimizer'); }
    public function get_priority() { return 67; } 
    public function get_group()    { return 'customer_profiling'; }

    const OPTION_KEY     = 'aieo_dmm_waitlist';
    const SCHEMA_OPTION  = 'aieo_waitlist_schema_version';
    const SCHEMA_VERSION = '1';
    const MIGRATED_FLAG  = 'aieo_waitlist_ppe_migrated';

    public static function get_table_name() {
        global $wpdb;
        return $wpdb->prefix . 'aieo_waitlist';
    }

    
    
    

    public function register_hooks() {
        
        add_action('wp_ajax_aieo_dmm_waitlist_save',   array($this, 'ajax_save_settings'));
        add_action('wp_ajax_aieo_dmm_waitlist_delete', array($this, 'ajax_delete_subscriber'));

        
        add_action('plugins_loaded', array($this, 'maybe_install'), 20);

        
        
        
        
        
        if ( ! defined( 'AIEO_RUNTIME_OWNS_AJAX' ) ) {
            add_action('wp_ajax_aieo_waitlist_subscribe',        array($this, 'ajax_subscribe'));
            add_action('wp_ajax_nopriv_aieo_waitlist_subscribe', array($this, 'ajax_subscribe'));
            add_action('wp_ajax_om_ppe_waitlist_subscribe',        array($this, 'ajax_subscribe'), 1);
            add_action('wp_ajax_nopriv_om_ppe_waitlist_subscribe', array($this, 'ajax_subscribe'), 1);
        }

        
        
        
        add_action('woocommerce_product_set_stock_status', array($this, 'on_stock_changed'), 10, 3);

        
        
        
        
        
        
        
        $settings = $this->get_settings();
        $dash_on  = class_exists('AIEO_DMM_Product_Display')
            && AIEO_DMM_Product_Display::module_is_enabled(self::OPTION_KEY);
        if ($dash_on && !empty($settings['frontend_enabled']) && $settings['frontend_enabled'] === 'yes') {
            
            
            
            
            
            
            
            $is_block_theme = function_exists('wp_is_block_theme') && wp_is_block_theme();
            $use_wc_hook    = (bool) apply_filters('aieo_dmm_waitlist_fse_use_hook', ! $is_block_theme);
            if ($use_wc_hook) {
                add_action('woocommerce_simple_add_to_cart',   array($this, 'render_form_hook'), 30);
                add_action('woocommerce_after_variations_form', array($this, 'render_form_hook'), 30);
            }
            add_action('wp_enqueue_scripts',                array($this, 'enqueue_frontend_assets'));
            add_shortcode('aieo_waitlist', array($this, 'shortcode_handler'));
        }
    }

    
    public function is_active() {
        $stored = $this->get_settings();
        if (!isset($stored['frontend_enabled']) || $stored['frontend_enabled'] !== 'yes') {
            return false;
        }
        
        
        if (isset($stored['enabled']) && empty($stored['enabled'])) {
            return false;
        }
        return true;
    }

    public function maybe_install() {
        if (get_option(self::SCHEMA_OPTION, '') !== self::SCHEMA_VERSION) {
            $this->install_table();
            update_option(self::SCHEMA_OPTION, self::SCHEMA_VERSION, false);
        }
        if (!get_option(self::MIGRATED_FLAG, 0)) {
            $this->migrate_from_ppe();
            update_option(self::MIGRATED_FLAG, 1, false);
        }
    }

    private function install_table() {
        global $wpdb;
        $table   = self::get_table_name();
        $charset = $wpdb->get_charset_collate();
        $sql = "CREATE TABLE {$table} (
            id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            email varchar(255) NOT NULL,
            product_id bigint(20) unsigned NOT NULL,
            variation_id bigint(20) unsigned NOT NULL DEFAULT 0,
            status varchar(20) NOT NULL DEFAULT 'subscribed',
            created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
            notified_at datetime NULL DEFAULT NULL,
            PRIMARY KEY  (id),
            UNIQUE KEY email_pid_vid (email, product_id, variation_id),
            KEY product_id (product_id),
            KEY status (status)
        ) {$charset};";
        if (!function_exists('dbDelta')) { require_once ABSPATH . 'wp-admin/includes/upgrade.php'; }
        dbDelta($sql);
    }

    
    private function migrate_from_ppe() {
        global $wpdb;
        $ppe = $wpdb->prefix . 'om_ppe_waitlist';
        $exists = $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $ppe));
        if ($exists !== $ppe) { return; }
        $dest = self::get_table_name();
        
        $wpdb->query("INSERT IGNORE INTO {$dest}
            (email, product_id, variation_id, status, created_at, notified_at)
            SELECT email, product_id, variation_id, status, created_at, notified_at
            FROM {$ppe}");
    }

    
    
    

    public function get_settings() {
        $stored = get_option(self::OPTION_KEY, array());
        if (!is_array($stored)) { $stored = array(); }
        return array_merge(array(
            'frontend_enabled'         => 'no',  
            'form_title'               => __('Email me when this item is back in stock.', 'ai-eshop-optimizer'),
            'form_email_label'         => __('Your Email Address', 'ai-eshop-optimizer'),
            'form_button_text'         => __('Notify Me', 'ai-eshop-optimizer'),
            'form_success_message'     => __('You have been successfully added to our stock waitlist. As soon as new stock becomes available, we will notify you via email.', 'ai-eshop-optimizer'),
            'email_subject'            => __('Back in Stock: {product_name}', 'ai-eshop-optimizer'),
            'email_message'            => __('Hello, thanks for your patience — your {product_name} is now back in stock! Stock is limited; this email is not a guarantee you will get one.', 'ai-eshop-optimizer'),
            'display_on_backorders'    => 'no',

            
            
            
            
            
            'title_size'               => 16,
            'text_size'                => 14,
            'bg_color'                 => '#f8f9fa',
            'border_color'             => '#e9ecef',
            'border_width'             => 1,
            'border_radius'            => 8,
            'btn_bg_color'             => '',
            'btn_text_color'           => '',
            'btn_border_color'         => '',
            'btn_border_width'         => '',
            'btn_border_radius'        => '',
        ), $stored);
    }

    
    
    

    public function render_subtab() {
        $paged    = max(1, isset($_GET['waitlist_paged']) ? (int) $_GET['waitlist_paged'] : 1);
        $per_page = 50;
        $this->render_template('waitlist.php', array(
            'settings'     => $this->get_settings(),
            'subscribers'  => $this->fetch_subscribers($per_page, $paged),
            'total'        => $this->count_subscribers(),
            'per_page'     => $per_page,
            'current_page' => $paged,
        ));
    }

    public function ajax_save_settings() {
        AIEO_DMM::verify_ajax_request();

        $data = array(
            'frontend_enabled'      => !empty($_POST['frontend_enabled']) ? 'yes' : 'no',
            'form_title'            => isset($_POST['form_title'])            ? sanitize_text_field(wp_unslash($_POST['form_title']))            : '',
            'form_email_label'      => isset($_POST['form_email_label'])      ? sanitize_text_field(wp_unslash($_POST['form_email_label']))      : '',
            'form_button_text'      => isset($_POST['form_button_text'])      ? sanitize_text_field(wp_unslash($_POST['form_button_text']))      : '',
            'form_success_message'  => isset($_POST['form_success_message'])  ? sanitize_textarea_field(wp_unslash($_POST['form_success_message']))  : '',
            'email_subject'         => isset($_POST['email_subject'])         ? sanitize_text_field(wp_unslash($_POST['email_subject']))         : '',
            'email_message'         => isset($_POST['email_message'])         ? sanitize_textarea_field(wp_unslash($_POST['email_message']))     : '',
            'display_on_backorders' => !empty($_POST['display_on_backorders']) ? 'yes' : 'no',
        );

        
        $num_clamps = array(
            'title_size'         => array(8, 48),
            'text_size'          => array(8, 32),
            'border_width'       => array(0, 10),
            'border_radius'      => array(0, 40),
            'btn_border_width'   => array(0, 10),
            'btn_border_radius'  => array(0, 40),
        );
        foreach ($num_clamps as $k => list($min, $max)) {
            if (isset($_POST[$k])) {
                $data[$k] = max($min, min($max, (int) wp_unslash($_POST[$k])));
            }
        }

        
        
        foreach (array('bg_color', 'border_color', 'btn_bg_color', 'btn_text_color', 'btn_border_color') as $k) {
            if (isset($_POST[$k])) {
                $v = sanitize_hex_color(wp_unslash((string) $_POST[$k]));
                if ($v !== null && $v !== '') { $data[$k] = $v; }
            }
        }

        
        
        $existing = get_option(self::OPTION_KEY, array());
        if (!is_array($existing)) { $existing = array(); }
        update_option(self::OPTION_KEY, array_merge($existing, $data), false);

        wp_send_json_success(array('message' => __('Waitlist settings saved.', 'ai-eshop-optimizer')));
    }

    public function ajax_delete_subscriber() {
        AIEO_DMM::verify_ajax_request();
        $id = isset($_POST['id']) ? (int) $_POST['id'] : 0;
        if ($id <= 0) { wp_send_json_error(array('message' => __('Invalid id.', 'ai-eshop-optimizer'))); }
        global $wpdb;
        $wpdb->delete(self::get_table_name(), array('id' => $id), array('%d'));
        wp_send_json_success();
    }

    private function fetch_subscribers($per_page, $paged) {
        global $wpdb;
        $table  = self::get_table_name();
        $offset = ($paged - 1) * $per_page;
        return $wpdb->get_results($wpdb->prepare(
            "SELECT id, email, product_id, variation_id, status, created_at, notified_at
             FROM {$table} ORDER BY id DESC LIMIT %d OFFSET %d",
            $per_page, $offset
        ));
    }

    private function count_subscribers() {
        global $wpdb;
        return (int) $wpdb->get_var('SELECT COUNT(*) FROM ' . self::get_table_name());
    }

    
    
    

    public function enqueue_frontend_assets() {
        if (!is_product()) { return; }
        $css_rel = 'assets/css/aieo-waitlist.css';
        $js_rel  = 'assets/js/aieo-waitlist.js';
        $css_path = dirname(AIEO_FILE) . '/' . $css_rel;
        $js_path  = dirname(AIEO_FILE) . '/' . $js_rel;
        $css_ver  = file_exists($css_path) ? filemtime($css_path) : AIEO_VERSION;
        $js_ver   = file_exists($js_path)  ? filemtime($js_path)  : AIEO_VERSION;

        wp_enqueue_style('aieo-waitlist',  plugins_url($css_rel, AIEO_FILE), array(), $css_ver);

        
        
        
        
        $s = $this->get_settings();
        
        
        
        $btn_bg     = trim((string) ($s['btn_bg_color']     ?? ''));
        $btn_text   = trim((string) ($s['btn_text_color']   ?? ''));
        $btn_border = trim((string) ($s['btn_border_color'] ?? ''));
        $btn_bw     = (string) ($s['btn_border_width']  ?? '');
        $btn_br     = (string) ($s['btn_border_radius'] ?? '');
        $vars = sprintf(
            '.aieo-waitlist-container{'
          . '--aieo-wl-bg:%s;'
          . '--aieo-wl-border:%s;'
          . '--aieo-wl-border-w:%dpx;'
          . '--aieo-wl-border-r:%dpx;'
          . '--aieo-wl-title-size:%dpx;'
          . '--aieo-wl-text-size:%dpx;'
          . '--aieo-wl-btn-bg:%s;'
          . '--aieo-wl-btn-text:%s;'
          . '--aieo-wl-btn-border:%s;'
          . '--aieo-wl-btn-border-w:%s;'
          . '--aieo-wl-btn-border-r:%s;'
          . '}',
            esc_attr((string) ($s['bg_color']            ?? '#f8f9fa')),
            esc_attr((string) ($s['border_color']        ?? '#e9ecef')),
            (int) ($s['border_width']      ?? 1),
            (int) ($s['border_radius']     ?? 8),
            (int) ($s['title_size']        ?? 16),
            (int) ($s['text_size']         ?? 14),
            $btn_bg     !== '' ? esc_attr($btn_bg)     : 'var(--aieo-btn-bg,#0073aa)',
            $btn_text   !== '' ? esc_attr($btn_text)   : 'var(--aieo-btn-fg,#ffffff)',
            $btn_border !== '' ? esc_attr($btn_border) : 'var(--aieo-btn-border,#0073aa)',
            $btn_bw     !== '' ? ((int) $btn_bw . 'px') : 'var(--aieo-btn-border-w,0px)',
            $btn_br     !== '' ? ((int) $btn_br . 'px') : 'var(--aieo-btn-radius,4px)'
        );
        wp_add_inline_style('aieo-waitlist', $vars);

        
        
        
        $iapi_on = method_exists('AIEO_DMM', 'is_iapi_enabled') && AIEO_DMM::is_iapi_enabled('waitlist');
        if ($iapi_on
            && function_exists('wp_enqueue_script_module')
            && defined('AIEO_RUNTIME_DIR')
            && file_exists(AIEO_RUNTIME_DIR . 'assets/js/aieo-waitlist-iapi.js')
        ) {
            wp_register_script_module(
                '@aieo/waitlist-iapi',
                AIEO_RUNTIME_URI . 'assets/js/aieo-waitlist-iapi.js',
                array('@aieo/iapi-bootstrap'),
                (string) filemtime(AIEO_RUNTIME_DIR . 'assets/js/aieo-waitlist-iapi.js')
            );
            wp_enqueue_script_module('@aieo/waitlist-iapi');
        } else {
            wp_enqueue_script('aieo-waitlist', plugins_url($js_rel,  AIEO_FILE), array('jquery'), $js_ver, true);
        }

        $waitlist_data = array(
            'ajaxUrl'    => admin_url('admin-ajax.php'),
            'nonce'      => wp_create_nonce('aieo_waitlist'),
            
            
            'legacyAction' => 'om_ppe_waitlist_subscribe',
            'i18n'       => array(
                'invalidEmail'  => __('Please enter a valid email address.', 'ai-eshop-optimizer'),
                'genericError'  => __('An error occurred. Please try again.', 'ai-eshop-optimizer'),
            ),
        );
        if ($iapi_on) {
            add_action('wp_print_footer_scripts', static function () use ($waitlist_data) {
                echo '<script id="aieo-waitlist-data">'
                    . 'window.aieoWaitlist = ' . wp_json_encode($waitlist_data) . ';'
                    . '</script>' . "\n";
            }, 5);
        } else {
            wp_localize_script('aieo-waitlist', 'aieoWaitlist', $waitlist_data);
        }
    }

    public function render_form_hook() {
        global $product;
        if (!$product || !$this->should_display($product)) { return; }
        
        echo $this->render_form($product);
    }

    public function shortcode_handler($atts = array()) {
        if (!is_singular('product')) { return ''; }
        global $product;
        if (!$product || !$this->should_display($product)) { return ''; }
        return $this->render_form($product);
    }

    private function should_display($product) {
        if (!$product || !method_exists($product, 'is_in_stock')) { return false; }
        if ($product->is_in_stock()) { return false; }
        $settings = $this->get_settings();
        if ($product->get_stock_status() === 'onbackorder') {
            return !empty($settings['display_on_backorders']) && $settings['display_on_backorders'] === 'yes';
        }
        return true;
    }

    private function render_form($product) {
        $settings = $this->get_settings();
        ob_start();
        ?>
        <div class="aieo-waitlist-container" data-aieo-waitlist>
            <div class="aieo-waitlist-overlay">
                <div class="aieo-waitlist-loader">
                    <svg viewBox="0 0 24 24" width="24" height="24" fill="none" xmlns="http://www.w3.org/2000/svg">
                        <path d="M12 19C15.866 19 19 15.866 19 12C19 8.13401 15.866 5 12 5C8.13401 5 5 8.13401 5 12C5 15.866 8.13401 19 12 19ZM12 22C17.5228 22 22 17.5228 22 12C22 6.47715 17.5228 2 12 2C6.47715 2 2 6.47715 2 12C2 17.5228 6.47715 22 12 22Z" fill="currentColor" fill-rule="evenodd" clip-rule="evenodd" opacity="0.2"/>
                        <path d="M2 12C2 6.47715 6.47715 2 12 2V5C8.13401 5 5 8.13401 5 12H2Z" fill="currentColor"/>
                    </svg>
                </div>
            </div>
            <form class="aieo-waitlist-form" method="post">
                <p class="aieo-waitlist-title"><?php echo esc_html($settings['form_title']); ?></p>
                <div class="aieo-waitlist-field">
                    <label for="aieo-waitlist-email-<?php echo (int) $product->get_id(); ?>">
                        <?php echo esc_html($settings['form_email_label']); ?>
                        <abbr class="required" title="required">*</abbr>
                    </label>
                    <input type="email"
                           name="email"
                           id="aieo-waitlist-email-<?php echo (int) $product->get_id(); ?>"
                           value=""
                           autocomplete="email"
                           placeholder="<?php echo esc_attr($settings['form_email_label']); ?>"
                           required>
                </div>
                <input type="hidden" name="product_id" value="<?php echo (int) $product->get_id(); ?>">
                <input type="hidden" name="variation_id" value="0" class="aieo-waitlist-variation-id">
                <button type="submit" class="aieo-waitlist-submit button"><?php echo esc_html($settings['form_button_text']); ?></button>
                <div class="aieo-waitlist-message"></div>
            </form>
        </div>
        <?php
        return ob_get_clean();
    }

    
    
    

    public function ajax_subscribe() {
        
        
        
        $nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : '';
        if (!wp_verify_nonce($nonce, 'aieo_waitlist')
            && !wp_verify_nonce($nonce, 'om-ppe-waitlist')) {
            wp_send_json_error(array('message' => __('Session expired. Reload the page and try again.', 'ai-eshop-optimizer')));
        }

        $email        = isset($_POST['email']) ? sanitize_email(wp_unslash($_POST['email'])) : '';
        $product_id   = isset($_POST['product_id']) ? absint(wp_unslash($_POST['product_id'])) : 0;
        $variation_id = isset($_POST['variation_id']) ? absint(wp_unslash($_POST['variation_id'])) : 0;

        if (!is_email($email)) {
            wp_send_json_error(array('message' => __('Please enter a valid email address.', 'ai-eshop-optimizer')));
        }
        $product = wc_get_product($product_id);
        if (!$product) {
            wp_send_json_error(array('message' => __('Invalid product.', 'ai-eshop-optimizer')));
        }
        if ($this->is_subscribed($email, $product_id, $variation_id)) {
            wp_send_json_error(array('message' => __('You are already subscribed to the stock notification list for this product.', 'ai-eshop-optimizer')));
        }

        global $wpdb;
        $ok = $wpdb->insert(self::get_table_name(), array(
            'email'        => $email,
            'product_id'   => $product_id,
            'variation_id' => $variation_id,
            'status'       => 'subscribed',
            'created_at'   => current_time('mysql', true),
        ), array('%s', '%d', '%d', '%s', '%s'));

        if (!$ok) {
            wp_send_json_error(array('message' => __('Could not save your subscription. Try again later.', 'ai-eshop-optimizer')));
        }
        $settings = $this->get_settings();
        wp_send_json_success(array('message' => $settings['form_success_message']));
    }

    private function is_subscribed($email, $product_id, $variation_id) {
        global $wpdb;
        return (int) $wpdb->get_var($wpdb->prepare(
            "SELECT COUNT(*) FROM " . self::get_table_name() . "
             WHERE email = %s AND product_id = %d AND variation_id = %d AND status = 'subscribed'",
            $email, $product_id, $variation_id
        )) > 0;
    }

    
    
    

    public function on_stock_changed($product_id, $stock_status, $product = null) {
        if ($stock_status !== 'instock') { return; }
        $this->send_notifications((int) $product_id);
    }

    private function send_notifications($product_id) {
        global $wpdb;
        $table = self::get_table_name();
        $subs  = $wpdb->get_results($wpdb->prepare(
            "SELECT * FROM {$table} WHERE product_id = %d AND status = 'subscribed'",
            $product_id
        ));
        if (empty($subs)) { return; }
        $product  = wc_get_product($product_id);
        if (!$product) { return; }
        $settings = $this->get_settings();
        foreach ($subs as $sub) {
            $this->send_email($sub->email, $product, $settings);
            $wpdb->update($table,
                array('status' => 'notified', 'notified_at' => current_time('mysql', true)),
                array('id' => (int) $sub->id),
                array('%s', '%s'),
                array('%d')
            );
        }
    }

    private function send_email($email, $product, $settings) {
        $name = $product->get_name();
        $url  = $product->get_permalink();
        $subject = str_replace('{product_name}', $name, $settings['email_subject']);
        $message = str_replace(
            array('{product_name}', '{product_url}'),
            array($name, $url),
            $settings['email_message']
        );
        
        $message .= "\n\n" . sprintf(__('View product: %s', 'ai-eshop-optimizer'), $url);
        wp_mail($email, $subject, $message, array('Content-Type: text/plain; charset=UTF-8'));
    }
}
