<?php
/**
 * Plugin Name: Lead Intake Form (MVP)
 * Description: Simple lead intake form with admin review.
 */

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

function fl_register_lead_cpt() {
    register_post_type('fl_lead', [
        'labels' => [
            'name' => 'Leads',
            'singular_name' => 'Lead',
        ],
        'public' => false,
        'show_ui' => true,
        'menu_icon' => 'dashicons-groups',
        'supports' => ['title', 'editor', 'custom-fields'],
    ]);
}
add_action('init', 'fl_register_lead_cpt');

function fl_lead_form_shortcode() {
    if (!empty($_POST['fl_lead_nonce']) && wp_verify_nonce($_POST['fl_lead_nonce'], 'fl_lead_submit')) {
        $name = sanitize_text_field($_POST['fl_name'] ?? '');
        $email = sanitize_email($_POST['fl_email'] ?? '');
        $role = sanitize_text_field($_POST['fl_role'] ?? '');
        $focus = sanitize_textarea_field($_POST['fl_focus'] ?? '');
        $q1 = sanitize_textarea_field($_POST['fl_q1'] ?? '');
        $q2 = sanitize_textarea_field($_POST['fl_q2'] ?? '');
        $q3 = sanitize_textarea_field($_POST['fl_q3'] ?? '');
        $q4 = sanitize_textarea_field($_POST['fl_q4'] ?? '');
        $q5 = sanitize_textarea_field($_POST['fl_q5'] ?? '');
        $agreement = isset($_POST['fl_agreement']) ? 'Agreed' : 'Not Agreed';

        if ($name && $email) {
            $lead_id = wp_insert_post([
                'post_type' => 'fl_lead',
                'post_status' => 'publish',
                'post_title' => $name . ' — ' . $email,
                'post_content' => "Role/Context: $role\n\nFocus: $focus\n\n1. Tell me about yourself: $q1\n\n2. Signature strengths (friends): $q2\n\n3. Signature strengths (yourself): $q3\n\n4. Zone of genius (lose track of time): $q4\n\n5. Time you transformed yourself: $q5\n\nCoaching Agreement: $agreement",
            ]);
            if ($lead_id) {
                return '<div class="fl-success">Thank you — your request was received. I will reply within 1–2 business days.</div>';
            }
        }
        return '<div class="fl-error">Please complete required fields and try again.</div>';
    }

    ob_start();
    ?>
    <form class="fl-lead-form" method="post">
        <?php wp_nonce_field('fl_lead_submit', 'fl_lead_nonce'); ?>
        <p><label>Full name*</label><br><input type="text" name="fl_name" required></p>
        <p><label>Email*</label><br><input type="email" name="fl_email" required></p>
        <p><label>Role / Company</label><br><input type="text" name="fl_role"></p>
        <p><label>What would you like to work on?*</label><br><textarea name="fl_focus" rows="4" required></textarea></p>
        <p><label>1. Tell me about yourself</label><br><textarea name="fl_q1" rows="2"></textarea></p>
        <p><label>2. What would your friends say are your top 3 signature strengths?</label><br><textarea name="fl_q2" rows="2"></textarea></p>
        <p><label>3. What would you say are your top 3 signature strengths?</label><br><textarea name="fl_q3" rows="2"></textarea></p>
        <p><label>4. What are you doing at work when you lose track of time (in the zone)?</label><br><textarea name="fl_q4" rows="2"></textarea></p>
        <p><label>5. Tell me about a time when you personally or professionally transformed yourself, what did you do, what was the outcome?</label><br><textarea name="fl_q5" rows="4"></textarea></p>
        <p><label><input type="checkbox" name="fl_agreement" required> I agree to the <a href="/pricing/#agreement" target="_blank">Coaching Agreement</a> (Liability, Confidentiality, Non-Compete)</label></p>
        <p><button type="submit">Request a coaching conversation</button></p>
    </form>
    <?php
    return ob_get_clean();
}
add_shortcode('lead_form', 'fl_lead_form_shortcode');

function fl_lead_form_styles() {
    echo '<style>
    .fl-lead-form input, .fl-lead-form textarea { width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid #cbd5e1; }
    .fl-lead-form button { background:#0f172a; color:#fff; padding:12px 18px; border-radius:999px; border:none; }
    .fl-success { padding:12px 16px; background:#dcfce7; border:1px solid #86efac; border-radius:10px; }
    .fl-error { padding:12px 16px; background:#fee2e2; border:1px solid #fca5a5; border-radius:10px; }
    </style>';
}
add_action('wp_head', 'fl_lead_form_styles');
