<?php
if (!defined('ABSPATH'))
    exit;

if (!class_exists('pawfect_Customize_Instagram_Gallery_Option')):

    class pawfect_Customize_Instagram_Gallery_Option extends pawfect_Customize_Base_Option
    {

        public function elements()
        {
            return array(

                'pawfect_instagram_heading' => array(
                    'setting' => array(),
                    'control' => array(
                        'type' => 'heading',
                        'priority' => 1,
                        'label' => esc_html__('Instagram Gallery Section', 'pawfect'),
                        'section' => 'pawfect_instagram_gallery_section',
                    ),
                ),

                'pawfect_instagram_gallery_enabled' => array(
                    'setting' => array(
                        'default' => false,
                        'sanitize_callback' => array('pawfect_Customizer_Sanitize', 'sanitize_checkbox'),
                    ),
                    'control' => array(
                        'type' => 'toggle',
                        'priority' => 2,
                        'label' => esc_html__('Enable Instagram Gallery Section', 'pawfect'),
                        'description' => esc_html__('Toggle ON to show this section', 'pawfect'),
                        'section' => 'pawfect_instagram_gallery_section',
                        'active_callback' => 'pawfect_instagram_pro_only',
                    ),
                ),

                'pawfect_instagram_heading_text' => array(
                    'setting' => array(
                        'default' => '@PAWS',
                        'sanitize_callback' => 'sanitize_text_field',
                    ),
                    'control' => array(
                        'type' => 'text',
                        'priority' => 3,
                        'label' => esc_html__('Heading Text', 'pawfect'),
                        'section' => 'pawfect_instagram_gallery_section',
                        'active_callback' => 'pawfect_instagram_pro_only',
                    ),
                ),

                'pawfect_instagram_subtext' => array(
                    'setting' => array(
                        'default' => 'FOLLOW US ON INSTAGRAM',
                        'sanitize_callback' => 'sanitize_text_field',
                    ),
                    'control' => array(
                        'type' => 'text',
                        'priority' => 4,
                        'label' => esc_html__('Sub Text', 'pawfect'),
                        'section' => 'pawfect_instagram_gallery_section',
                        'active_callback' => 'pawfect_instagram_gallery_enabled',
                    ),
                ),
                'pawfect_instagram_access_token' => array(
                    'setting' => array(
                        'default' => '',
                        'sanitize_callback' => 'sanitize_text_field',
                    ),
                    'control' => array(
                        'type' => 'text',
                        'priority' => 6,
                        'label' => esc_html__('Instagram Access Token', 'pawfect'),
                        'description' => esc_html__('Enter your long-lived Instagram Graph API Access Token', 'pawfect'),
                        'section' => 'pawfect_instagram_gallery_section',
                        'active_callback' => 'pawfect_instagram_api_mode_active',
                    ),
                ),

                'pawfect_instagram_user_id' => array(
                    'setting' => array(
                        'default' => '',
                        'sanitize_callback' => 'sanitize_text_field',
                    ),
                    'control' => array(
                        'type' => 'text',
                        'priority' => 7,
                        'label' => esc_html__('Instagram User ID', 'pawfect'),
                        'description' => esc_html__('Your Instagram Business User ID', 'pawfect'),
                        'section' => 'pawfect_instagram_gallery_section',
                        'active_callback' => 'pawfect_instagram_api_mode_active',
                    ),
                ),

                'pawfect_instagram_mode' => array(
                    'setting' => array(
                        'default' => 'api',
                        'sanitize_callback' => 'sanitize_text_field',
                    ),
                    'control' => array(
                        'type' => 'radio-switch',
                        'priority' => 5,
                        'label' => esc_html__('Instagram Source', 'pawfect'),
                        'section' => 'pawfect_instagram_gallery_section',
                        'choices' => array(
                            'api' => esc_html__('Instagram API', 'pawfect'),
                            'manual' => esc_html__('Manual Upload', 'pawfect'),
                        ),
                        'active_callback' => 'pawfect_instagram_gallery_enabled',
                    ),
                ),

                'pawfect_instagram_notice' => array(
                    'setting' => array(),
                    'control' => array(
                        'type' => 'description',
                        'priority' => 5.5,
                        'label' => esc_html__('How to Get Token & User ID', 'pawfect'),
                        'description' => esc_html__('You need a Business or Creator Instagram account linked to a Facebook Page. Use tools like Graph API Explorer to generate tokens.', 'pawfect'),
                        'section' => 'pawfect_instagram_gallery_section',
                        'active_callback' => 'pawfect_instagram_api_mode_active',
                    ),
                ),

                'pawfect_instagram_manual_images' => array(
                    'setting' => array(
                        'default' => '',
                        'sanitize_callback' => 'wp_kses_post',
                    ),
                    'control' => array(
                        'type' => 'repeater',
                        'priority' => 8,
                        'label' => esc_html__('Manual Gallery Images', 'pawfect'),
                        'section' => 'pawfect_instagram_gallery_section',
                        'add_field_label' => esc_html__('Add new image', 'pawfect'),
                        'item_name' => esc_html__('Gallery Item', 'pawfect'),
                        'customizer_repeater_image_control' => true,
                        'customizer_repeater_link_control' => true,
                        'customizer_repeater_text_control' => true,
                        'customizer_repeater_checkbox_control' => true,
                        'active_callback' => 'pawfect_instagram_manual_mode_active',
                    ),
                ),

                // Upgrade control for Instagram Gallery
                'pawfect_instagram_gallery_upsale' => array(
                    'setting' => array(
                        'sanitize_callback' => 'sanitize_text_field',
                    ),
                    'control' => array(
                        'type' => 'pawfect-upgrade',
                        'priority' => 1,
                        'label' => esc_html__('Instagram Gallery Features', 'pawfect'),
                        'section' => 'pawfect_instagram_gallery_section',
                        'upgrade_message' => esc_html__('for Instagram Gallery integration and premium features!', 'pawfect'),
                    ),
                ),
            );
        }
    }

    new pawfect_Customize_Instagram_Gallery_Option();

endif;

/**
 * Active callback functions for Instagram settings
 */
function pawfect_instagram_gallery_enabled($control) {
    // Following main slider pattern - return true when instagram gallery is enabled
    // This makes other instagram settings visible when main toggle is ON
    return true === ( $control->manager->get_setting( 'pawfect_instagram_gallery_enabled' )->value() == true );
}

function pawfect_instagram_api_mode_active($control) {
    // Check if Instagram gallery is enabled AND mode is API
    $gallery_enabled = $control->manager->get_setting( 'pawfect_instagram_gallery_enabled' )->value();
    $mode_is_api = get_theme_mod('pawfect_instagram_mode', 'api') === 'api';
    return ($gallery_enabled == true) && $mode_is_api;
}

function pawfect_instagram_manual_mode_active($control) {
    // Check if Instagram gallery is enabled AND mode is manual
    $gallery_enabled = $control->manager->get_setting( 'pawfect_instagram_gallery_enabled' )->value();
    $mode_is_manual = get_theme_mod('pawfect_instagram_mode', 'api') === 'manual';
    return ($gallery_enabled == true) && $mode_is_manual;
}

function pawfect_instagram_pro_only($control) {
    // Pro-only features - always return false to make them inactive
    return false;
}
