<?php
/**
 * Customizer: Color Settings Section
 *
 * @package Paws_And_Care
 * @since 3.0.2
 */

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

// Define color defaults as a constant for reuse (matches theme's style.css :root values)
define('PAWS_AND_CARE_COLOR_DEFAULTS', array(
    'paws_and_care_theme_color' => '#ff8c42',           // Main theme color
    'paws_and_care_enable_gradients' => true,           // Enable gradient backgrounds
    'paws_and_care_gradient_end_color' => '#ffa726',    // Gradient end color
    'paws_and_care_secondary_color' => '#4a90e2',       // Secondary color
    'paws_and_care_body_text_color' => '#222a33',       // Body text color
    'paws_and_care_heading_color' => '#222a33',         // Heading color
    'paws_and_care_link_color' => '#ff8a4c',            // Link color
    'paws_and_care_link_hover_color' => '#ff6b35',      // Link hover color
    'paws_and_care_body_bg_color' => '#f8f9fa',         // Body background
    'paws_and_care_content_bg_color' => '#ffffff',      // Content background
    'paws_and_care_btn_bg_color' => '#ff8c42',          // Button background
    'paws_and_care_btn_text_color' => '#ffffff',        // Button text
    'paws_and_care_btn_hover_bg_color' => '#ff6b35',    // Button hover background
    'paws_and_care_btn_hover_text_color' => '#ffffff',  // Button hover text
));

/**
 * Register Color Settings Section
 *
 * @param WP_Customize_Manager $wp_customize Customizer object.
 */
function paws_and_care_customizer_colors($wp_customize)
{
    $defaults = PAWS_AND_CARE_COLOR_DEFAULTS;

    // ========================================
    // Colors Section (inside Theme Settings panel)
    // ========================================
    $wp_customize->add_section('paws_and_care_colors', array(
        'title' => __('Colors', 'paws-and-care'),
        'description' => __('Customize theme colors. Click "Reset Colors to Defaults" button at the bottom to restore default colors.', 'paws-and-care'),
        'panel' => 'paws_and_care_theme_settings',
        'priority' => 5,
    ));

    // ========================================
    // GLOBAL COLORS
    // ========================================

    $wp_customize->add_setting('paws_and_care_global_colors_heading', array(
        'sanitize_callback' => 'sanitize_text_field',
    ));
    $wp_customize->add_control(new Paws_And_Care_Customize_Heading_Control($wp_customize, 'paws_and_care_global_colors_heading', array(
        'label' => __('Global Colors', 'paws-and-care'),
        'section' => 'paws_and_care_colors',
    )));

    // 1. Theme Color (was Primary Color)
    $wp_customize->add_setting('paws_and_care_theme_color', array(
        'default' => $defaults['paws_and_care_theme_color'],
        'sanitize_callback' => 'paws_and_care_sanitize_rgba_color',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control(new Paws_And_Care_Customize_Color_Control($wp_customize, 'paws_and_care_theme_color', array(
        'label' => __('Theme Color', 'paws-and-care'),
        'description' => __('Main theme color for buttons, links, and highlights.', 'paws-and-care'),
        'section' => 'paws_and_care_colors',
        'settings' => 'paws_and_care_theme_color',
    )));

    // 2. Enable Gradients Toggle
    $wp_customize->add_setting('paws_and_care_enable_gradients', array(
        'default' => $defaults['paws_and_care_enable_gradients'],
        'sanitize_callback' => 'paws_and_care_sanitize_checkbox',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control(new Paws_And_Care_Customize_Toggle_Control($wp_customize, 'paws_and_care_enable_gradients', array(
        'label' => __('Enable Gradients', 'paws-and-care'),
        'description' => __('Use gradient backgrounds for buttons and icons instead of solid colors.', 'paws-and-care'),
        'section' => 'paws_and_care_colors',
        'settings' => 'paws_and_care_enable_gradients',
    )));

    // 3. Gradient End Color (was Accent Color) - conditionally visible via JS
    $wp_customize->add_setting('paws_and_care_gradient_end_color', array(
        'default' => $defaults['paws_and_care_gradient_end_color'],
        'sanitize_callback' => 'paws_and_care_sanitize_rgba_color',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control(new Paws_And_Care_Customize_Color_Control($wp_customize, 'paws_and_care_gradient_end_color', array(
        'label' => __('Gradient End Color', 'paws-and-care'),
        'description' => __('End color for gradient backgrounds. Should complement Theme Color.', 'paws-and-care'),
        'section' => 'paws_and_care_colors',
        'settings' => 'paws_and_care_gradient_end_color',
    )));

    // 4. Secondary Color
    $wp_customize->add_setting('paws_and_care_secondary_color', array(
        'default' => $defaults['paws_and_care_secondary_color'],
        'sanitize_callback' => 'paws_and_care_sanitize_rgba_color',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control(new Paws_And_Care_Customize_Color_Control($wp_customize, 'paws_and_care_secondary_color', array(
        'label' => __('Secondary Color', 'paws-and-care'),
        'description' => __('Secondary buttons and less prominent elements.', 'paws-and-care'),
        'section' => 'paws_and_care_colors',
        'settings' => 'paws_and_care_secondary_color',
    )));

    // ========================================
    // TEXT COLORS
    // ========================================

    $wp_customize->add_setting('paws_and_care_text_colors_heading', array(
        'sanitize_callback' => 'sanitize_text_field',
    ));
    $wp_customize->add_control(new Paws_And_Care_Customize_Heading_Control($wp_customize, 'paws_and_care_text_colors_heading', array(
        'label' => __('Text Colors', 'paws-and-care'),
        'section' => 'paws_and_care_colors',
    )));

    // Body Text Color
    $wp_customize->add_setting('paws_and_care_body_text_color', array(
        'default' => $defaults['paws_and_care_body_text_color'],
        'sanitize_callback' => 'sanitize_hex_color',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'paws_and_care_body_text_color', array(
        'label' => __('Body Text Color', 'paws-and-care'),
        'description' => __('Ensure good contrast with Content Background.', 'paws-and-care'),
        'section' => 'paws_and_care_colors',
        'settings' => 'paws_and_care_body_text_color',
    )));

    // Heading Color
    $wp_customize->add_setting('paws_and_care_heading_color', array(
        'default' => $defaults['paws_and_care_heading_color'],
        'sanitize_callback' => 'sanitize_hex_color',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'paws_and_care_heading_color', array(
        'label' => __('Heading Color', 'paws-and-care'),
        'description' => __('Color for H1-H6 headings.', 'paws-and-care'),
        'section' => 'paws_and_care_colors',
        'settings' => 'paws_and_care_heading_color',
    )));

    // Link Color
    $wp_customize->add_setting('paws_and_care_link_color', array(
        'default' => $defaults['paws_and_care_link_color'],
        'sanitize_callback' => 'sanitize_hex_color',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'paws_and_care_link_color', array(
        'label' => __('Link Color', 'paws-and-care'),
        'description' => __('Should stand out from body text.', 'paws-and-care'),
        'section' => 'paws_and_care_colors',
        'settings' => 'paws_and_care_link_color',
    )));

    // Link Hover Color
    $wp_customize->add_setting('paws_and_care_link_hover_color', array(
        'default' => $defaults['paws_and_care_link_hover_color'],
        'sanitize_callback' => 'sanitize_hex_color',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'paws_and_care_link_hover_color', array(
        'label' => __('Link Hover Color', 'paws-and-care'),
        'description' => __('Color when hovering over links.', 'paws-and-care'),
        'section' => 'paws_and_care_colors',
        'settings' => 'paws_and_care_link_hover_color',
    )));

    // ========================================
    // BACKGROUND COLORS
    // ========================================

    $wp_customize->add_setting('paws_and_care_bg_colors_heading', array(
        'sanitize_callback' => 'sanitize_text_field',
    ));
    $wp_customize->add_control(new Paws_And_Care_Customize_Heading_Control($wp_customize, 'paws_and_care_bg_colors_heading', array(
        'label' => __('Background Colors', 'paws-and-care'),
        'section' => 'paws_and_care_colors',
    )));

    // Body Background Color
    $wp_customize->add_setting('paws_and_care_body_bg_color', array(
        'default' => $defaults['paws_and_care_body_bg_color'],
        'sanitize_callback' => 'paws_and_care_sanitize_rgba_color',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control(new Paws_And_Care_Customize_Color_Control($wp_customize, 'paws_and_care_body_bg_color', array(
        'label' => __('Body Background', 'paws-and-care'),
        'description' => __('Main page background. Supports transparency.', 'paws-and-care'),
        'section' => 'paws_and_care_colors',
        'settings' => 'paws_and_care_body_bg_color',
    )));

    // Content Background Color
    $wp_customize->add_setting('paws_and_care_content_bg_color', array(
        'default' => $defaults['paws_and_care_content_bg_color'],
        'sanitize_callback' => 'paws_and_care_sanitize_rgba_color',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control(new Paws_And_Care_Customize_Color_Control($wp_customize, 'paws_and_care_content_bg_color', array(
        'label' => __('Content Background', 'paws-and-care'),
        'description' => __('Cards and content areas. Ensure text is readable.', 'paws-and-care'),
        'section' => 'paws_and_care_colors',
        'settings' => 'paws_and_care_content_bg_color',
    )));

    // ========================================
    // BUTTON COLORS
    // ========================================

    $wp_customize->add_setting('paws_and_care_btn_colors_heading', array(
        'sanitize_callback' => 'sanitize_text_field',
    ));
    $wp_customize->add_control(new Paws_And_Care_Customize_Heading_Control($wp_customize, 'paws_and_care_btn_colors_heading', array(
        'label' => __('Button Colors', 'paws-and-care'),
        'section' => 'paws_and_care_colors',
    )));

    // Button Background Color
    $wp_customize->add_setting('paws_and_care_btn_bg_color', array(
        'default' => $defaults['paws_and_care_btn_bg_color'],
        'sanitize_callback' => 'paws_and_care_sanitize_rgba_color',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control(new Paws_And_Care_Customize_Color_Control($wp_customize, 'paws_and_care_btn_bg_color', array(
        'label' => __('Button Background', 'paws-and-care'),
        'description' => __('Note: Decorative elements like the Hero Slider and Service buttons are excluded to preserve the core color identity (Theme Color).', 'paws-and-care'),
        'section' => 'paws_and_care_colors',
        'settings' => 'paws_and_care_btn_bg_color',
    )));

    // Button Text Color
    $wp_customize->add_setting('paws_and_care_btn_text_color', array(
        'default' => $defaults['paws_and_care_btn_text_color'],
        'sanitize_callback' => 'sanitize_hex_color',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'paws_and_care_btn_text_color', array(
        'label' => __('Button Text Color', 'paws-and-care'),
        'description' => __('Should contrast with button background.', 'paws-and-care'),
        'section' => 'paws_and_care_colors',
        'settings' => 'paws_and_care_btn_text_color',
    )));

    // Button Hover Background Color
    $wp_customize->add_setting('paws_and_care_btn_hover_bg_color', array(
        'default' => $defaults['paws_and_care_btn_hover_bg_color'],
        'sanitize_callback' => 'paws_and_care_sanitize_rgba_color',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control(new Paws_And_Care_Customize_Color_Control($wp_customize, 'paws_and_care_btn_hover_bg_color', array(
        'label' => __('Button Hover Background', 'paws-and-care'),
        'description' => __('Button background when hovered.', 'paws-and-care'),
        'section' => 'paws_and_care_colors',
        'settings' => 'paws_and_care_btn_hover_bg_color',
    )));

    // Button Hover Text Color
    $wp_customize->add_setting('paws_and_care_btn_hover_text_color', array(
        'default' => $defaults['paws_and_care_btn_hover_text_color'],
        'sanitize_callback' => 'sanitize_hex_color',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'paws_and_care_btn_hover_text_color', array(
        'label' => __('Button Hover Text', 'paws-and-care'),
        'description' => __('Button text color when hovered.', 'paws-and-care'),
        'section' => 'paws_and_care_colors',
        'settings' => 'paws_and_care_btn_hover_text_color',
    )));

    // ========================================
    // RESET COLORS BUTTON
    // ========================================

    $wp_customize->add_setting('paws_and_care_reset_colors_heading', array(
        'sanitize_callback' => 'sanitize_text_field',
    ));
    $wp_customize->add_control(new Paws_And_Care_Customize_Heading_Control($wp_customize, 'paws_and_care_reset_colors_heading', array(
        'label' => __('Reset Options', 'paws-and-care'),
        'section' => 'paws_and_care_colors',
    )));

    // Reset Colors - Hidden setting for AJAX
    $wp_customize->add_setting('paws_and_care_reset_colors', array(
        'default' => '',
        'sanitize_callback' => 'sanitize_text_field',
        'transport' => 'postMessage',
    ));
    $wp_customize->add_control(new Paws_And_Care_Customize_Button_Control($wp_customize, 'paws_and_care_reset_colors', array(
        'label' => __('Reset Colors', 'paws-and-care'),
        'description' => __('Click to reset all colors to their default values. Changes will be applied after you click Publish.', 'paws-and-care'),
        'section' => 'paws_and_care_colors',
        'button_text' => __('Reset Colors to Defaults', 'paws-and-care'),
        'button_class' => 'button button-secondary',
        'input_attrs' => array(
            'action' => 'reset-colors',
        ),
    )));
}

/**
 * Enqueue customizer scripts for conditional visibility and reset button
 */
function paws_and_care_colors_customizer_scripts()
{
    $defaults = PAWS_AND_CARE_COLOR_DEFAULTS;
    ?>
    <script>
        (function ($) {
            wp.customize.bind('ready', function () {

                // ========================================
                // CONDITIONAL VISIBILITY: Gradient End Color
                // ========================================
                var toggleGradientEndVisibility = function (value) {
                    var gradientControl = $('#customize-control-paws_and_care_gradient_end_color');
                    if (value) {
                        gradientControl.slideDown(200);
                    } else {
                        gradientControl.slideUp(200);
                    }
                };

                // Set initial state
                setTimeout(function () {
                    var gradientEnabled = wp.customize('paws_and_care_enable_gradients').get();
                    if (!gradientEnabled) {
                        $('#customize-control-paws_and_care_gradient_end_color').hide();
                    }
                }, 100);

                // Listen for changes
                wp.customize('paws_and_care_enable_gradients', function (setting) {
                    setting.bind(toggleGradientEndVisibility);
                });

                // ========================================
                // RESET COLORS BUTTON
                // ========================================
                setTimeout(function () {
                    $(document).on('click', '[data-action="reset-colors"], #paws_and_care_reset_colors-button', function (e) {
                        e.preventDefault();

                        if (!confirm('<?php echo esc_js(__('Are you sure you want to reset all colors to defaults?', 'paws-and-care')); ?>')) {
                            return;
                        }

                        // Color defaults
                        var defaults = <?php echo json_encode($defaults); ?>;

                        // Reset each color setting
                        $.each(defaults, function (setting, value) {
                            var control = wp.customize(setting);
                            if (control) {
                                control.set(value);
                            }
                        });

                        // Show success message
                        alert('<?php echo esc_js(__('Colors reset to defaults! Click Publish to save changes.', 'paws-and-care')); ?>');
                    });
                }, 500);
            });
        })(jQuery);
    </script>
    <?php
}
add_action('customize_controls_print_footer_scripts', 'paws_and_care_colors_customizer_scripts');
