<?php
if (!defined('ABSPATH')) exit;

class DC_WA_Inquiry_By_XD_Admin {

    private $plugin_name;
    private $version;

    public function __construct($plugin_name, $version) {
        $this->plugin_name = $plugin_name;
        $this->version = $version;
        
        // Hook the admin menu and settings
        add_action('admin_menu', array($this, 'add_admin_menu'));
        add_action('admin_init', array($this, 'settings_init'));
    }

    public function enqueue_styles() {
        wp_enqueue_style(
            $this->plugin_name,
            plugin_dir_url(__FILE__) . 'css/style.css',
            array(),
            $this->version,
            'all'
        );
    }

    public function enqueue_scripts() {
        wp_enqueue_script(
            $this->plugin_name . '-admin',
            plugin_dir_url(__FILE__) . 'js/admin.js',
            array('jquery'),
            $this->version,
            true
        );
    }

    public function add_admin_menu() {
        $hook = add_submenu_page(
            'woocommerce',
            __('WhatsApp Inquiry Settings', 'direct-chat-inquiry-by-xd'),
            __('WhatsApp Inquiry', 'direct-chat-inquiry-by-xd'),
            'manage_options',
            'dc-wa-inquiry-by-xd',
            array($this, 'display_settings_page')
        );
        
        // Load assets only on our plugin page
        add_action('admin_print_styles-' . $hook, array($this, 'enqueue_styles'));
        add_action('admin_print_scripts-' . $hook, array($this, 'enqueue_scripts'));
    }

    public function settings_init() {
        // Register settings
        // Register settings with sanitization callbacks
        register_setting(
            'dc_wa_inquiry_by_xd', 
            'dc_wa_inquiry_by_xd_whatsapp_number',
            array(
                'sanitize_callback' => array($this, 'sanitize_whatsapp_number')
            )
        );
        register_setting(
            'dc_wa_inquiry_by_xd', 
            'dc_wa_inquiry_by_xd_button_text',
            array(
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        register_setting(
            'dc_wa_inquiry_by_xd', 
            'dc_wa_inquiry_by_xd_button_position',
            array(
                'sanitize_callback' => array($this, 'sanitize_button_position')
            )
        );

        // Add settings section
        add_settings_section(
            'dc_wa_inquiry_by_xd_section',
            __('WhatsApp Button Settings', 'direct-chat-inquiry-by-xd'),
            array($this, 'settings_section_callback'),
            'dc-wa-inquiry-by-xd'
        );

        // Add settings fields
        add_settings_field(
            'dc_wa_inquiry_by_xd_whatsapp_number',
            __('WhatsApp Number', 'direct-chat-inquiry-by-xd'),
            array($this, 'whatsapp_number_field_callback'),
            'dc-wa-inquiry-by-xd',
            'dc_wa_inquiry_by_xd_section'
        );

        add_settings_field(
            'dc_wa_inquiry_by_xd_button_text',
            __('Button Text', 'direct-chat-inquiry-by-xd'),
            array($this, 'button_text_field_callback'),
            'dc-wa-inquiry-by-xd',
            'dc_wa_inquiry_by_xd_section'
        );

        add_settings_field(
            'dc_wa_inquiry_by_xd_button_position',
            __('Button Position', 'direct-chat-inquiry-by-xd'),
            array($this, 'button_position_field_callback'),
            'dc-wa-inquiry-by-xd',
            'dc_wa_inquiry_by_xd_section'
        );
    }

    public function settings_section_callback() {
        echo '<p>' . esc_html__('Configure the WhatsApp inquiry button settings below.', 'direct-chat-inquiry-by-xd') . '</p>';
    }

    public function whatsapp_number_field_callback() {
        $value = get_option('dc_wa_inquiry_by_xd_whatsapp_number', '');
        echo '<input type="text" name="dc_wa_inquiry_by_xd_whatsapp_number" value="' . esc_attr($value) . '" class="regular-text">';
        echo '<p class="description">' . esc_html__('Enter WhatsApp number with country code (e.g., 923001234567)', 'direct-chat-inquiry-by-xd') . '</p>';
    }

    public function button_text_field_callback() {
        $value = get_option('dc_wa_inquiry_by_xd_button_text', __('Inquire on WhatsApp', 'direct-chat-inquiry-by-xd'));
        echo '<input type="text" name="dc_wa_inquiry_by_xd_button_text" value="' . esc_attr($value) . '" class="regular-text">';
    }

    public function button_position_field_callback() {
        $value = get_option('dc_wa_inquiry_by_xd_button_position', 'after_add_to_cart');
        ?>
        <select name="dc_wa_inquiry_by_xd_button_position" class="regular-text">
            <option value="after_add_to_cart" <?php selected($value, 'after_add_to_cart'); ?>>
                <?php esc_html_e('After Add to Cart', 'direct-chat-inquiry-by-xd'); ?>
            </option>
            <option value="before_add_to_cart" <?php selected($value, 'before_add_to_cart'); ?>>
                <?php esc_html_e('Before Add to Cart', 'direct-chat-inquiry-by-xd'); ?>
            </option>
            <option value="after_summary" <?php selected($value, 'after_summary'); ?>>
                <?php esc_html_e('After Product Summary', 'direct-chat-inquiry-by-xd'); ?>
            </option>
        </select>
        <?php
    }

    /**
     * Sanitize WhatsApp number
     */
    public function sanitize_whatsapp_number($input) {
        // Remove all non-digit characters
        $sanitized = preg_replace('/[^0-9]/', '', $input);
        return $sanitized;
    }

    /**
     * Sanitize button position
     */
    public function sanitize_button_position($input) {
        $valid_positions = array('after_add_to_cart', 'before_add_to_cart', 'after_summary');
        return in_array($input, $valid_positions) ? $input : 'after_add_to_cart';
    }
    
    public function display_settings_page() {
        if (!current_user_can('manage_options')) {
            wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'direct-chat-inquiry-by-xd'));
        }
        ?>
        <div class="wrap dc-xd-admin-wrap">
            <div class="dc-xd-admin-header">
                <h1><?php esc_html_e('WhatsApp Inquiry Settings', 'direct-chat-inquiry-by-xd'); ?></h1>
            </div>
            
            <form method="post" action="options.php">
                <?php
                settings_fields('dc_wa_inquiry_by_xd');
                do_settings_sections('dc-wa-inquiry-by-xd');
                submit_button();
                ?>
            </form>
            
            <div class="dc-xd-shortcode-card">
                <h3><?php esc_html_e('Shortcode Usage', 'direct-chat-inquiry-by-xd'); ?></h3>
                <ul class="dc-xd-shortcode-list">
                    <li><code>[dc_whatsapp_inquiry]</code> - <?php esc_html_e('Basic button', 'direct-chat-inquiry-by-xd'); ?></li>
                    <li><code>[dc_whatsapp_inquiry text="Custom Text"]</code> - <?php esc_html_e('Custom button text', 'direct-chat-inquiry-by-xd'); ?></li>
                    <li><code>[dc_inquiry_by_xd]</code> - <em><?php esc_html_e('Deprecated, use dc_whatsapp_inquiry instead', 'direct-chat-inquiry-by-xd'); ?></em></li>
                </ul>
                <p><?php esc_html_e('Note: Shortcode works only on product pages', 'direct-chat-inquiry-by-xd'); ?></p>
            </div>
        </div>
        <?php
    }
}