<?php
/**
 * Software Launch Theme Functions
 * 
 * @package SoftwareLaunchTheme
 * @version 1.0.0
 */

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

define('SLT_VERSION', '1.0.0');
define('SLT_DIR', get_template_directory());
define('SLT_URI', get_template_directory_uri());

/**
 * Theme Setup
 */
function slt_theme_setup() {
    // Add theme supports
    add_theme_support('title-tag');
    add_theme_support('post-thumbnails');
    add_theme_support('html5', array(
        'search-form',
        'comment-form',
        'comment-list',
        'gallery',
        'caption',
        'style',
        'script',
    ));
    
    // Custom image sizes
    add_image_size('slt-testimonial', 120, 120, true);
    add_image_size('slt-bonus', 240, 240, true);
    add_image_size('slt-feature-icon', 128, 128, true);
    add_image_size('slt-hero-video-poster', 1280, 720, true);
    add_image_size('slt-product-mockup', 600, 600, false);
}
add_action('after_setup_theme', 'slt_theme_setup');

/**
 * Enqueue Scripts and Styles
 */
function slt_enqueue_assets() {
    // Google Fonts
    wp_enqueue_style(
        'slt-google-fonts',
        'https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&family=Montserrat:wght@600;700;800&display=swap',
        array(),
        null
    );
    
    // Theme Stylesheet
    wp_enqueue_style(
        'slt-style',
        get_stylesheet_uri(),
        array('slt-google-fonts'),
        SLT_VERSION
    );
    
    // Countdown Timer Script
    wp_enqueue_script(
        'slt-countdown',
        SLT_URI . '/assets/js/countdown.js',
        array(),
        SLT_VERSION,
        true
    );
    
    // Localize countdown data
    $countdown_deadline = get_field('countdown_deadline', get_the_ID());
    $countdown_hours = get_field('countdown_hours', get_the_ID()) ?: 24;
    
    wp_localize_script('slt-countdown', 'sltCountdown', array(
        'deadline' => $countdown_deadline ?: '',
        'hours' => intval($countdown_hours),
        'timezone' => wp_timezone_string(),
    ));
}
add_action('wp_enqueue_scripts', 'slt_enqueue_assets');

/**
 * Register ACF Fields if ACF is active
 */
function slt_register_acf_fields() {
    if (!function_exists('acf_add_local_field_group')) {
        return;
    }
    
    // Field Group: Global Settings
    acf_add_local_field_group(array(
        'key' => 'group_slt_global',
        'title' => 'Software Launch - Global Settings',
        'fields' => array(
            // Countdown Settings
            array(
                'key' => 'field_slt_countdown_tab',
                'label' => 'Countdown Timer',
                'name' => '',
                'type' => 'tab',
            ),
            array(
                'key' => 'field_slt_countdown_deadline',
                'label' => 'Countdown Deadline',
                'name' => 'countdown_deadline',
                'type' => 'date_time_picker',
                'instructions' => 'Set specific deadline, or leave blank to use hours countdown',
                'display_format' => 'F j, Y g:i a',
                'return_format' => 'Y-m-d H:i:s',
            ),
            array(
                'key' => 'field_slt_countdown_hours',
                'label' => 'Countdown Hours (Cookie-based)',
                'name' => 'countdown_hours',
                'type' => 'number',
                'instructions' => 'If no deadline set, start X hour countdown per visitor',
                'default_value' => 24,
                'min' => 1,
                'max' => 168,
            ),
            array(
                'key' => 'field_slt_countdown_text',
                'label' => 'Countdown Bar Text',
                'name' => 'countdown_bar_text',
                'type' => 'text',
                'default_value' => '🔥 LAUNCH SPECIAL: $37 price expires in',
            ),
            array(
                'key' => 'field_slt_countdown_cta_text',
                'label' => 'Countdown Bar CTA',
                'name' => 'countdown_bar_cta',
                'type' => 'text',
                'default_value' => 'GET INSTANT ACCESS →',
            ),
            array(
                'key' => 'field_slt_countdown_cta_url',
                'label' => 'Countdown Bar CTA URL',
                'name' => 'countdown_bar_cta_url',
                'type' => 'url',
            ),
            // Footer Settings
            array(
                'key' => 'field_slt_footer_tab',
                'label' => 'Footer',
                'name' => '',
                'type' => 'tab',
            ),
            array(
                'key' => 'field_slt_footer_logo',
                'label' => 'Footer Logo',
                'name' => 'footer_logo',
                'type' => 'image',
                'return_format' => 'array',
                'preview_size' => 'thumbnail',
            ),
            array(
                'key' => 'field_slt_company_name',
                'label' => 'Company Name',
                'name' => 'company_name',
                'type' => 'text',
            ),
            array(
                'key' => 'field_slt_privacy_url',
                'label' => 'Privacy Policy URL',
                'name' => 'privacy_url',
                'type' => 'url',
            ),
            array(
                'key' => 'field_slt_terms_url',
                'label' => 'Terms URL',
                'name' => 'terms_url',
                'type' => 'url',
            ),
            array(
                'key' => 'field_slt_contact_url',
                'label' => 'Contact URL',
                'name' => 'contact_url',
                'type' => 'url',
            ),
            array(
                'key' => 'field_slt_earnings_disclaimer',
                'label' => 'Earnings Disclaimer',
                'name' => 'earnings_disclaimer',
                'type' => 'textarea',
                'rows' => 4,
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'page_template',
                    'operator' => '==',
                    'value' => 'template-software-launch.php',
                ),
            ),
        ),
        'menu_order' => 0,
    ));
    
    // Field Group: Hero Section
    acf_add_local_field_group(array(
        'key' => 'group_slt_hero',
        'title' => 'Software Launch - Hero Section',
        'fields' => array(
            array(
                'key' => 'field_slt_eyebrow',
                'label' => 'Eyebrow Badge Text',
                'name' => 'eyebrow_text',
                'type' => 'text',
                'default_value' => 'NEW FOR 2026',
            ),
            array(
                'key' => 'field_slt_headline_white',
                'label' => 'Headline (White)',
                'name' => 'headline_white',
                'type' => 'text',
                'instructions' => 'First part of headline in white',
            ),
            array(
                'key' => 'field_slt_headline_accent',
                'label' => 'Headline (Orange Accent)',
                'name' => 'headline_accent',
                'type' => 'text',
                'instructions' => 'Second part of headline in orange',
            ),
            array(
                'key' => 'field_slt_subheadline',
                'label' => 'Subheadline',
                'name' => 'subheadline',
                'type' => 'textarea',
                'rows' => 2,
            ),
            array(
                'key' => 'field_slt_video_url',
                'label' => 'Video URL',
                'name' => 'video_url',
                'type' => 'url',
                'instructions' => 'YouTube, Vimeo, or direct video URL',
            ),
            array(
                'key' => 'field_slt_video_poster',
                'label' => 'Video Poster Image',
                'name' => 'video_poster',
                'type' => 'image',
                'return_format' => 'array',
                'preview_size' => 'medium',
            ),
            array(
                'key' => 'field_slt_hero_cta_text',
                'label' => 'CTA Button Text',
                'name' => 'hero_cta_text',
                'type' => 'text',
                'default_value' => 'YES! GIVE ME INSTANT ACCESS →',
            ),
            array(
                'key' => 'field_slt_hero_cta_url',
                'label' => 'CTA Button URL',
                'name' => 'hero_cta_url',
                'type' => 'url',
            ),
            array(
                'key' => 'field_slt_trust_badges',
                'label' => 'Trust Badges',
                'name' => 'trust_badges',
                'type' => 'repeater',
                'layout' => 'table',
                'button_label' => 'Add Badge',
                'sub_fields' => array(
                    array(
                        'key' => 'field_slt_trust_badge_icon',
                        'label' => 'Icon',
                        'name' => 'icon',
                        'type' => 'text',
                        'instructions' => 'Emoji or icon code',
                        'wrapper' => array('width' => '20'),
                    ),
                    array(
                        'key' => 'field_slt_trust_badge_text',
                        'label' => 'Text',
                        'name' => 'text',
                        'type' => 'text',
                        'wrapper' => array('width' => '80'),
                    ),
                ),
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'page_template',
                    'operator' => '==',
                    'value' => 'template-software-launch.php',
                ),
            ),
        ),
        'menu_order' => 1,
    ));
    
    // Field Group: Problem Section
    acf_add_local_field_group(array(
        'key' => 'group_slt_problem',
        'title' => 'Software Launch - Problem Section',
        'fields' => array(
            array(
                'key' => 'field_slt_problem_intro',
                'label' => 'Problem Introduction',
                'name' => 'problem_intro',
                'type' => 'text',
                'default_value' => "You're working harder than ever, but...",
            ),
            array(
                'key' => 'field_slt_pain_points',
                'label' => 'Pain Points',
                'name' => 'pain_points',
                'type' => 'repeater',
                'layout' => 'block',
                'button_label' => 'Add Pain Point',
                'sub_fields' => array(
                    array(
                        'key' => 'field_slt_pain_point_text',
                        'label' => 'Pain Point',
                        'name' => 'text',
                        'type' => 'text',
                    ),
                ),
            ),
            array(
                'key' => 'field_slt_problem_transition',
                'label' => 'Transition Text',
                'name' => 'problem_transition',
                'type' => 'text',
                'default_value' => "Sound familiar? There's a better way.",
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'page_template',
                    'operator' => '==',
                    'value' => 'template-software-launch.php',
                ),
            ),
        ),
        'menu_order' => 2,
    ));
    
    // Field Group: Solution Section
    acf_add_local_field_group(array(
        'key' => 'group_slt_solution',
        'title' => 'Software Launch - Solution Section',
        'fields' => array(
            array(
                'key' => 'field_slt_product_name',
                'label' => 'Product Name',
                'name' => 'product_name',
                'type' => 'text',
            ),
            array(
                'key' => 'field_slt_product_image',
                'label' => 'Product Mockup Image',
                'name' => 'product_image',
                'type' => 'image',
                'return_format' => 'array',
                'preview_size' => 'medium',
            ),
            array(
                'key' => 'field_slt_solution_tagline',
                'label' => 'Solution Tagline',
                'name' => 'solution_tagline',
                'type' => 'textarea',
                'rows' => 2,
                'instructions' => 'The first [category] that [benefit] without [pain point].',
            ),
            array(
                'key' => 'field_slt_solution_benefits',
                'label' => 'Key Benefits',
                'name' => 'solution_benefits',
                'type' => 'repeater',
                'layout' => 'block',
                'button_label' => 'Add Benefit',
                'sub_fields' => array(
                    array(
                        'key' => 'field_slt_solution_benefit_text',
                        'label' => 'Benefit',
                        'name' => 'text',
                        'type' => 'text',
                    ),
                ),
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'page_template',
                    'operator' => '==',
                    'value' => 'template-software-launch.php',
                ),
            ),
        ),
        'menu_order' => 3,
    ));
    
    // Field Group: Social Proof Section
    acf_add_local_field_group(array(
        'key' => 'group_slt_social_proof',
        'title' => 'Software Launch - Social Proof Stats',
        'fields' => array(
            array(
                'key' => 'field_slt_stats',
                'label' => 'Statistics',
                'name' => 'stats',
                'type' => 'repeater',
                'layout' => 'table',
                'button_label' => 'Add Stat',
                'max' => 4,
                'sub_fields' => array(
                    array(
                        'key' => 'field_slt_stat_value',
                        'label' => 'Value',
                        'name' => 'value',
                        'type' => 'text',
                        'instructions' => 'e.g., $2.7B+, 102K+',
                    ),
                    array(
                        'key' => 'field_slt_stat_label',
                        'label' => 'Label',
                        'name' => 'label',
                        'type' => 'text',
                        'instructions' => 'e.g., Sales, Users',
                    ),
                ),
            ),
            array(
                'key' => 'field_slt_authority_quote',
                'label' => 'Authority Quote',
                'name' => 'authority_quote',
                'type' => 'textarea',
                'rows' => 2,
            ),
            array(
                'key' => 'field_slt_authority_name',
                'label' => 'Authority Name',
                'name' => 'authority_name',
                'type' => 'text',
            ),
            array(
                'key' => 'field_slt_authority_title',
                'label' => 'Authority Title/Company',
                'name' => 'authority_title',
                'type' => 'text',
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'page_template',
                    'operator' => '==',
                    'value' => 'template-software-launch.php',
                ),
            ),
        ),
        'menu_order' => 4,
    ));
    
    // Field Group: Features Section
    acf_add_local_field_group(array(
        'key' => 'group_slt_features',
        'title' => 'Software Launch - Features Grid',
        'fields' => array(
            array(
                'key' => 'field_slt_features_headline',
                'label' => 'Section Headline',
                'name' => 'features_headline',
                'type' => 'text',
                'default_value' => 'EVERYTHING YOU GET INSIDE',
            ),
            array(
                'key' => 'field_slt_features',
                'label' => 'Features',
                'name' => 'features',
                'type' => 'repeater',
                'layout' => 'block',
                'button_label' => 'Add Feature',
                'sub_fields' => array(
                    array(
                        'key' => 'field_slt_feature_icon',
                        'label' => 'Icon',
                        'name' => 'icon',
                        'type' => 'text',
                        'instructions' => 'Emoji or icon class',
                        'wrapper' => array('width' => '20'),
                    ),
                    array(
                        'key' => 'field_slt_feature_title',
                        'label' => 'Title',
                        'name' => 'title',
                        'type' => 'text',
                        'wrapper' => array('width' => '40'),
                    ),
                    array(
                        'key' => 'field_slt_feature_description',
                        'label' => 'Description',
                        'name' => 'description',
                        'type' => 'text',
                        'wrapper' => array('width' => '40'),
                    ),
                ),
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'page_template',
                    'operator' => '==',
                    'value' => 'template-software-launch.php',
                ),
            ),
        ),
        'menu_order' => 5,
    ));
    
    // Field Group: How It Works Section
    acf_add_local_field_group(array(
        'key' => 'group_slt_how_it_works',
        'title' => 'Software Launch - How It Works',
        'fields' => array(
            array(
                'key' => 'field_slt_steps_headline',
                'label' => 'Section Headline',
                'name' => 'steps_headline',
                'type' => 'text',
                'default_value' => 'GET STARTED IN 3 SIMPLE STEPS',
            ),
            array(
                'key' => 'field_slt_steps',
                'label' => 'Steps',
                'name' => 'steps',
                'type' => 'repeater',
                'layout' => 'block',
                'button_label' => 'Add Step',
                'max' => 5,
                'sub_fields' => array(
                    array(
                        'key' => 'field_slt_step_image',
                        'label' => 'Image',
                        'name' => 'image',
                        'type' => 'image',
                        'return_format' => 'array',
                        'preview_size' => 'thumbnail',
                    ),
                    array(
                        'key' => 'field_slt_step_title',
                        'label' => 'Title',
                        'name' => 'title',
                        'type' => 'text',
                    ),
                    array(
                        'key' => 'field_slt_step_description',
                        'label' => 'Description',
                        'name' => 'description',
                        'type' => 'text',
                    ),
                ),
            ),
            array(
                'key' => 'field_slt_steps_tagline',
                'label' => 'Closing Tagline',
                'name' => 'steps_tagline',
                'type' => 'text',
                'default_value' => 'Your product goes live in 5 minutes.',
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'page_template',
                    'operator' => '==',
                    'value' => 'template-software-launch.php',
                ),
            ),
        ),
        'menu_order' => 6,
    ));
    
    // Field Group: Comparison Section
    acf_add_local_field_group(array(
        'key' => 'group_slt_comparison',
        'title' => 'Software Launch - Comparison Table',
        'fields' => array(
            array(
                'key' => 'field_slt_comparison_headline',
                'label' => 'Section Headline',
                'name' => 'comparison_headline',
                'type' => 'text',
                'default_value' => 'THE OLD WAY vs. THE NEW WAY',
            ),
            array(
                'key' => 'field_slt_comparison_without_label',
                'label' => 'Without Product Label',
                'name' => 'comparison_without_label',
                'type' => 'text',
                'default_value' => 'WITHOUT [PRODUCT]',
            ),
            array(
                'key' => 'field_slt_comparison_with_label',
                'label' => 'With Product Label',
                'name' => 'comparison_with_label',
                'type' => 'text',
                'default_value' => 'WITH [PRODUCT]',
            ),
            array(
                'key' => 'field_slt_comparison_items',
                'label' => 'Comparison Items',
                'name' => 'comparison_items',
                'type' => 'repeater',
                'layout' => 'table',
                'button_label' => 'Add Row',
                'sub_fields' => array(
                    array(
                        'key' => 'field_slt_comparison_without',
                        'label' => 'Without (Bad)',
                        'name' => 'without',
                        'type' => 'text',
                    ),
                    array(
                        'key' => 'field_slt_comparison_with',
                        'label' => 'With (Good)',
                        'name' => 'with',
                        'type' => 'text',
                    ),
                ),
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'page_template',
                    'operator' => '==',
                    'value' => 'template-software-launch.php',
                ),
            ),
        ),
        'menu_order' => 7,
    ));
    
    // Field Group: Bonuses Section
    acf_add_local_field_group(array(
        'key' => 'group_slt_bonuses',
        'title' => 'Software Launch - Bonus Stack',
        'fields' => array(
            array(
                'key' => 'field_slt_bonuses_headline',
                'label' => 'Section Headline',
                'name' => 'bonuses_headline',
                'type' => 'text',
                'default_value' => 'FAST ACTION BONUSES',
            ),
            array(
                'key' => 'field_slt_bonuses_badge',
                'label' => 'Scarcity Badge',
                'name' => 'bonuses_badge',
                'type' => 'text',
                'default_value' => '🎁 First 50 Only',
            ),
            array(
                'key' => 'field_slt_bonuses',
                'label' => 'Bonuses',
                'name' => 'bonuses',
                'type' => 'repeater',
                'layout' => 'block',
                'button_label' => 'Add Bonus',
                'sub_fields' => array(
                    array(
                        'key' => 'field_slt_bonus_title',
                        'label' => 'Title',
                        'name' => 'title',
                        'type' => 'text',
                    ),
                    array(
                        'key' => 'field_slt_bonus_description',
                        'label' => 'Description',
                        'name' => 'description',
                        'type' => 'textarea',
                        'rows' => 2,
                    ),
                    array(
                        'key' => 'field_slt_bonus_value',
                        'label' => 'Value ($)',
                        'name' => 'value',
                        'type' => 'number',
                    ),
                    array(
                        'key' => 'field_slt_bonus_image',
                        'label' => 'Image',
                        'name' => 'image',
                        'type' => 'image',
                        'return_format' => 'array',
                        'preview_size' => 'thumbnail',
                    ),
                ),
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'page_template',
                    'operator' => '==',
                    'value' => 'template-software-launch.php',
                ),
            ),
        ),
        'menu_order' => 8,
    ));
    
    // Field Group: Pricing Section
    acf_add_local_field_group(array(
        'key' => 'group_slt_pricing',
        'title' => 'Software Launch - Pricing',
        'fields' => array(
            array(
                'key' => 'field_slt_pricing_headline',
                'label' => 'Section Headline',
                'name' => 'pricing_headline',
                'type' => 'text',
                'default_value' => 'CHOOSE YOUR PACKAGE',
            ),
            // Front-end offer
            array(
                'key' => 'field_slt_fe_tab',
                'label' => 'Front-End Offer',
                'name' => '',
                'type' => 'tab',
            ),
            array(
                'key' => 'field_slt_fe_title',
                'label' => 'Title',
                'name' => 'fe_title',
                'type' => 'text',
                'default_value' => 'FRONT END',
            ),
            array(
                'key' => 'field_slt_fe_original_price',
                'label' => 'Original Price',
                'name' => 'fe_original_price',
                'type' => 'number',
            ),
            array(
                'key' => 'field_slt_fe_sale_price',
                'label' => 'Sale Price',
                'name' => 'fe_sale_price',
                'type' => 'number',
            ),
            array(
                'key' => 'field_slt_fe_price_type',
                'label' => 'Price Type',
                'name' => 'fe_price_type',
                'type' => 'select',
                'choices' => array(
                    'one-time' => 'One-Time',
                    'monthly' => 'Monthly',
                    'yearly' => 'Yearly',
                ),
                'default_value' => 'one-time',
            ),
            array(
                'key' => 'field_slt_fe_features',
                'label' => 'Features',
                'name' => 'fe_features',
                'type' => 'repeater',
                'layout' => 'table',
                'button_label' => 'Add Feature',
                'sub_fields' => array(
                    array(
                        'key' => 'field_slt_fe_feature_text',
                        'label' => 'Feature',
                        'name' => 'text',
                        'type' => 'text',
                    ),
                ),
            ),
            array(
                'key' => 'field_slt_fe_cta_text',
                'label' => 'CTA Text',
                'name' => 'fe_cta_text',
                'type' => 'text',
                'default_value' => 'GET ACCESS',
            ),
            array(
                'key' => 'field_slt_fe_cta_url',
                'label' => 'CTA URL',
                'name' => 'fe_cta_url',
                'type' => 'url',
            ),
            // Bundle offer
            array(
                'key' => 'field_slt_bundle_tab',
                'label' => 'Bundle Offer',
                'name' => '',
                'type' => 'tab',
            ),
            array(
                'key' => 'field_slt_bundle_enabled',
                'label' => 'Show Bundle?',
                'name' => 'bundle_enabled',
                'type' => 'true_false',
                'default_value' => 1,
                'ui' => 1,
            ),
            array(
                'key' => 'field_slt_bundle_badge',
                'label' => 'Badge Text',
                'name' => 'bundle_badge',
                'type' => 'text',
                'default_value' => '★ BEST VALUE ★',
            ),
            array(
                'key' => 'field_slt_bundle_title',
                'label' => 'Title',
                'name' => 'bundle_title',
                'type' => 'text',
                'default_value' => 'FULL BUNDLE',
            ),
            array(
                'key' => 'field_slt_bundle_original_price',
                'label' => 'Original Price',
                'name' => 'bundle_original_price',
                'type' => 'number',
            ),
            array(
                'key' => 'field_slt_bundle_sale_price',
                'label' => 'Sale Price',
                'name' => 'bundle_sale_price',
                'type' => 'number',
            ),
            array(
                'key' => 'field_slt_bundle_price_type',
                'label' => 'Price Type',
                'name' => 'bundle_price_type',
                'type' => 'select',
                'choices' => array(
                    'one-time' => 'One-Time',
                    'monthly' => 'Monthly',
                    'yearly' => 'Yearly',
                ),
                'default_value' => 'one-time',
            ),
            array(
                'key' => 'field_slt_bundle_features',
                'label' => 'Features',
                'name' => 'bundle_features',
                'type' => 'repeater',
                'layout' => 'table',
                'button_label' => 'Add Feature',
                'sub_fields' => array(
                    array(
                        'key' => 'field_slt_bundle_feature_text',
                        'label' => 'Feature',
                        'name' => 'text',
                        'type' => 'text',
                    ),
                ),
            ),
            array(
                'key' => 'field_slt_bundle_cta_text',
                'label' => 'CTA Text',
                'name' => 'bundle_cta_text',
                'type' => 'text',
                'default_value' => 'GET BUNDLE',
            ),
            array(
                'key' => 'field_slt_bundle_cta_url',
                'label' => 'CTA URL',
                'name' => 'bundle_cta_url',
                'type' => 'url',
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'page_template',
                    'operator' => '==',
                    'value' => 'template-software-launch.php',
                ),
            ),
        ),
        'menu_order' => 9,
    ));
    
    // Field Group: Testimonials Section
    acf_add_local_field_group(array(
        'key' => 'group_slt_testimonials',
        'title' => 'Software Launch - Testimonials',
        'fields' => array(
            array(
                'key' => 'field_slt_testimonials_headline',
                'label' => 'Section Headline',
                'name' => 'testimonials_headline',
                'type' => 'text',
                'default_value' => 'WHAT OTHERS ARE SAYING',
            ),
            array(
                'key' => 'field_slt_testimonials',
                'label' => 'Testimonials',
                'name' => 'testimonials',
                'type' => 'repeater',
                'layout' => 'block',
                'button_label' => 'Add Testimonial',
                'sub_fields' => array(
                    array(
                        'key' => 'field_slt_testimonial_photo',
                        'label' => 'Photo',
                        'name' => 'photo',
                        'type' => 'image',
                        'return_format' => 'array',
                        'preview_size' => 'thumbnail',
                    ),
                    array(
                        'key' => 'field_slt_testimonial_quote',
                        'label' => 'Quote',
                        'name' => 'quote',
                        'type' => 'textarea',
                        'rows' => 3,
                    ),
                    array(
                        'key' => 'field_slt_testimonial_name',
                        'label' => 'Name',
                        'name' => 'name',
                        'type' => 'text',
                    ),
                    array(
                        'key' => 'field_slt_testimonial_title',
                        'label' => 'Title/Role',
                        'name' => 'title',
                        'type' => 'text',
                    ),
                    array(
                        'key' => 'field_slt_testimonial_rating',
                        'label' => 'Rating (1-5)',
                        'name' => 'rating',
                        'type' => 'number',
                        'min' => 1,
                        'max' => 5,
                        'default_value' => 5,
                    ),
                ),
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'page_template',
                    'operator' => '==',
                    'value' => 'template-software-launch.php',
                ),
            ),
        ),
        'menu_order' => 10,
    ));
    
    // Field Group: FAQ Section
    acf_add_local_field_group(array(
        'key' => 'group_slt_faq',
        'title' => 'Software Launch - FAQ',
        'fields' => array(
            array(
                'key' => 'field_slt_faq_headline',
                'label' => 'Section Headline',
                'name' => 'faq_headline',
                'type' => 'text',
                'default_value' => 'FREQUENTLY ASKED QUESTIONS',
            ),
            array(
                'key' => 'field_slt_faqs',
                'label' => 'FAQs',
                'name' => 'faqs',
                'type' => 'repeater',
                'layout' => 'block',
                'button_label' => 'Add FAQ',
                'sub_fields' => array(
                    array(
                        'key' => 'field_slt_faq_question',
                        'label' => 'Question',
                        'name' => 'question',
                        'type' => 'text',
                    ),
                    array(
                        'key' => 'field_slt_faq_answer',
                        'label' => 'Answer',
                        'name' => 'answer',
                        'type' => 'wysiwyg',
                        'tabs' => 'visual',
                        'toolbar' => 'basic',
                        'media_upload' => 0,
                    ),
                ),
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'page_template',
                    'operator' => '==',
                    'value' => 'template-software-launch.php',
                ),
            ),
        ),
        'menu_order' => 11,
    ));
    
    // Field Group: Final CTA Section
    acf_add_local_field_group(array(
        'key' => 'group_slt_final_cta',
        'title' => 'Software Launch - Final CTA',
        'fields' => array(
            array(
                'key' => 'field_slt_final_headline',
                'label' => 'Headline',
                'name' => 'final_headline',
                'type' => 'text',
                'default_value' => '⏰ THIS OFFER EXPIRES WHEN THE TIMER HITS ZERO',
            ),
            array(
                'key' => 'field_slt_final_urgency_text',
                'label' => 'Urgency Text',
                'name' => 'final_urgency_text',
                'type' => 'text',
                'default_value' => 'After that, the price goes to $97/month.',
            ),
            array(
                'key' => 'field_slt_final_cta_text',
                'label' => 'CTA Text',
                'name' => 'final_cta_text',
                'type' => 'text',
                'default_value' => 'YES! GIVE ME INSTANT ACCESS FOR $37 →',
            ),
            array(
                'key' => 'field_slt_final_cta_url',
                'label' => 'CTA URL',
                'name' => 'final_cta_url',
                'type' => 'url',
            ),
            array(
                'key' => 'field_slt_final_benefits',
                'label' => 'Benefit Checkmarks',
                'name' => 'final_benefits',
                'type' => 'repeater',
                'layout' => 'table',
                'button_label' => 'Add Benefit',
                'sub_fields' => array(
                    array(
                        'key' => 'field_slt_final_benefit_text',
                        'label' => 'Benefit',
                        'name' => 'text',
                        'type' => 'text',
                    ),
                ),
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'page_template',
                    'operator' => '==',
                    'value' => 'template-software-launch.php',
                ),
            ),
        ),
        'menu_order' => 12,
    ));
}
add_action('acf/init', 'slt_register_acf_fields');

/**
 * Helper: Get star rating HTML
 */
function slt_get_stars($rating = 5) {
    $rating = max(1, min(5, intval($rating)));
    $stars = '';
    for ($i = 0; $i < $rating; $i++) {
        $stars .= '⭐';
    }
    return $stars;
}

/**
 * Helper: Format price
 */
function slt_format_price($price) {
    return '$' . number_format(floatval($price), 0);
}

/**
 * Helper: Get video embed
 */
function slt_get_video_embed($url) {
    if (empty($url)) {
        return '';
    }
    
    // YouTube
    if (preg_match('/youtube\.com\/watch\?v=([^\&\?\/]+)/', $url, $matches) || 
        preg_match('/youtu\.be\/([^\&\?\/]+)/', $url, $matches)) {
        $video_id = $matches[1];
        return '<iframe src="https://www.youtube.com/embed/' . esc_attr($video_id) . '?rel=0" frameborder="0" allowfullscreen allow="autoplay; encrypted-media"></iframe>';
    }
    
    // Vimeo
    if (preg_match('/vimeo\.com\/(\d+)/', $url, $matches)) {
        $video_id = $matches[1];
        return '<iframe src="https://player.vimeo.com/video/' . esc_attr($video_id) . '" frameborder="0" allowfullscreen></iframe>';
    }
    
    // Direct video
    $ext = pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_EXTENSION);
    if (in_array($ext, array('mp4', 'webm', 'ogg'))) {
        return '<video controls><source src="' . esc_url($url) . '" type="video/' . esc_attr($ext) . '"></video>';
    }
    
    return '';
}

/**
 * ACF Admin Notice
 */
function slt_acf_admin_notice() {
    if (!function_exists('acf_add_local_field_group')) {
        ?>
        <div class="notice notice-warning">
            <p><strong>Software Launch Theme:</strong> Advanced Custom Fields Pro is required for full functionality. <a href="https://www.advancedcustomfields.com/pro/" target="_blank">Get ACF Pro</a></p>
        </div>
        <?php
    }
}
add_action('admin_notices', 'slt_acf_admin_notice');

/**
 * Disable Gutenberg for this template
 */
function slt_disable_gutenberg($use_block_editor, $post) {
    if (!empty($post)) {
        $template = get_page_template_slug($post->ID);
        if ($template === 'template-software-launch.php') {
            return false;
        }
    }
    return $use_block_editor;
}
add_filter('use_block_editor_for_post', 'slt_disable_gutenberg', 10, 2);

/**
 * Add body class for template
 */
function slt_body_class($classes) {
    if (is_page_template('template-software-launch.php')) {
        $classes[] = 'software-launch-page';
    }
    return $classes;
}
add_filter('body_class', 'slt_body_class');

/**
 * REST API Endpoint for headless usage
 */
function slt_register_rest_routes() {
    register_rest_route('slt/v1', '/page/(?P<id>\d+)', array(
        'methods' => 'GET',
        'callback' => 'slt_get_page_data',
        'permission_callback' => '__return_true',
        'args' => array(
            'id' => array(
                'validate_callback' => function($param) {
                    return is_numeric($param);
                }
            ),
        ),
    ));
}
add_action('rest_api_init', 'slt_register_rest_routes');

/**
 * REST API callback - return all ACF fields
 */
function slt_get_page_data($request) {
    $page_id = $request['id'];
    $page = get_post($page_id);
    
    if (!$page || $page->post_type !== 'page') {
        return new WP_Error('invalid_page', 'Page not found', array('status' => 404));
    }
    
    $fields = get_fields($page_id);
    
    return array(
        'id' => $page_id,
        'title' => $page->post_title,
        'slug' => $page->post_name,
        'template' => get_page_template_slug($page_id),
        'fields' => $fields ?: array(),
    );
}
