<?php
/**
 * Theme Customizer Controls
 *
 * @package Creamy Bakery
 */

if ( ! function_exists( 'creamy_bakery_customizer_home_product_register' ) ) :
function creamy_bakery_customizer_home_product_register( $wp_customize ) {

    $wp_customize->add_section(
        'creamy_bakery_home_product_settings',
        array (
            'priority'      => 25,
            'capability'    => 'edit_theme_options',
            'title'         => esc_html__( 'Product Settings', 'creamy-bakery' )
        )
    );

    // Title label
    $wp_customize->add_setting( 
        'creamy_bakery_label_product_settings_title', 
        array(
            'sanitize_callback' => 'creamy_bakery_sanitize_title',
        ) 
    );

    $wp_customize->add_control( 
        new Creamy_Bakery_Title_Info_Control( $wp_customize, 'creamy_bakery_label_product_settings_title', 
        array(
            'label'       => esc_html__( 'Product Settings', 'creamy-bakery' ),
            'section'     => 'creamy_bakery_home_product_settings',
            'type'        => 'creamy-bakery-title',
            'settings'    => 'creamy_bakery_label_product_settings_title',
        ) 
    ));

    $wp_customize->add_setting(
        'creamy_bakery_product_section_bg_image',
        array(
            'default'           => '',
            'sanitize_callback' => 'creamy_bakery_sanitize_image',

        )
    );
    
    $wp_customize->add_control(
        new WP_Customize_Image_Control(
            $wp_customize, 'creamy_bakery_product_section_bg_image', 
            array(
                'label'           => sprintf( esc_html__( 'Section Background Image', 'creamy-bakery' ), ),
                'settings'  => 'creamy_bakery_product_section_bg_image',
                'section'   => 'creamy_bakery_home_product_settings'
            ) 
        )
    );

    $wp_customize->add_setting(
        'creamy_bakery_product_left_image',
        array(
            'default'           => '',
            'sanitize_callback' => 'creamy_bakery_sanitize_image',

        )
    );
    
    $wp_customize->add_control(
        new WP_Customize_Image_Control(
            $wp_customize, 'creamy_bakery_product_left_image', 
            array(
                'label'           => sprintf( esc_html__( 'Left Image', 'creamy-bakery' ), ),
                'settings'  => 'creamy_bakery_product_left_image',
                'section'   => 'creamy_bakery_home_product_settings'
            ) 
        )
    );

    // Banner sm-hd
    $wp_customize->add_setting(
        'creamy_bakery_product_main_heading',
        array(
            'default'           => '',
            'sanitize_callback' => 'sanitize_text_field',
        )
    );

    $wp_customize->add_control(
        'creamy_bakery_product_main_heading',
        array(
            'label'           => sprintf( esc_html__( 'Main Heading', 'creamy-bakery' ), ),
            'section'         => 'creamy_bakery_home_product_settings',
            'settings'        => 'creamy_bakery_product_main_heading' ,
            'type'            => 'text',
        )
    );

    // Banner Heading
    $wp_customize->add_setting(
        'creamy_bakery_product_main_text',
        array(
            'default'           => '',
            'sanitize_callback' => 'sanitize_text_field',
        )
    );

    $wp_customize->add_control(
        'creamy_bakery_product_main_text',
        array(
            'label'           => sprintf( esc_html__( 'Text', 'creamy-bakery' ), ),
            'section'         => 'creamy_bakery_home_product_settings',
            'settings'        => 'creamy_bakery_product_main_text' ,
            'type'            => 'text',
        )
    );
    
     // Get all product categories to populate the dropdown
    $args = array(
        'taxonomy'   => 'product_cat',
        'orderby'    => 'name',
        'order'      => 'ASC',
        'hide_empty' => false, // Show even empty categories
    );

    $categories = get_terms( $args );
    $cats = array();

    // Prepare categories for the dropdown
    if ( ! empty( $categories ) && ! is_wp_error( $categories ) ) {
        foreach ( $categories as $category ) {
            $cats[$category->slug] = $category->name;
        }
    }

    $wp_customize->add_setting(
        'creamy_bakery_product_category',
        array(
            'default'           => '',
            'sanitize_callback' => 'sanitize_text_field',
        )
    );

    $wp_customize->add_control(
        'creamy_bakery_product_category',
        array(
            'label'    => esc_html__( 'Select Product Category', 'creamy-bakery' ),
            'section'  => 'creamy_bakery_home_product_settings',
            'settings' => 'creamy_bakery_product_category',
            'type'     => 'select',
            'choices'  => $cats,
        )
    );

   $wp_customize->add_setting( 'creamy_bakery_product_count', array(
        'default'           => 4, 
        'sanitize_callback' => 'absint',
    ));

    // Add control for number of products
    $wp_customize->add_control( 'creamy_bakery_product_count', array(
        'label'       => __( 'Number of Products to Display', 'creamy-bakery' ),
        'section'     => 'creamy_bakery_home_product_settings', 
        'type'        => 'number',
        'input_attrs' => array(
            'min' => 1,
            'max' => 8,
        ),
    ));
}
endif;

add_action( 'customize_register', 'creamy_bakery_customizer_home_product_register' );