<?php
/**
 * Blog Section Customizer Settings
 *
 * @package Paws_And_Care
 * @since 1.0.0
 */

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

/**
 * Register Blog section customizer settings
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function paws_and_care_customizer_blog( $wp_customize ) {

    // Blog Section
    $wp_customize->add_section( 'paws_and_care_blog', array(
        'title'    => __( 'Blog Settings', 'paws-and-care' ),
        'panel'    => 'paws_and_care_front_page',
        'priority' => 70,
    ) );

    // Enable Blog Section
    $wp_customize->add_setting( 'paws_and_care_blog_enable', 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_blog_enable', array(
        'label'       => __( 'Enable Blog Section', 'paws-and-care' ),
        'description' => __( 'Show or hide the blog section on homepage', 'paws-and-care' ),
        'section'     => 'paws_and_care_blog',
    ) ) );

    // Content Settings Heading
    $wp_customize->add_setting( 'paws_and_care_blog_content_heading', array(
        'sanitize_callback' => 'sanitize_text_field',
    ) );

    $wp_customize->add_control( new Paws_And_Care_Customize_Heading_Control( $wp_customize, 'paws_and_care_blog_content_heading', array(
        'label'           => __( 'Content Settings', 'paws-and-care' ),
        'section'         => 'paws_and_care_blog',
        'active_callback' => 'paws_and_care_is_blog_enabled',
    ) ) );

    // Section Title
    $wp_customize->add_setting( 'paws_and_care_blog_title', array(
        'default'           => __( 'From Our Blog', 'paws-and-care' ),
        'sanitize_callback' => 'sanitize_text_field',
    ) );

    $wp_customize->add_control( 'paws_and_care_blog_title', array(
        'label'           => __( 'Section Title', 'paws-and-care' ),
        'section'         => 'paws_and_care_blog',
        'type'            => 'text',
        'active_callback' => 'paws_and_care_is_blog_enabled',
    ) );

    // Subtitle
    $wp_customize->add_setting( 'paws_and_care_blog_subtitle', array(
        'default'           => __( 'Tips, stories, and updates from the world of pet care.', 'paws-and-care' ),
        'sanitize_callback' => 'sanitize_text_field',
    ) );

    $wp_customize->add_control( 'paws_and_care_blog_subtitle', array(
        'label'           => __( 'Subtitle', 'paws-and-care' ),
        'section'         => 'paws_and_care_blog',
        'type'            => 'text',
        'active_callback' => 'paws_and_care_is_blog_enabled',
    ) );

    // Query Settings Heading
    $wp_customize->add_setting( 'paws_and_care_blog_query_heading', array(
        'sanitize_callback' => 'sanitize_text_field',
    ) );

    $wp_customize->add_control( new Paws_And_Care_Customize_Heading_Control( $wp_customize, 'paws_and_care_blog_query_heading', array(
        'label'           => __( 'Query Settings', 'paws-and-care' ),
        'section'         => 'paws_and_care_blog',
        'active_callback' => 'paws_and_care_is_blog_enabled',
    ) ) );

    // Number of Posts
    $wp_customize->add_setting( 'paws_and_care_blog_posts_count', array(
        'default'           => 3,
        'sanitize_callback' => 'absint',
    ) );

    $wp_customize->add_control( new Paws_And_Care_Customize_Range_Control( $wp_customize, 'paws_and_care_blog_posts_count', array(
        'label'           => __( 'Number of Posts', 'paws-and-care' ),
        'section'         => 'paws_and_care_blog',
        'input_attrs'     => array(
            'min'  => 1,
            'max'  => 12,
            'step' => 1,
        ),
        'active_callback' => 'paws_and_care_is_blog_enabled',
    ) ) );

    // Category Filter
    $wp_customize->add_setting( 'paws_and_care_blog_category', array(
        'default'           => '',
        'sanitize_callback' => 'sanitize_text_field',
    ) );

    $wp_customize->add_control( 'paws_and_care_blog_category', array(
        'label'           => __( 'Category', 'paws-and-care' ),
        'description'     => __( 'Select a category or leave empty for all', 'paws-and-care' ),
        'section'         => 'paws_and_care_blog',
        'type'            => 'select',
        'choices'         => paws_and_care_get_categories_choices(),
        'active_callback' => 'paws_and_care_is_blog_enabled',
    ) );

    // Display Settings Heading
    $wp_customize->add_setting( 'paws_and_care_blog_display_heading', array(
        'sanitize_callback' => 'sanitize_text_field',
    ) );

    $wp_customize->add_control( new Paws_And_Care_Customize_Heading_Control( $wp_customize, 'paws_and_care_blog_display_heading', array(
        'label'           => __( 'Display Settings', 'paws-and-care' ),
        'section'         => 'paws_and_care_blog',
        'active_callback' => 'paws_and_care_is_blog_enabled',
    ) ) );

    // Show Category Badge
    $wp_customize->add_setting( 'paws_and_care_blog_show_category', 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_blog_show_category', array(
        'label'           => __( 'Show Category Badge', 'paws-and-care' ),
        'section'         => 'paws_and_care_blog',
        'active_callback' => 'paws_and_care_is_blog_enabled',
    ) ) );

    // Show Date
    $wp_customize->add_setting( 'paws_and_care_blog_show_date', 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_blog_show_date', array(
        'label'           => __( 'Show Date', 'paws-and-care' ),
        'section'         => 'paws_and_care_blog',
        'active_callback' => 'paws_and_care_is_blog_enabled',
    ) ) );

    // Excerpt Length
    $wp_customize->add_setting( 'paws_and_care_blog_excerpt_length', array(
        'default'           => 20,
        'sanitize_callback' => 'absint',
    ) );

    $wp_customize->add_control( new Paws_And_Care_Customize_Range_Control( $wp_customize, 'paws_and_care_blog_excerpt_length', array(
        'label'           => __( 'Excerpt Length (words)', 'paws-and-care' ),
        'section'         => 'paws_and_care_blog',
        'input_attrs'     => array(
            'min'  => 10,
            'max'  => 50,
            'step' => 5,
        ),
        'active_callback' => 'paws_and_care_is_blog_enabled',
    ) ) );

    // Read More Text
    $wp_customize->add_setting( 'paws_and_care_blog_read_more', array(
        'default'           => __( 'Read More', 'paws-and-care' ),
        'sanitize_callback' => 'sanitize_text_field',
    ) );

    $wp_customize->add_control( 'paws_and_care_blog_read_more', array(
        'label'           => __( 'Read More Text', 'paws-and-care' ),
        'section'         => 'paws_and_care_blog',
        'type'            => 'text',
        'active_callback' => 'paws_and_care_is_blog_enabled',
    ) );
}

/**
 * Active callback for blog section
 */
function paws_and_care_is_blog_enabled() {
    return get_theme_mod( 'paws_and_care_blog_enable', true );
}

/**
 * Get categories for dropdown
 */
function paws_and_care_get_categories_choices() {
    $choices    = array( '' => __( 'All Categories', 'paws-and-care' ) );
    $categories = get_categories( array( 'hide_empty' => false ) );

    foreach ( $categories as $category ) {
        $choices[ $category->term_id ] = $category->name;
    }

    return $choices;
}

