<?php
/**
 * Plugin Name: Workshop Headless CMS
 * Description: Exposes all workshop content via REST API for headless frontend
 * Version: 1.0.0
 */

// Enable CORS for headless
add_action('rest_api_init', function() {
    remove_filter('rest_pre_serve_request', 'rest_send_cors_headers');
    add_filter('rest_pre_serve_request', function($value) {
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
        header('Access-Control-Allow-Headers: Content-Type, Authorization, X-WP-Nonce');
        return $value;
    });
}, 15);

// Register custom REST endpoint for workshop content
add_action('rest_api_init', function() {
    register_rest_route('workshop/v1', '/content', array(
        'methods' => 'GET',
        'callback' => 'get_workshop_content',
        'permission_callback' => '__return_true',
    ));
});

function get_workshop_content() {
    // Get the workshop page
    $page = get_page_by_path('workshop-content');
    if (!$page) {
        $page_id = get_option('workshop_content_page_id', 0);
    } else {
        $page_id = $page->ID;
    }
    
    // Get all ACF fields
    $fields = get_fields($page_id);
    
    if (empty($fields)) {
        return new WP_REST_Response(get_default_workshop_content(), 200);
    }
    
    return new WP_REST_Response($fields, 200);
}

function get_default_workshop_content() {
    return array(
        'hero_badge' => 'Free Live Training',
        'hero_headline' => 'How to Generate <span class="text-accent">100 Qualified Leads Per Day</span> on Autopilot',
        'hero_subheadline' => 'Discover the exact 5-step framework used by 50,000+ professionals to build predictable revenue — without paid ads or cold calling.',
        'hero_date_time' => 'Thursday, June 15th at 2:00 PM EST',
        'hero_video_url' => '',
        'pain_headline' => 'Does This Sound Familiar?',
        'pain_subheadline' => "You're working harder than ever, but the results aren't matching the effort...",
        'pain_points' => array(
            array('text' => "You're spending thousands on ads that barely break even — or worse, lose money"),
            array('text' => "Your calendar is empty while competitors seem to have endless leads"),
            array('text' => "You've tried \"proven\" strategies that worked for everyone except you"),
            array('text' => "The feast-or-famine cycle keeps you stressed and unable to plan ahead"),
            array('text' => "You're stuck trading time for money with no clear path to scale"),
        ),
        'pain_cta_text' => 'If any of these resonate, this workshop was made for you.',
    );
}

// ACF JSON save point
add_filter('acf/settings/save_json', function($path) {
    return plugin_dir_path(__FILE__) . 'acf-json';
});

// ACF JSON load point
add_filter('acf/settings/load_json', function($paths) {
    $paths[] = plugin_dir_path(__FILE__) . 'acf-json';
    return $paths;
});

// Create workshop content page on activation
register_activation_hook(__FILE__, 'workshop_headless_activate');

function workshop_headless_activate() {
    $page = get_page_by_path('workshop-content');
    if (!$page) {
        $page_id = wp_insert_post(array(
            'post_title' => 'Workshop Content',
            'post_name' => 'workshop-content',
            'post_status' => 'publish',
            'post_type' => 'page',
        ));
        update_option('workshop_content_page_id', $page_id);
    }
}

// Register ACF field groups programmatically
add_action('acf/init', 'workshop_register_acf_fields');

function workshop_register_acf_fields() {
    if (!function_exists('acf_add_local_field_group')) {
        return;
    }
    
    // Hero Section
    acf_add_local_field_group(array(
        'key' => 'group_workshop_hero',
        'title' => 'Workshop - Hero Section',
        'fields' => array(
            array(
                'key' => 'field_hero_badge',
                'label' => 'Badge Text',
                'name' => 'hero_badge',
                'type' => 'text',
                'default_value' => 'Free Live Training',
            ),
            array(
                'key' => 'field_hero_headline',
                'label' => 'Headline (HTML allowed)',
                'name' => 'hero_headline',
                'type' => 'textarea',
                'rows' => 2,
            ),
            array(
                'key' => 'field_hero_subheadline',
                'label' => 'Subheadline',
                'name' => 'hero_subheadline',
                'type' => 'textarea',
                'rows' => 3,
            ),
            array(
                'key' => 'field_hero_date_time',
                'label' => 'Date/Time Display',
                'name' => 'hero_date_time',
                'type' => 'text',
            ),
            array(
                'key' => 'field_hero_video_url',
                'label' => 'Video URL (YouTube/Vimeo)',
                'name' => 'hero_video_url',
                'type' => 'url',
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'page',
                    'operator' => '==',
                    'value' => 'workshop-content',
                ),
            ),
        ),
        'menu_order' => 0,
    ));

    // Pain Section
    acf_add_local_field_group(array(
        'key' => 'group_workshop_pain',
        'title' => 'Workshop - Pain Section',
        'fields' => array(
            array(
                'key' => 'field_pain_headline',
                'label' => 'Section Headline',
                'name' => 'pain_headline',
                'type' => 'text',
            ),
            array(
                'key' => 'field_pain_subheadline',
                'label' => 'Section Subheadline',
                'name' => 'pain_subheadline',
                'type' => 'textarea',
            ),
            array(
                'key' => 'field_pain_points',
                'label' => 'Pain Points',
                'name' => 'pain_points',
                'type' => 'repeater',
                'layout' => 'table',
                'button_label' => 'Add Pain Point',
                'sub_fields' => array(
                    array(
                        'key' => 'field_pain_point_text',
                        'label' => 'Pain Point',
                        'name' => 'text',
                        'type' => 'textarea',
                        'rows' => 2,
                    ),
                ),
            ),
            array(
                'key' => 'field_pain_cta_text',
                'label' => 'CTA Text',
                'name' => 'pain_cta_text',
                'type' => 'text',
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'page',
                    'operator' => '==',
                    'value' => 'workshop-content',
                ),
            ),
        ),
        'menu_order' => 1,
    ));

    // Benefits Section
    acf_add_local_field_group(array(
        'key' => 'group_workshop_benefits',
        'title' => 'Workshop - Benefits Section',
        'fields' => array(
            array(
                'key' => 'field_benefits_headline',
                'label' => 'Section Headline',
                'name' => 'benefits_headline',
                'type' => 'text',
            ),
            array(
                'key' => 'field_benefits_cards',
                'label' => 'Benefit Cards',
                'name' => 'benefits_cards',
                'type' => 'repeater',
                'layout' => 'block',
                'button_label' => 'Add Benefit Card',
                'sub_fields' => array(
                    array(
                        'key' => 'field_benefit_icon',
                        'label' => 'Icon (Emoji)',
                        'name' => 'icon',
                        'type' => 'text',
                    ),
                    array(
                        'key' => 'field_benefit_title',
                        'label' => 'Title',
                        'name' => 'title',
                        'type' => 'text',
                    ),
                    array(
                        'key' => 'field_benefit_description',
                        'label' => 'Description',
                        'name' => 'description',
                        'type' => 'textarea',
                        'rows' => 2,
                    ),
                ),
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'page',
                    'operator' => '==',
                    'value' => 'workshop-content',
                ),
            ),
        ),
        'menu_order' => 2,
    ));

    // Curriculum Section
    acf_add_local_field_group(array(
        'key' => 'group_workshop_curriculum',
        'title' => 'Workshop - Curriculum Section',
        'fields' => array(
            array(
                'key' => 'field_curriculum_headline',
                'label' => 'Section Headline',
                'name' => 'curriculum_headline',
                'type' => 'text',
            ),
            array(
                'key' => 'field_curriculum_subheadline',
                'label' => 'Section Subheadline',
                'name' => 'curriculum_subheadline',
                'type' => 'text',
            ),
            array(
                'key' => 'field_curriculum_modules',
                'label' => 'Modules',
                'name' => 'curriculum_modules',
                'type' => 'repeater',
                'layout' => 'block',
                'button_label' => 'Add Module',
                'sub_fields' => array(
                    array(
                        'key' => 'field_module_title',
                        'label' => 'Module Title',
                        'name' => 'title',
                        'type' => 'text',
                    ),
                    array(
                        'key' => 'field_module_bullets',
                        'label' => 'Bullets',
                        'name' => 'bullets',
                        'type' => 'repeater',
                        'layout' => 'table',
                        'button_label' => 'Add Bullet',
                        'sub_fields' => array(
                            array(
                                'key' => 'field_bullet_text',
                                'label' => 'Bullet Text',
                                'name' => 'text',
                                'type' => 'text',
                            ),
                        ),
                    ),
                ),
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'page',
                    'operator' => '==',
                    'value' => 'workshop-content',
                ),
            ),
        ),
        'menu_order' => 3,
    ));

    // Audience Section
    acf_add_local_field_group(array(
        'key' => 'group_workshop_audience',
        'title' => 'Workshop - Audience Section',
        'fields' => array(
            array(
                'key' => 'field_audience_headline',
                'label' => 'Section Headline',
                'name' => 'audience_headline',
                'type' => 'text',
            ),
            array(
                'key' => 'field_audience_types',
                'label' => 'Audience Types',
                'name' => 'audience_types',
                'type' => 'repeater',
                'layout' => 'block',
                'button_label' => 'Add Audience Type',
                'sub_fields' => array(
                    array(
                        'key' => 'field_audience_title',
                        'label' => 'Title',
                        'name' => 'title',
                        'type' => 'text',
                    ),
                    array(
                        'key' => 'field_audience_description',
                        'label' => 'Description',
                        'name' => 'description',
                        'type' => 'textarea',
                        'rows' => 2,
                    ),
                ),
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'page',
                    'operator' => '==',
                    'value' => 'workshop-content',
                ),
            ),
        ),
        'menu_order' => 4,
    ));

    // Presenter Section
    acf_add_local_field_group(array(
        'key' => 'group_workshop_presenter',
        'title' => 'Workshop - Presenter Section',
        'fields' => array(
            array(
                'key' => 'field_presenter_name',
                'label' => 'Name',
                'name' => 'presenter_name',
                'type' => 'text',
            ),
            array(
                'key' => 'field_presenter_title',
                'label' => 'Title',
                'name' => 'presenter_title',
                'type' => 'text',
            ),
            array(
                'key' => 'field_presenter_bio',
                'label' => 'Bio (one paragraph per line)',
                'name' => 'presenter_bio',
                'type' => 'textarea',
                'rows' => 5,
            ),
            array(
                'key' => 'field_presenter_photo',
                'label' => 'Photo',
                'name' => 'presenter_photo',
                'type' => 'image',
                'return_format' => 'url',
            ),
            array(
                'key' => 'field_presenter_stats',
                'label' => 'Stats',
                'name' => 'presenter_stats',
                'type' => 'repeater',
                'layout' => 'table',
                'button_label' => 'Add Stat',
                'sub_fields' => array(
                    array(
                        'key' => 'field_pstat_value',
                        'label' => 'Value',
                        'name' => 'value',
                        'type' => 'text',
                    ),
                    array(
                        'key' => 'field_pstat_label',
                        'label' => 'Label',
                        'name' => 'label',
                        'type' => 'text',
                    ),
                ),
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'page',
                    'operator' => '==',
                    'value' => 'workshop-content',
                ),
            ),
        ),
        'menu_order' => 5,
    ));

    // Social Proof Section
    acf_add_local_field_group(array(
        'key' => 'group_workshop_social_proof',
        'title' => 'Workshop - Social Proof Section',
        'fields' => array(
            array(
                'key' => 'field_social_headline',
                'label' => 'Section Headline',
                'name' => 'social_headline',
                'type' => 'text',
            ),
            array(
                'key' => 'field_social_stats',
                'label' => 'Stats',
                'name' => 'social_stats',
                'type' => 'repeater',
                'layout' => 'table',
                'button_label' => 'Add Stat',
                'sub_fields' => array(
                    array(
                        'key' => 'field_sstat_number',
                        'label' => 'Number',
                        'name' => 'number',
                        'type' => 'text',
                    ),
                    array(
                        'key' => 'field_sstat_label',
                        'label' => 'Label',
                        'name' => 'label',
                        'type' => 'text',
                    ),
                ),
            ),
            array(
                'key' => 'field_testimonials',
                'label' => 'Testimonials',
                'name' => 'testimonials',
                'type' => 'repeater',
                'layout' => 'block',
                'button_label' => 'Add Testimonial',
                'sub_fields' => array(
                    array(
                        'key' => 'field_testimonial_quote',
                        'label' => 'Quote',
                        'name' => 'quote',
                        'type' => 'textarea',
                        'rows' => 3,
                    ),
                    array(
                        'key' => 'field_testimonial_name',
                        'label' => 'Name',
                        'name' => 'name',
                        'type' => 'text',
                    ),
                    array(
                        'key' => 'field_testimonial_role',
                        'label' => 'Role',
                        'name' => 'role',
                        'type' => 'text',
                    ),
                    array(
                        'key' => 'field_testimonial_initials',
                        'label' => 'Initials (2 letters)',
                        'name' => 'initials',
                        'type' => 'text',
                    ),
                ),
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'page',
                    'operator' => '==',
                    'value' => 'workshop-content',
                ),
            ),
        ),
        'menu_order' => 6,
    ));

    // Event Details Section
    acf_add_local_field_group(array(
        'key' => 'group_workshop_event',
        'title' => 'Workshop - Event Details',
        'fields' => array(
            array(
                'key' => 'field_event_date',
                'label' => 'Event Date',
                'name' => 'event_date',
                'type' => 'text',
            ),
            array(
                'key' => 'field_event_time',
                'label' => 'Event Time',
                'name' => 'event_time',
                'type' => 'text',
            ),
            array(
                'key' => 'field_event_duration',
                'label' => 'Duration',
                'name' => 'event_duration',
                'type' => 'text',
            ),
            array(
                'key' => 'field_event_platform',
                'label' => 'Platform',
                'name' => 'event_platform',
                'type' => 'text',
            ),
            array(
                'key' => 'field_event_note',
                'label' => 'Note Text',
                'name' => 'event_note',
                'type' => 'text',
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'page',
                    'operator' => '==',
                    'value' => 'workshop-content',
                ),
            ),
        ),
        'menu_order' => 7,
    ));

    // Bonuses Section
    acf_add_local_field_group(array(
        'key' => 'group_workshop_bonuses',
        'title' => 'Workshop - Bonuses Section',
        'fields' => array(
            array(
                'key' => 'field_bonuses_badge',
                'label' => 'Badge Text',
                'name' => 'bonuses_badge',
                'type' => 'text',
            ),
            array(
                'key' => 'field_bonuses_headline',
                'label' => 'Section Headline',
                'name' => 'bonuses_headline',
                'type' => 'text',
            ),
            array(
                'key' => 'field_bonuses_items',
                'label' => 'Bonus Items',
                'name' => 'bonuses_items',
                'type' => 'repeater',
                'layout' => 'block',
                'button_label' => 'Add Bonus',
                'sub_fields' => array(
                    array(
                        'key' => 'field_bonus_icon',
                        'label' => 'Icon (Emoji)',
                        'name' => 'icon',
                        'type' => 'text',
                    ),
                    array(
                        'key' => 'field_bonus_title',
                        'label' => 'Title',
                        'name' => 'title',
                        'type' => 'text',
                    ),
                    array(
                        'key' => 'field_bonus_description',
                        'label' => 'Description',
                        'name' => 'description',
                        'type' => 'textarea',
                        'rows' => 2,
                    ),
                    array(
                        'key' => 'field_bonus_value',
                        'label' => 'Value (e.g., $297 Value)',
                        'name' => 'value',
                        'type' => 'text',
                    ),
                ),
            ),
            array(
                'key' => 'field_bonuses_total_value',
                'label' => 'Total Bonus Value',
                'name' => 'bonuses_total_value',
                'type' => 'text',
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'page',
                    'operator' => '==',
                    'value' => 'workshop-content',
                ),
            ),
        ),
        'menu_order' => 8,
    ));

    // Urgency Section
    acf_add_local_field_group(array(
        'key' => 'group_workshop_urgency',
        'title' => 'Workshop - Urgency Section',
        'fields' => array(
            array(
                'key' => 'field_urgency_headline',
                'label' => 'Headline',
                'name' => 'urgency_headline',
                'type' => 'text',
            ),
            array(
                'key' => 'field_urgency_subheadline',
                'label' => 'Subheadline',
                'name' => 'urgency_subheadline',
                'type' => 'text',
            ),
            array(
                'key' => 'field_urgency_seats',
                'label' => 'Seats Remaining Text',
                'name' => 'urgency_seats',
                'type' => 'text',
            ),
            array(
                'key' => 'field_countdown_end_date',
                'label' => 'Countdown End Date',
                'name' => 'countdown_end_date',
                'type' => 'date_time_picker',
                'display_format' => 'Y-m-d H:i:s',
                'return_format' => 'Y-m-d H:i:s',
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'page',
                    'operator' => '==',
                    'value' => 'workshop-content',
                ),
            ),
        ),
        'menu_order' => 9,
    ));

    // Registration Form Section
    acf_add_local_field_group(array(
        'key' => 'group_workshop_form',
        'title' => 'Workshop - Registration Form',
        'fields' => array(
            array(
                'key' => 'field_form_headline',
                'label' => 'Headline',
                'name' => 'form_headline',
                'type' => 'text',
            ),
            array(
                'key' => 'field_form_subheadline',
                'label' => 'Subheadline',
                'name' => 'form_subheadline',
                'type' => 'text',
            ),
            array(
                'key' => 'field_form_button_text',
                'label' => 'Button Text',
                'name' => 'form_button_text',
                'type' => 'text',
            ),
            array(
                'key' => 'field_form_privacy_text',
                'label' => 'Privacy Text',
                'name' => 'form_privacy_text',
                'type' => 'text',
            ),
            array(
                'key' => 'field_form_action_url',
                'label' => 'Form Action URL',
                'name' => 'form_action_url',
                'type' => 'url',
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'page',
                    'operator' => '==',
                    'value' => 'workshop-content',
                ),
            ),
        ),
        'menu_order' => 10,
    ));

    // FAQ Section
    acf_add_local_field_group(array(
        'key' => 'group_workshop_faq',
        'title' => 'Workshop - FAQ Section',
        'fields' => array(
            array(
                'key' => 'field_faq_headline',
                'label' => 'Section Headline',
                'name' => 'faq_headline',
                'type' => 'text',
            ),
            array(
                'key' => 'field_faq_items',
                'label' => 'FAQ Items',
                'name' => 'faq_items',
                'type' => 'repeater',
                'layout' => 'block',
                'button_label' => 'Add FAQ',
                'sub_fields' => array(
                    array(
                        'key' => 'field_faq_question',
                        'label' => 'Question',
                        'name' => 'question',
                        'type' => 'text',
                    ),
                    array(
                        'key' => 'field_faq_answer',
                        'label' => 'Answer',
                        'name' => 'answer',
                        'type' => 'textarea',
                        'rows' => 4,
                    ),
                ),
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'page',
                    'operator' => '==',
                    'value' => 'workshop-content',
                ),
            ),
        ),
        'menu_order' => 11,
    ));

    // Final CTA Section
    acf_add_local_field_group(array(
        'key' => 'group_workshop_final_cta',
        'title' => 'Workshop - Final CTA',
        'fields' => array(
            array(
                'key' => 'field_final_headline',
                'label' => 'Headline',
                'name' => 'final_headline',
                'type' => 'text',
            ),
            array(
                'key' => 'field_final_subheadline',
                'label' => 'Subheadline',
                'name' => 'final_subheadline',
                'type' => 'text',
            ),
            array(
                'key' => 'field_final_button_text',
                'label' => 'Button Text',
                'name' => 'final_button_text',
                'type' => 'text',
            ),
            array(
                'key' => 'field_final_benefits',
                'label' => 'Benefit Tags',
                'name' => 'final_benefits',
                'type' => 'repeater',
                'layout' => 'table',
                'button_label' => 'Add Tag',
                'sub_fields' => array(
                    array(
                        'key' => 'field_final_benefit_text',
                        'label' => 'Text',
                        'name' => 'text',
                        'type' => 'text',
                    ),
                ),
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'page',
                    'operator' => '==',
                    'value' => 'workshop-content',
                ),
            ),
        ),
        'menu_order' => 12,
    ));

    // Footer Section
    acf_add_local_field_group(array(
        'key' => 'group_workshop_footer',
        'title' => 'Workshop - Footer',
        'fields' => array(
            array(
                'key' => 'field_footer_links',
                'label' => 'Footer Links',
                'name' => 'footer_links',
                'type' => 'repeater',
                'layout' => 'table',
                'button_label' => 'Add Link',
                'sub_fields' => array(
                    array(
                        'key' => 'field_footer_link_text',
                        'label' => 'Text',
                        'name' => 'text',
                        'type' => 'text',
                    ),
                    array(
                        'key' => 'field_footer_link_url',
                        'label' => 'URL',
                        'name' => 'url',
                        'type' => 'url',
                    ),
                ),
            ),
            array(
                'key' => 'field_footer_copyright',
                'label' => 'Copyright Text',
                'name' => 'footer_copyright',
                'type' => 'text',
            ),
            array(
                'key' => 'field_footer_disclaimer',
                'label' => 'Disclaimer',
                'name' => 'footer_disclaimer',
                'type' => 'textarea',
                'rows' => 4,
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'page',
                    'operator' => '==',
                    'value' => 'workshop-content',
                ),
            ),
        ),
        'menu_order' => 13,
    ));
}

// Add admin menu link to content page
add_action('admin_menu', function() {
    add_menu_page(
        'Workshop Content',
        'Workshop Content',
        'edit_pages',
        'workshop-content-edit',
        function() {
            $page = get_page_by_path('workshop-content');
            if ($page) {
                wp_redirect(admin_url('post.php?post=' . $page->ID . '&action=edit'));
                exit;
            } else {
                echo '<div class="wrap"><h1>Workshop Content</h1><p>Please activate the plugin to create the content page.</p></div>';
            }
        },
        'dashicons-megaphone',
        30
    );
});
