<?php
/**
 * Contact Page Template Customizer Settings
 *
 * @package Paws_And_Care
 * @since 1.2.6
 */

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

/**
 * Add Contact Page Template customizer settings
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function paws_and_care_customizer_contact_page_template($wp_customize)
{

    // Contact Page Template Section
    $wp_customize->add_section('paws_and_care_contact_page_template', array(
        'title' => __('Contact Page Template', 'paws-and-care'),
        'panel' => 'paws_and_care_templates',
        'priority' => 40,
    ));

    // --- Contact Info ---

    // Show Contact Info
    $wp_customize->add_setting('paws_and_care_contact_page_show_info', array(
        'default' => true,
        'sanitize_callback' => 'paws_and_care_sanitize_checkbox',
    ));
    $wp_customize->add_control(new Paws_And_Care_Customize_Toggle_Control($wp_customize, 'paws_and_care_contact_page_show_info', array(
        'label' => __('Show Contact Info', 'paws-and-care'),
        'section' => 'paws_and_care_contact_page_template',
    )));

    // Address
    $wp_customize->add_setting('paws_and_care_contact_address', array(
        'default' => '123 Pet Care Avenue, Animal City, AC 12345',
        'sanitize_callback' => 'sanitize_text_field',
    ));
    $wp_customize->add_control('paws_and_care_contact_address', array(
        'label' => __('Address', 'paws-and-care'),
        'section' => 'paws_and_care_contact_page_template',
        'type' => 'text',
    ));

    // Phone
    $wp_customize->add_setting('paws_and_care_contact_phone', array(
        'default' => '+1 234 567 890',
        'sanitize_callback' => 'sanitize_text_field',
    ));
    $wp_customize->add_control('paws_and_care_contact_phone', array(
        'label' => __('Phone', 'paws-and-care'),
        'section' => 'paws_and_care_contact_page_template',
        'type' => 'text',
    ));

    // Email
    $wp_customize->add_setting('paws_and_care_contact_email', array(
        'default' => 'example@example.com',
        'sanitize_callback' => 'sanitize_email',
    ));
    $wp_customize->add_control('paws_and_care_contact_email', array(
        'label' => __('Email', 'paws-and-care'),
        'section' => 'paws_and_care_contact_page_template',
        'type' => 'email',
    ));

    // Hours
    $wp_customize->add_setting('paws_and_care_contact_hours', array(
        'default' => 'Mon-Fri: 9AM-6PM, Sat: 9AM-4PM',
        'sanitize_callback' => 'sanitize_textarea_field',
    ));
    $wp_customize->add_control('paws_and_care_contact_hours', array(
        'label' => __('Hours', 'paws-and-care'),
        'section' => 'paws_and_care_contact_page_template',
        'type' => 'textarea',
    ));

    // --- Emergency Contact ---

    // Show Emergency
    $wp_customize->add_setting('paws_and_care_contact_page_show_emergency', array(
        'default' => true,
        'sanitize_callback' => 'paws_and_care_sanitize_checkbox',
    ));
    $wp_customize->add_control(new Paws_And_Care_Customize_Toggle_Control($wp_customize, 'paws_and_care_contact_page_show_emergency', array(
        'label' => __('Show Emergency Box', 'paws-and-care'),
        'section' => 'paws_and_care_contact_page_template',
    )));

    // Emergency Label
    $wp_customize->add_setting('paws_and_care_contact_emergency_label', array(
        'default' => '24/7 Emergency Line',
        'sanitize_callback' => 'sanitize_text_field',
    ));
    $wp_customize->add_control('paws_and_care_contact_emergency_label', array(
        'label' => __('Emergency Label', 'paws-and-care'),
        'section' => 'paws_and_care_contact_page_template',
        'type' => 'text',
    ));

    // Emergency Phone
    $wp_customize->add_setting('paws_and_care_contact_emergency_phone', array(
        'default' => '+1 234 567 999',
        'sanitize_callback' => 'sanitize_text_field',
    ));
    $wp_customize->add_control('paws_and_care_contact_emergency_phone', array(
        'label' => __('Emergency Phone', 'paws-and-care'),
        'section' => 'paws_and_care_contact_page_template',
        'type' => 'text',
    ));

    // Emergency Text
    $wp_customize->add_setting('paws_and_care_contact_emergency_text', array(
        'default' => 'For urgent pet emergencies outside of business hours, please call our emergency hotline.',
        'sanitize_callback' => 'sanitize_text_field',
    ));
    $wp_customize->add_control('paws_and_care_contact_emergency_text', array(
        'label' => __('Emergency Description', 'paws-and-care'),
        'section' => 'paws_and_care_contact_page_template',
        'type' => 'text',
    ));

    // --- Map ---

    // Show Map
    $wp_customize->add_setting('paws_and_care_contact_page_show_map', array(
        'default' => true,
        'sanitize_callback' => 'paws_and_care_sanitize_checkbox',
    ));
    $wp_customize->add_control(new Paws_And_Care_Customize_Toggle_Control($wp_customize, 'paws_and_care_contact_page_show_map', array(
        'label' => __('Show Map Section', 'paws-and-care'),
        'section' => 'paws_and_care_contact_page_template',
    )));

    // Map Title
    $wp_customize->add_setting('paws_and_care_contact_map_title', array(
        'default' => 'Find Us',
        'sanitize_callback' => 'sanitize_text_field',
    ));
    $wp_customize->add_control('paws_and_care_contact_map_title', array(
        'label' => __('Map Title', 'paws-and-care'),
        'section' => 'paws_and_care_contact_page_template',
        'type' => 'text',
    ));

    // Map Embed URL
    $wp_customize->add_setting('paws_and_care_contact_map_embed', array(
        'default' => '',
        'sanitize_callback' => 'esc_url_raw',
    ));
    $wp_customize->add_control('paws_and_care_contact_map_embed', array(
        'label' => __('Google Maps Embed URL', 'paws-and-care'),
        'section' => 'paws_and_care_contact_page_template',
        'type' => 'url',
    ));

    // --- FAQ (simplified - no repeater) ---

    // Show FAQ
    $wp_customize->add_setting('paws_and_care_contact_page_show_faq', array(
        'default' => true,
        'sanitize_callback' => 'paws_and_care_sanitize_checkbox',
    ));
    $wp_customize->add_control(new Paws_And_Care_Customize_Toggle_Control($wp_customize, 'paws_and_care_contact_page_show_faq', array(
        'label' => __('Show FAQ Section', 'paws-and-care'),
        'description' => __('Uses default FAQs. Use FAQ plugins for custom FAQs.', 'paws-and-care'),
        'section' => 'paws_and_care_contact_page_template',
    )));

    // FAQ Title
    $wp_customize->add_setting('paws_and_care_contact_faq_title', array(
        'default' => 'Frequently Asked Questions',
        'sanitize_callback' => 'sanitize_text_field',
    ));
    $wp_customize->add_control('paws_and_care_contact_faq_title', array(
        'label' => __('FAQ Title', 'paws-and-care'),
        'section' => 'paws_and_care_contact_page_template',
        'type' => 'text',
    ));

    // FAQ Items Repeater
    $wp_customize->add_setting('paws_and_care_contact_faq_items', array(
        'default' => paws_and_care_get_faq_default(),
        'sanitize_callback' => 'paws_and_care_sanitize_faq_items',
    ));
    $wp_customize->add_control(new Paws_And_Care_Repeater($wp_customize, 'paws_and_care_contact_faq_items', array(
        'label' => __('FAQ Items', 'paws-and-care'),
        'section' => 'paws_and_care_contact_page_template',
        'add_field_label' => __('Add FAQ Item', 'paws-and-care'),
        'item_name' => __('FAQ Item', 'paws-and-care'),
        'max_items' => 6,
        'customizer_repeater_icon_control' => true,
        'customizer_repeater_title_control' => true,
        'customizer_repeater_text_control' => true,
    )));
}

/**
 * Sanitize FAQ items — enforces a hard limit of 5 items at save time.
 *
 * @param string $input JSON-encoded FAQ items.
 * @return string Sanitized JSON with at most 5 items.
 */
function paws_and_care_sanitize_faq_items( $input ) {
    $decoded = json_decode( $input, true );

    if ( ! is_array( $decoded ) ) {
        return paws_and_care_repeater_sanitize( $input );
    }

    // Enforce hard maximum of 5 FAQ items.
    $decoded = array_slice( $decoded, 0, 5 );

    return paws_and_care_repeater_sanitize( json_encode( $decoded ) );
}
