<?php
/**
 * Theme Customizer Controls
 *
 * @package Unique Gift Shop
 */

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

    $wp_customize->add_section(
        'unique_gift_shop_home_product_settings',
        array (
            'priority'      => 25,
            'capability'    => 'edit_theme_options',
            'title'         => esc_html__( 'Product Settings', 'unique-gift-shop' )
        )
    );

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

    $wp_customize->add_control( 
        new Unique_Gift_Shop_Title_Info_Control( $wp_customize, 'unique_gift_shop_label_product_settings_title', 
        array(
            'label'       => esc_html__( 'Product Settings', 'unique-gift-shop' ),
            'section'     => 'unique_gift_shop_home_product_settings',
            'type'        => 'unique-gift-shop-title',
            'settings'    => 'unique_gift_shop_label_product_settings_title',
        ) 
    ));

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

    $wp_customize->add_control(
        'unique_gift_shop_product_main_heading',
        array(
            'label'           => sprintf( esc_html__( 'Main Heading', 'unique-gift-shop' ), ),
            'section'         => 'unique_gift_shop_home_product_settings',
            'settings'        => 'unique_gift_shop_product_main_heading' ,
            '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(
        'unique_gift_shop_product_category',
        array(
            'default'           => '',
            'sanitize_callback' => 'sanitize_text_field',
        )
    );

    $wp_customize->add_control(
        'unique_gift_shop_product_category',
        array(
            'label'    => esc_html__( 'Select Product Category', 'unique-gift-shop' ),
            'section'  => 'unique_gift_shop_home_product_settings',
            'settings' => 'unique_gift_shop_product_category',
            'type'     => 'select',
            'choices'  => $cats,
        )
    );

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

    // Add control for number of products
    $wp_customize->add_control( 'unique_gift_shop_product_count', array(
        'label'       => __( 'Number of Products to Display', 'unique-gift-shop' ),
        'section'     => 'unique_gift_shop_home_product_settings', 
        'type'        => 'number',
        'input_attrs' => array(
            'min' => 1,
            'max' => 8,
        ),
    ));
}
endif;

add_action( 'customize_register', 'unique_gift_shop_customizer_home_product_register' );