<?php

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

/*
* Watermark settings
*/
$watermark_options = [
    'w_text_rotation'         => ['type' => 'integer', 'sanitize_callback' => 'absint'],
    'w_text_font_value'       => ['type' => 'integer', 'sanitize_callback' => 'absint'],
    'w_text_font_weight'      => ['type' => 'integer', 'sanitize_callback' => 'absint'],
    // URL
    'w_image_url'             => ['type' => 'string', 'sanitize_callback' => 'esc_url_raw'],
    'w_text_color'            => ['type' => 'string', 'sanitize_callback' => 'sanitize_hex_color'],
    'w_img_opacity_value'     => ['type' => 'number', 'sanitize_callback' => 'floatval'],
    'w_developer_tools_check' => ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field'],
    'w_right_click_check'     => ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field'],
    'w_text_img_allow'        => ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field'],
    'w_cv_2allow'             => ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field'],
    'w_cv_allow'              => ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field'],
    'watermark_text'          => ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field'],
    'w_text_font_family'      => ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field'],
    'w_text_font_style'       => ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field'],
];

foreach ( $watermark_options as $option => $args ) {
    register_setting( 'bwdcv_watermark_group', $option, $args );
}

/*
* Style settings
* attern-based sanitizer detection:
* _color | _bgcolor  → sanitize_hex_color _font_family | _font_weight | _font_style → sanitize_text_field
* everything else (font_value, width, radius, padding, margin) → sanitize_text_field
  because these store CSS strings like "16px", "10px 20px", "1em" — NOT pure integers
*/

$optionStyles = [
    'cv_buy_now_text_color',
    'cv_buy_now_text_h_color',
    'cv_buy_now_text_bgcolor',
    'cv_buy_now_text_h_bgcolor',
    'cv_buy_now_text_font_value',
    'cv_buy_now_text_font_family',
    'cv_buy_now_text_font_weight',
    'cv_buy_now_text_font_style',
    'cv_buy_now_border_width_value',
    'cv_buy_now_border_color',
    'cv_buy_now_border_radius_value',
    'cv_buy_now_padding',
    'cv_buy_now_margin',
    'bwdcv_reg_btn_text_color',
    'bwdcv_reg_btn_text_h_color',
    'bwdcv_reg_btn_text_bgcolor',
    'bwdcv_reg_btn_text_h_bgcolor',
    'bwdcv_reg_btn_text_font_value',
    'bwdcv_reg_btn_text_font_family',
    'bwdcv_reg_btn_text_font_weight',
    'bwdcv_reg_btn_text_font_style',
    'bwdcv_reg_btn_border_width_value',
    'bwdcv_reg_btn_border_color',
    'bwdcv_reg_btn_border_radius_value',
    'bwdcv_reg_btn_padding',
    'bwdcv_reg_btn_margin',
    'cv_download_text_color',
    'cv_download_text_h_color',
    'cv_download_text_bgcolor',
    'cv_download_text_h_bgcolor',
    'cv_download_text_font_value',
    'cv_download_text_font_family',
    'cv_download_text_font_weight',
    'cv_download_text_font_style',
    'cv_download_border_width_value',
    'cv_download_border_color',
    'cv_download_border_radius_value',
    'cv_download_padding',
    'cv_download_margin',
    'cv_download_info_title_text_color',
    'cv_download_info_title_text_h_color',
    'cv_download_info_title_text_bgcolor',
    'cv_download_info_title_text_h_bgcolor',
    'cv_download_info_title_text_font_value',
    'cv_download_info_title_text_font_family',
    'cv_download_info_title_text_font_weight',
    'cv_download_info_title_text_font_style',
    'cv_download_info_title_border_width_value',
    'cv_download_info_title_border_color',
    'cv_download_info_title_border_radius_value',
    'cv_download_info_title_padding',
    'cv_download_info_title_margin',
    'cv_anew_text_color',
    'cv_anew_text_h_color',
    'cv_anew_text_bgcolor',
    'cv_anew_text_h_bgcolor',
    'cv_anew_text_font_value',
    'cv_anew_text_font_family',
    'cv_anew_text_font_weight',
    'cv_anew_text_font_style',
    'cv_anew_border_width_value',
    'cv_anew_border_color',
    'cv_anew_border_radius_value',
    'cv_anew_padding',
    'cv_anew_margin',
    'cv_sub_page_product_img_border_width_value',
    'cv_sub_page_product_img_border_color',
    'cv_sub_page_product_img_border_radius_value',
    'cv_sub_page_product_img_padding',
    'cv_sub_page_product_img_margin',
    'cv_sub_page_title_color',
    'cv_sub_page_title_h_color',
    'cv_sub_page_title_bgcolor',
    'cv_sub_page_title_h_bgcolor',
    'cv_sub_page_title_font_value',
    'cv_sub_page_title_font_family',
    'cv_sub_page_title_font_weight',
    'cv_sub_page_title_font_style',
    'cv_sub_page_title_border_width',
    'cv_sub_page_title_border_color',
    'cv_sub_page_title_border_radius_value',
    'cv_sub_page_title_padding',
    'cv_sub_page_title_margin',
    'cv_sub_page_dsc_color',
    'cv_sub_page_dsc_h_color',
    'cv_sub_page_dsc_bgcolor',
    'cv_sub_page_dsc_h_bgcolor',
    'cv_sub_page_dsc_font_value',
    'cv_sub_page_dsc_font_family',
    'cv_sub_page_dsc_font_weight',
    'cv_sub_page_dsc_font_style',
    'cv_sub_page_dsc_border_width_value',
    'cv_sub_page_dsc_border_color',
    'cv_sub_page_dsc_border_radius_value',
    'cv_sub_page_dsc_padding',
    'cv_sub_page_dsc_margin',
    'cv_sub_page_mprice_color',
    'cv_sub_page_sprice_color',
    'cv_sub_page_mprice_h_color',
    'cv_sub_page_sprice_h_color',
    'cv_sub_page_mprice_bgcolor',
    'cv_sub_page_sprice_bgcolor',
    'cv_sub_page_mprice_h_bgcolor',
    'cv_sub_page_sprice_h_bgcolor',
    'cv_sub_page_mprice_font_value',
    'cv_sub_page_mprice_font_family',
    'cv_sub_page_mprice_font_weight',
    'cv_sub_page_mprice_font_style',
    'cv_sub_page_mprice_border_width_value',
    'cv_sub_page_mprice_border_color',
    'cv_sub_page_mprice_border_radius_value',
    'cv_sub_page_mprice_padding',
    'cv_sub_page_mprice_margin',
    'cv_sub_page_plan_name_color',
    'cv_sub_page_plan_name_h_color',
    'cv_sub_page_plan_name_bgcolor',
    'cv_sub_page_plan_name_h_bgcolor',
    'cv_sub_page_plan_name_font_value',
    'cv_sub_page_plan_name_font_family',
    'cv_sub_page_plan_name_font_weight',
    'cv_sub_page_plan_name_font_style',
    'cv_sub_page_plan_name_border_width_value',
    'cv_sub_page_plan_name_border_color',
    'cv_sub_page_plan_name_border_radius_value',
    'cv_sub_page_plan_name_padding',
    'cv_sub_page_plan_name_margin',
    'bwdcv_reg_tab_color',
    'bwdcv_reg_tab_h_color',
    'bwdcv_reg_tab_bgcolor',
    'bwdcv_reg_tab_h_bgcolor',
    'bwdcv_reg_tab_font_value',
    'bwdcv_reg_tab_font_family',
    'bwdcv_reg_tab_font_weight',
    'bwdcv_reg_tab_font_style',
    'bwdcv_reg_tab_border_width_value',
    'bwdcv_reg_tab_border_color',
    'bwdcv_reg_tab_border_radius_value',
    'bwdcv_reg_tab_padding',
    'bwdcv_reg_tab_margin',
    'cv_sub_page_sub_name_color',
    'cv_sub_page_sub_name_h_color',
    'cv_sub_page_sub_name_bgcolor',
    'cv_sub_page_sub_name_h_bgcolor',
    'cv_sub_page_sub_name_font_value',
    'cv_sub_page_sub_name_font_family',
    'cv_sub_page_sub_name_font_weight',
    'cv_sub_page_sub_name_font_style',
    'cv_sub_page_sub_name_border_width_value',
    'cv_sub_page_sub_name_border_color',
    'cv_sub_page_sub_name_border_radius_value',
    'cv_sub_page_sub_name_padding',
    'cv_sub_page_sub_name_margin',
    'bwdcv_reg_label_color',
    'bwdcv_reg_label_h_color',
    'bwdcv_reg_label_bgcolor',
    'bwdcv_reg_label_h_bgcolor',
    'bwdcv_reg_label_font_value',
    'bwdcv_reg_label_font_family',
    'bwdcv_reg_label_font_weight',
    'bwdcv_reg_label_font_style',
    'bwdcv_reg_label_border_width_value',
    'bwdcv_reg_label_border_color',
    'bwdcv_reg_label_border_radius_value',
    'bwdcv_reg_label_padding',
    'bwdcv_reg_label_margin',
    'cv_sub_page_product_wrap_bgcolor',
    'cv_sub_page_product_wrap_h_bgcolor',
    'cv_sub_page_product_wrap_border_width_value',
    'cv_sub_page_product_wrap_border_color',
    'cv_sub_page_product_wrap_border_radius_value',
    'cv_sub_page_product_wrap_padding',
    'cv_sub_page_product_wrap_margin',
    'bwdcv_reg_form_wrap_bgcolor',
    'bwdcv_reg_form_wrap_h_bgcolor',
    'bwdcv_reg_form_wrap_border_width_value',
    'bwdcv_reg_form_wrap_border_color',
    'bwdcv_reg_form_wrap_border_radius_value',
    'bwdcv_reg_form_wrap_padding',
    'bwdcv_reg_form_wrap_margin',
    'cv_dash_tab_color',
    'cv_dash_tab_h_color',
    'cv_dash_tab_bgcolor',
    'cv_dash_tab_h_bgcolor',
    'cv_dash_tab_font_value',
    'cv_dash_tab_font_family',
    'cv_dash_tab_font_weight',
    'cv_dash_tab_font_style',
    'cv_dash_tab_border_width_value',
    'cv_dash_tab_border_color',
    'cv_dash_tab_border_radius_value',
    'cv_dash_tab_padding',
    'cv_dash_tab_margin',
    'cv_dash_tab_content_color',
    'cv_dash_tab_content_h_color',
    'cv_dash_tab_content_bgcolor',
    'cv_dash_tab_content_h_bgcolor',
    'cv_dash_tab_content_font_value',
    'cv_dash_tab_content_font_family',
    'cv_dash_tab_content_font_weight',
    'cv_dash_tab_content_font_style',
    'cv_dash_tab_content_border_width_value',
    'cv_dash_tab_content_border_color',
    'cv_dash_tab_content_border_radius_value',
    'cv_dash_tab_content_padding',
    'cv_dash_tab_content_margin',
];

foreach ( $optionStyles as $optionStyle ) {
    if ( preg_match( '/(_color|_bgcolor)$/', $optionStyle ) ) {
        $sanitize_callback = 'sanitize_hex_color';
        $type = 'string';
    } elseif ( preg_match( '/(_font_value|_font_weight|_width_value|_radius_value)$/', $optionStyle ) ) {
        $sanitize_callback = 'absint'; 
        $type = 'integer';
    } else {
        $sanitize_callback = 'sanitize_text_field';
        $type = 'string';
    }

    register_setting( 'bwdcv_style_settings_tab', $optionStyle, [
        'type'              => $type,
        'sanitize_callback' => $sanitize_callback,
    ] );
}

/*
* API settings
*/

$optionAPIs = [
    'cv_m_api_key_text'         => 'sanitize_text_field',
    'cv_m_audience_bwd'         => 'sanitize_text_field',
    'cv_wemail_api_key_text'    => 'sanitize_text_field',
    'cv_wemail_list_number'     => 'sanitize_text_field',
    'cv_site_key_text'          => 'sanitize_text_field',
    'cv_cloudflare_secret_key'  => 'sanitize_text_field',
    'cv_m_tag_bwd'              => 'sanitize_text_field',
    'cv_openai_api_key_text'    => 'sanitize_text_field',
    'cv_google_client_id'       => 'sanitize_text_field',
    'cv_google_client_secret'   => 'sanitize_text_field',
    'cv_facebook_app_id'        => 'sanitize_text_field',
    'cv_facebook_app_secret'    => 'sanitize_text_field',
    'cv_linkedin_client_id'     => 'sanitize_text_field',
    'cv_linkedin_client_secret' => 'sanitize_text_field',
    'bwdcv_user_text'           => 'sanitize_text_field',
];

foreach ( $optionAPIs as $optionAPI => $sanitize_callback ) {
    register_setting( 'bwdcv_api_settings_tab', $optionAPI, [
        'type'              => 'string',
        'sanitize_callback' => $sanitize_callback,
    ] );
}

/*
* Basic / general settings
*/
$basic_toggle_options = [
    'cv_order_status_change', 
    'cv_social_share',       
    'cv_cover_letter_allow',  
];

foreach ( $basic_toggle_options as $option ) {

    // Determine type
    if ( $option === 'cv_order_status_change' ) {
        $type = 'integer';
        $sanitize_callback = function( $value ) {
            return absint( $value ); 
        };
    } else {
        $type = 'string';
        $sanitize_callback = function( $value ) {
            $value = sanitize_text_field( $value );      
            $allowed = [ '', 'on' ];                     
            if ( ! in_array( $value, $allowed, true ) ) {
                $value = '';
            }
            return $value;
        };
    }

    register_setting( 'bwdcv_settings_tab', $option, [
        'type'              => $type,
        'sanitize_callback' => $sanitize_callback,
    ] );
}

// Plain text label/button options
$basic_text_options = [
    'cv_buy_now_text',
    'cv_download_bwd',
    'cv_download_info_title_bwd',
    'cv_settings_bwd',
    'cv_edit_bwd',
    'cv_anew_bwd',
    'cv_plan_name_bwd',
    'cv_per_download_bwd',
    'cv_billing_period_bwd',
];
foreach ( $basic_text_options as $option ) {
    register_setting( 'bwdcv_settings_tab', $option, [
        'type'              => 'string',
        'sanitize_callback' => 'sanitize_text_field',
    ] );
}

// Style toggle options — cv_allow_style1 through cv_allow_style28

register_setting( 'bwdcv_settings_tab', 'bwdcv_preview_options', [
    'type' => 'array', // Option is an array
    'sanitize_callback' => function( $options ) {
        if ( ! is_array( $options ) ) {
            $options = [];
        }

        for ( $i = 1; $i <= 28; $i++ ) {
            $key = 'cv_allow_style' . $i;
            $options[$key] = isset( $options[$key] ) ? absint( $options[$key] ) : 0;
        }

        return $options;
    },
]);