<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

/**
 * Callback wrapper for global settings validation
 */
function doosearchas_search_global_validate_callback($input) {
    return DooSearchas_Search_Settings::doosearchas_search_validate_callback($input);
}

/**
 * Class responsible for managing all plugin settings pages and options
 */
class DooSearchas_Search_Settings{
    // Static variables for storing plugin options
    public static $mainOptions;
    public static $styleOptions;
    public static $upgradeOptions;
    public static $filterOptions;
    public static $productOptions;
    public static $additionalOptions;

    /**
     * Constructor: Loads settings and hooks into admin_init
     */
    public function __construct(){
        // Load plugin options from the database
        self::$mainOptions = get_option('doosearchas_search_main_options');
        self::$styleOptions = get_option('doosearchas_search_style_options');
        self::$upgradeOptions = get_option('doosearchas_search_upgrade_options');
        self::$filterOptions = get_option('doosearchas_search_filter_options');
        self::$productOptions = get_option('doosearchas_search_product_options');
        self::$additionalOptions = get_option('doosearchas_search_additional_options');
        $this->include_settings_views();
        add_action('admin_init', array($this,'admin_init'));
    }

    /**
     * Includes PHP files that generate admin settings UI
     */
    private function include_settings_views() {
        require_once(DOOSEARCHAS_SEARCH_PATH . 'views/settings-options/main-options.php');
        require_once(DOOSEARCHAS_SEARCH_PATH . 'views/settings-options/style-options.php');
        require_once(DOOSEARCHAS_SEARCH_PATH . 'views/settings-options/upgrade-options.php');
        require_once(DOOSEARCHAS_SEARCH_PATH . 'views/settings-options/filter-options.php');
        require_once(DOOSEARCHAS_SEARCH_PATH . 'views/settings-options/product-options.php');
        require_once(DOOSEARCHAS_SEARCH_PATH . 'views/settings-options/additional-options.php');
    }

    /**
     * Registers all plugin settings groups, sections, and fields
     */
    public function admin_init(){
        // Register settings groups
        register_setting('doosearchas_search_main_group', 'doosearchas_search_main_options', 'doosearchas_search_global_validate_callback');
        register_setting('doosearchas_search_style_group', 'doosearchas_search_style_options', 'doosearchas_search_global_validate_callback');
        register_setting('doosearchas_search_upgrade_group', 'doosearchas_search_upgrade_options', 'doosearchas_search_global_validate_callback');
        register_setting('doosearchas_search_filter_group', 'doosearchas_search_filter_options', 'doosearchas_search_global_validate_callback');
        register_setting('doosearchas_search_product_group', 'doosearchas_search_product_options', 'doosearchas_search_global_validate_callback');
        register_setting('doosearchas_search_additional_group', 'doosearchas_search_additional_options', 'doosearchas_search_global_validate_callback'); 
        
        // Settings sections (grouping for fields)
        add_settings_section('doosearchas_search_main_section_id', esc_html__('How does it works?','doosearch-ajax-search-for-woo'), null, 'doosearchas_search_main_page');
        add_settings_section('doosearchas_search_style_section_id', null, null, 'doosearchas_search_style_page');
        add_settings_section('doosearchas_search_upgrade_section_id', null, null, 'doosearchas_search_upgrade_page');
        add_settings_section('doosearchas_search_filter_section_id', null, null, 'doosearchas_search_filter_page');
        add_settings_section('doosearchas_search_product_section_id', null, null, 'doosearchas_search_product_page');
        add_settings_section('doosearchas_search_additional_section_id', esc_html__('Other Plugin Options','doosearch-ajax-search-for-woo'), null, 'doosearchas_search_additional_page');

        // Register settings fields with appropriate callbacks
        add_settings_field('doosearchas_search_cart_display_field_id', esc_html__('View Cart','doosearch-ajax-search-for-woo'), array($this,'doosearchas_search_cart_display_callback'), 'doosearchas_search_additional_page', 'doosearchas_search_additional_section_id');
        add_settings_field('doosearchas_search_shortcode_field_id', esc_html__('shortcode','doosearch-ajax-search-for-woo'), array($this,'doosearchas_search_shortcode_callback'), 'doosearchas_search_main_page', 'doosearchas_search_main_section_id');
        add_settings_field('doosearchas_search_rebuild_index_field_id', esc_html__('Indexing Options', 'doosearch-ajax-search-for-woo'), array($this, 'doosearchas_search_rebuild_index_callback'), 'doosearchas_search_main_page', 'doosearchas_search_main_section_id');  
        add_settings_field('doosearchas_search_search_bar_style_field_id', null, array($this,'doosearchas_search_search_bar_style_callback'), 'doosearchas_search_style_page', 'doosearchas_search_style_section_id');
        add_settings_field('doosearchas_search_upgrade_btn_field_id', null, array($this,'doosearchas_search_upgrade_btn_callback'), 'doosearchas_search_upgrade_page', 'doosearchas_search_upgrade_section_id');
        add_settings_field('doosearchas_search_license_field_id', esc_html__('License','doosearch-ajax-search-for-woo'), array($this,'doosearchas_search_license_callback'), 'doosearchas_search_upgrade_page', 'doosearchas_search_upgrade_section_id');
        add_settings_field('doosearchas_search_popup_ui_field_id', esc_html__('Popup UI','doosearch-ajax-search-for-woo'), array($this,'doosearchas_search_popup_ui_callback'), 'doosearchas_search_additional_page', 'doosearchas_search_additional_section_id');
        $doosearchas_get_options = get_option('doosearchas_search_additional_options');
        $popup_ui_type = isset($doosearchas_get_options['doosearchas_search_popup_ui_db']) ? $doosearchas_get_options['doosearchas_search_popup_ui_db'] : 'recommended';
        if($popup_ui_type === 'recommended'){
            add_settings_field('doosearchas_search_recommended_products_priority_field_id', esc_html__('Recommended Products Priority ID','doosearch-ajax-search-for-woo'), array($this,'doosearchas_search_recommended_products_priority_callback'), 'doosearchas_search_additional_page', 'doosearchas_search_additional_section_id');
        }
        add_settings_field('doosearchas_search_suggestion_list_limit_field_id', esc_html__('Limit Suggestion List','doosearch-ajax-search-for-woo'), array($this,'doosearchas_search_suggestion_list_limit_callback'), 'doosearchas_search_additional_page', 'doosearchas_search_additional_section_id');
        add_settings_field('doosearchas_search_suggestions_priority_field_id', esc_html__('Suggestions Priority','doosearch-ajax-search-for-woo'), array($this,'doosearchas_search_suggestions_priority_callback'), 'doosearchas_search_additional_page', 'doosearchas_search_additional_section_id');
        add_settings_field('doosearchas_search_suggestion_stopwords_field_id', esc_html__('Suggestions StopWords','doosearch-ajax-search-for-woo'), array($this,'doosearchas_search_suggestion_stopwords_callback'), 'doosearchas_search_additional_page', 'doosearchas_search_additional_section_id');
        add_settings_field('doosearchas_search_s_f_field_id', esc_html__('Search Fields','doosearch-ajax-search-for-woo'), array($this,'doosearchas_search_s_field_callback'), 'doosearchas_search_filter_page', 'doosearchas_search_filter_section_id');
        add_settings_field('doosearchas_search_brand_filter_exclude_field_id', esc_html__('Exclude Brand Filter','doosearch-ajax-search-for-woo'), array($this,'doosearchas_search_brand_filter_exclude_callback'), 'doosearchas_search_filter_page', 'doosearchas_search_filter_section_id');
        add_settings_field('doosearchas_search_category_filter_select_field_id', esc_html__('Select Category Filter','doosearch-ajax-search-for-woo'), array($this,'doosearchas_search_category_filter_select_callback'), 'doosearchas_search_filter_page', 'doosearchas_search_filter_section_id');
        add_settings_field('doosearchas_search_category_filter_exclude_field_id', esc_html__('Exclude Category Filter','doosearch-ajax-search-for-woo'), array($this,'doosearchas_search_category_filter_exclude_callback'), 'doosearchas_search_filter_page', 'doosearchas_search_filter_section_id');
        add_settings_field('doosearchas_search_attribute_filter_exclude_field_id', esc_html__('Exclude Attribute Filter','doosearch-ajax-search-for-woo'), array($this,'doosearchas_search_attribute_filter_exclude_callback'), 'doosearchas_search_filter_page', 'doosearchas_search_filter_section_id');
        add_settings_field('doosearchas_search_display_description_field_id', esc_html__('Display Product Description','doosearch-ajax-search-for-woo'), array($this,'doosearchas_search_display_description_callback'), 'doosearchas_search_product_page', 'doosearchas_search_product_section_id');
        $doosearchas_product_options = get_option('doosearchas_search_product_options');
        $is_show_description = isset($doosearchas_product_options['doosearchas_search_disply_description_db']) ? $doosearchas_product_options['doosearchas_search_disply_description_db'] : 'description';
        if($is_show_description != 'none'){
            add_settings_field('doosearchas_search_description_length_field_id', esc_html__('Description Length','doosearch-ajax-search-for-woo'), array($this,'doosearchas_search_description_length_callback'), 'doosearchas_search_product_page', 'doosearchas_search_product_section_id');
        }
        add_settings_field('doosearchas_search_features_list_display_field_id', esc_html__('Display Features List','doosearch-ajax-search-for-woo'), array($this,'doosearchas_search_features_list_display_callback'), 'doosearchas_search_product_page', 'doosearchas_search_product_section_id',array('label_for' => 'doosearchas_search_features_list_display_field_id'));
        $is_features_list_display_checked = isset($doosearchas_product_options['doosearchas_search_features_list_display_db']) && $doosearchas_product_options['doosearchas_search_features_list_display_db'] == 1;
        if($is_features_list_display_checked){
            add_settings_field('doosearchas_search_features_list_limit_field_id', esc_html__('Limit Features List','doosearch-ajax-search-for-woo'), array($this,'doosearchas_search_features_list_limit_callback'), 'doosearchas_search_product_page', 'doosearchas_search_product_section_id');
            add_settings_field('doosearchas_search_features_list_exclude_field_id', esc_html__('Exclude Features List','doosearch-ajax-search-for-woo'), array($this,'doosearchas_search_features_list_exclude_callback'), 'doosearchas_search_product_page', 'doosearchas_search_product_section_id');
            add_settings_field('doosearchas_search_features_list_prioritized_field_id', esc_html__('Prioritized Features List','doosearch-ajax-search-for-woo'), array($this,'doosearchas_search_features_list_prioritized_callback'), 'doosearchas_search_product_page', 'doosearchas_search_product_section_id');
        }
    }

    // ================== Callback Functions for Field Rendering ==================
    public function doosearchas_search_cart_display_callback(){
        doosearchas_search_cart_display_callback_content(self::$additionalOptions);
    }
    public function doosearchas_search_shortcode_callback(){
        doosearchas_search_shortcode_callback_content();
    }
    public function doosearchas_search_rebuild_index_callback() {
        doosearchas_search_rebuild_index_callback_content(self::$mainOptions);
    }
    public function doosearchas_search_search_bar_style_callback(){
        doosearchas_search_search_bar_style_callback_content(self::$styleOptions);
    }
    public function doosearchas_search_upgrade_btn_callback(){
        doosearchas_search_upgrade_btn_callback_content();
    }
    public function doosearchas_search_license_callback(){
        doosearchas_search_license_callback_content(self::$upgradeOptions);
    }
    public function doosearchas_search_popup_ui_callback(){
        doosearchas_search_popup_ui_callback_content();
    }
    public function doosearchas_search_recommended_products_priority_callback(){
        doosearchas_search_recommended_products_priority_callback_content(self::$additionalOptions);
    }
    public function doosearchas_search_suggestion_list_limit_callback(){
        doosearchas_search_suggestion_list_limit_callback_content(self::$additionalOptions);
    }
    public function doosearchas_search_suggestions_priority_callback(){
        doosearchas_search_suggestions_priority_callback_content();
    }
    public function doosearchas_search_suggestion_stopwords_callback(){
        doosearchas_search_suggestion_stopwords_callback_content();
    }
    public function doosearchas_search_s_field_callback(){
        doosearchas_search_s_field_callback_content(self::$filterOptions);
    }
    public function doosearchas_search_brand_filter_exclude_callback(){
        doosearchas_search_brand_filter_exclude_callback_content(self::$filterOptions);
    }
    public function doosearchas_search_category_filter_select_callback(){
        doosearchas_search_category_filter_select_callback_content(self::$filterOptions);
    }
    public function doosearchas_search_category_filter_exclude_callback(){
        doosearchas_search_category_filter_exclude_callback_content(self::$filterOptions);
    }
    public function doosearchas_search_attribute_filter_exclude_callback(){
        doosearchas_search_attribute_filter_exclude_callback_content(self::$filterOptions);
    }
    public function doosearchas_search_display_description_callback(){
        doosearchas_search_display_description_callback_content(self::$productOptions);
    }
    public function doosearchas_search_description_length_callback(){
        doosearchas_search_description_length_callback_content(self::$productOptions);
    }
    public function doosearchas_search_features_list_display_callback($arg){
        doosearchas_search_features_list_display_callback_content(self::$productOptions, $arg);
    }
    public function doosearchas_search_features_list_limit_callback(){
        doosearchas_search_features_list_limit_callback_content(self::$productOptions);
    }
    public function doosearchas_search_features_list_exclude_callback(){
        doosearchas_search_features_list_exclude_callback_content(self::$productOptions);
    }
    public function doosearchas_search_features_list_prioritized_callback(){
        doosearchas_search_features_list_prioritized_callback_content(self::$productOptions);
    }

    /**
     * Validation callback for all settings fields
     * Sanitizes input before saving to database
     */
    public static function doosearchas_search_validate_callback($input){
        $new_input =array();
        foreach($input as $key => $value){
            switch($key){
                case 'license_key_db':
                    if( empty($value)){
                        add_settings_error('doosearchas_search_main_options', 'doosearchas_search_message_class', esc_html__('Please enter a license key.','doosearch-ajax-search-for-woo'), 'error');
                        $value = '';
                    }
                    $new_input[$key] = sanitize_text_field($value);
                break;
                case  'ai_search_url_id':   
                    $new_input[$key] = esc_url_raw($value);
                break;
                case 'ai_search_int_id':
                    $new_input[$key] = absint($value);  
                break;
                default:
                    $new_input[$key] = sanitize_text_field($value);
                break;    
            }  
        }
        return $new_input;
    }
}