<?php
// === Pricing Section Customizer ===
function howcan_pricing_customizer( $wp_customize ) {

    // === Add Section ===
    $wp_customize->add_section( 'howcan_pricing_section', array(
        'title'       => __( 'Pricing Section', 'howcan' ),
        'priority'    => 200,
        'description' => __( 'Manage your pricing table on the front page', 'howcan' ),
    ) );

    // === Show/Hide Toggle ===
    $wp_customize->add_setting( 'howcan_pricing_section_on', array(
        'default'           => true,
        'sanitize_callback' => 'wp_validate_boolean',
    ) );
    $wp_customize->add_control( 'howcan_pricing_section_on', array(
        'label'   => __( 'Enable Pricing Section', 'howcan' ),
        'section' => 'howcan_pricing_section',
        'type'    => 'checkbox',
    ) );

    // === Section Title & Description ===
    $wp_customize->add_setting('howcan_psection_title', array(
        'default' => 'Our Best Plans',
        'sanitize_callback' => 'sanitize_text_field',
    ));
    $wp_customize->add_control('howcan_psection_title', array(
        'label' => __('Section Title', 'howcan'),
        'section' => 'howcan_pricing_section',
        'type' => 'text',
    ));

    $wp_customize->add_setting('howcan_psection_desc', array(
        'default' => 'Choose your landing page package and start your online journey with Howcan Theme.',
        'sanitize_callback' => 'sanitize_text_field',
    ));
    $wp_customize->add_control('howcan_psection_desc', array(
        'label' => __('Section Description', 'howcan'),
        'section' => 'howcan_pricing_section',
        'type' => 'textarea',
    ));

    // === Packages: Basic, Standard, Premium ===
    $packages = array(
        'basic'    => 'Basic',
        'standard' => 'Standard',
        'premium'  => 'Premium',
    );

    foreach ($packages as $key => $label) {

        // ✅ Fixed nested ternary with parentheses
        $defaults = ($key === 'basic') ? 
            array(
                'title'    => "$label Package",
                'price'    => '৳4,999',
                'features' => "1 Page setup using Howcan Theme\nBasic customization\nFree AmarHoster setup\nMobile responsive design\nSSL configuration",
                'btn'      => 'Get Started',
                'link'     => '#',
            ) : (
                ($key === 'standard') ? 
                array(
                    'title'    => "$label Package",
                    'price'    => '৳7,999',
                    'features' => "All features from Basic\nAdvanced customization\nContact form integration\nOn-page SEO optimization\nGoogle Analytics setup",
                    'btn'      => 'Buy Now',
                    'link'     => '#',
                ) :
                array(
                    'title'    => "$label Package",
                    'price'    => '৳12,000 - ৳15,000',
                    'features' => "Everything from Standard\nCustom graphics & sections\nSpeed optimization\nWhatsApp chat button\n1-Year free maintenance",
                    'btn'      => 'Order Now',
                    'link'     => '#',
                )
            );

        foreach ($defaults as $field => $default) {
            $sanitize = ($field === 'features') ? 'wp_kses_post' : (($field === 'link') ? 'esc_url_raw' : 'sanitize_text_field');
            $wp_customize->add_setting("howcan_{$key}_{$field}", array(
                'default' => $default,
                'sanitize_callback' => $sanitize,
            ));

            // Label & Type
            $label_text = ucfirst($field);
            if ($field === 'btn') $label_text = 'Button Text';
            if ($field === 'link') $label_text = 'Button Link';
            if ($field === 'features') $label_text = 'Features (One per line)';
            $type = ($field === 'features') ? 'textarea' : (($field === 'link') ? 'url' : 'text');

            $wp_customize->add_control("howcan_{$key}_{$field}", array(
                'label' => sprintf(__('%s %s', 'howcan'), $label, $label_text),
                'section' => 'howcan_pricing_section',
                'type' => $type,
            ));
        }
    }
}
add_action('customize_register', 'howcan_pricing_customizer');