<?php
declare(strict_types=1);
// ==================================================
// Nuclear Fancy Pants Edition™ — NUCLEAR+++
// File: templates/overview.php
// Description: Overview tab — adaptive hero, onboarding steps, stats, license modal
// Last Edited: 2026-03-28
// ==================================================
if (!defined('ABSPATH')) {
    exit;
}

$is_inactive   = ($license_status === 'inactive' || $license_status === 'deactivated');
$is_free       = ($license_status === 'free');
$is_paid       = ($license_status === 'active');
$has_license   = !$is_inactive;
$has_ai        = ($active_ai_count > 0);
$current_plan  = get_option('automarketer_ai_plan_id', 'free');

// Facebook connection state
$fb_connection = \AutoMarketerAI\FacebookAuth::getConnection();
$has_facebook  = $fb_connection !== null;
$fb_page_name  = $has_facebook ? esc_html((string) ($fb_connection['page_name'] ?? '')) : '';

$plan_labels = [
    'free'         => 'Free Plan',
    'starter'      => 'Starter',
    'starter_pro'  => 'Starter Pro',
    'unlimited_ai' => 'Unlimited AI',
    'agency'       => 'Agency',
];
$plan_label = $plan_labels[$current_plan] ?? ucfirst($current_plan);

// Hero state — 5 distinct states driving copy and CTA
if ($is_inactive) {
    $hero_state = 'inactive';
} elseif ($is_paid) {
    $hero_state = 'paid';
} elseif ($has_ai && $has_facebook) {
    $hero_state = 'all_done';
} elseif ($has_ai) {
    $hero_state = 'ready_for_fb';
} else {
    $hero_state = 'ready_for_ai';
}
?>

<!-- ── Page header ──────────────────────────────────────────── -->
<div id="amai-ov-header">
    <div class="amai-ov-title">AutoMarketer AI</div>
    <p>AI-powered marketing automation for your business</p>
    <?php include AMAI_PLUGIN_DIR . 'templates/partials/tabs.php'; ?>
</div>

<div id="amai-overview">

    <!-- ── Hero ─────────────────────────────────────────────── -->

    <?php if ($hero_state === 'inactive'): ?>
        <div class="am-ov-hero">
            <div class="am-ov-eyebrow">Welcome to AutoMarketer AI</div>
            <h2 class="am-ov-hero-title">Built fair. Priced fair.<br>Does the heavy lifting so you don't have to.</h2>
            <button class="am-ov-cta" id="am-open-license-modal">Get started free - no card needed</button>
            <span class="am-ov-cta-sub">Free forever &nbsp;&middot;&nbsp; Upgrade anytime</span>
        </div>

    <?php elseif ($hero_state === 'ready_for_ai'): ?>
        <div class="am-ov-hero is-active">
            <div class="am-ov-eyebrow">You're in - let's get you set up</div>
            <h2 class="am-ov-hero-title">First thing: pick your AI.</h2>
            <p class="am-ov-hero-desc">Choose which AI provider you want to use. Each one runs on your own API key so you stay in control of your data and costs. Not sure which? Start with Claude - it's what we'd pick.</p>
            <a href="<?php echo esc_url(admin_url('admin.php?page=automarketer-ai-modules&from=overview')); ?>" class="am-ov-cta am-ov-cta--green">Choose your AI →</a>
        </div>

    <?php elseif ($hero_state === 'ready_for_fb'): ?>
        <div class="am-ov-hero is-active">
            <div class="am-ov-eyebrow">AI is ready - one more step</div>
            <h2 class="am-ov-hero-title">Now let's connect Facebook.</h2>
            <p class="am-ov-hero-desc">Your AI provider is all set. Connect your Facebook Business Page and we'll start writing and scheduling posts for you - Monday through Friday, every week.</p>
            <a href="<?php echo esc_url(admin_url('admin.php?page=automarketer-ai-social&channel=facebook')); ?>" class="am-ov-cta am-ov-cta--green">Connect Facebook →</a>
        </div>

    <?php elseif ($hero_state === 'all_done'):
        $has_instagram   = \AutoMarketerAI\FacebookAuth::getInstagramAccountId() !== null;
        $channel_phrase  = ($has_facebook && $has_instagram)
            ? 'Your Facebook and Instagram are connected.'
            : ($has_instagram ? 'Your Instagram is connected.' : 'Your Facebook page is connected.');
        $dashboard_url   = ($has_instagram && !$has_facebook)
            ? admin_url('admin.php?page=automarketer-ai-social&channel=instagram')
            : admin_url('admin.php?page=automarketer-ai-social&channel=facebook');
        $dashboard_label = ($has_instagram && !$has_facebook)
            ? 'Go to your Instagram dashboard →'
            : 'Go to your Facebook dashboard →';
    ?>
        <div class="am-ov-hero is-active">
            <div class="am-ov-eyebrow">You're all set up</div>
            <h2 class="am-ov-hero-title">We've got you covered.</h2>
            <p class="am-ov-hero-desc">Your AI is writing. <?php echo esc_html($channel_phrase); ?> Posts go out every weekday. Approve each one or let it run on autopilot - totally up to you.</p>
            <a href="<?php echo esc_url($dashboard_url); ?>" class="am-ov-cta am-ov-cta--green"><?php echo esc_html($dashboard_label); ?></a>
        </div>

    <?php else: ?>
        <div class="am-ov-hero is-active">
            <div class="am-ov-eyebrow"><?php echo esc_html($plan_label); ?> - Active</div>
            <h2 class="am-ov-hero-title">You're running on all cylinders.</h2>
            <p class="am-ov-hero-desc">Your <?php echo esc_html($plan_label); ?> plan is active. Everything is humming along.</p>
            <a href="<?php echo esc_url(admin_url('admin.php?page=automarketer-ai-social&channel=facebook')); ?>" class="am-ov-cta am-ov-cta--green">Go to your Facebook dashboard →</a>
            <span class="am-ov-cta-sub"><a href="<?php echo esc_url(admin_url('admin.php?page=automarketer-ai-billing')); ?>">Manage your plan</a></span>
        </div>
    <?php endif; ?>

    <?php if ($has_license && \AutoMarketerAI\Mailer::getActiveProviderId() === '' && (!defined('AMAI_INTERNAL_INSTALL') || !AMAI_INTERNAL_INSTALL)): ?>
    <div class="am-ov-nudge am-ov-nudge--email">
        <div class="am-ov-nudge-icon">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" width="20" height="20"><path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z" stroke-linecap="round" stroke-linejoin="round"/><path d="M22 6l-10 7L2 6" stroke-linecap="round" stroke-linejoin="round"/></svg>
        </div>
        <div class="am-ov-nudge-body">
            <strong>Set up your email provider</strong>
            <span>Send abandoned cart reminders, campaigns, and transactional emails directly to your customers. Takes about 2 minutes.</span>
        </div>
        <a href="<?php echo esc_url(admin_url('admin.php?page=automarketer-ai-modules')); ?>" class="am-ov-nudge-cta">Set up email &rarr;</a>
    </div>
    <?php endif; ?>

    <!-- ── Steps ─────────────────────────────────────────────── -->
    <div class="am-ov-steps">

        <div class="am-ov-step <?php echo $has_license ? 'is-done' : ''; ?>">
            <div class="am-ov-step-num"><?php echo $has_license ? '✓' : '1'; ?></div>
            <div class="am-ov-step-title"><?php echo $has_license ? 'Plan activated' : 'Activate your plan'; ?></div>
            <div class="am-ov-step-desc">
                <?php echo $has_license
                    ? 'You\'re on the ' . esc_html($plan_label) . '.'
                    : 'Start free - no credit card, no catch. Takes about 30 seconds.'; ?>
            </div>
            <?php if (!$has_license): ?>
                <button class="am-ov-step-link am-btn-reset" id="am-open-license-modal-2">Get started free →</button>
            <?php endif; ?>
        </div>

        <div class="am-ov-step <?php echo (!$has_license) ? 'is-locked' : ($has_ai ? 'is-done' : ''); ?>">
            <div class="am-ov-step-num">
                <?php if (!$has_license): ?>
                    <svg width="13" height="13" viewBox="0 0 13 13" fill="none"><rect x="2" y="5.5" width="9" height="6.5" rx="1.5" stroke="currentColor" stroke-width="1.3"/><path d="M4.5 5.5V3.5a2 2 0 014 0v2" stroke="currentColor" stroke-width="1.3" stroke-linecap="round"/></svg>
                <?php elseif ($has_ai): ?>
                    ✓
                <?php else: ?>
                    2
                <?php endif; ?>
            </div>
            <div class="am-ov-step-title"><?php echo $has_ai ? 'AI provider connected' : 'Choose your AI'; ?></div>
            <div class="am-ov-step-desc">
                <?php echo $has_ai
                    ? 'Your AI provider is ready to write your content.'
                    : 'Connect Claude, Gemini, Grok, or OpenAI using your own API key - you keep full control.'; ?>
            </div>
            <?php if (!$has_license): ?>
                <span class="am-ov-step-lock-badge">Complete step 1 first</span>
            <?php elseif ($has_ai): ?>
                <a class="am-ov-step-link" href="<?php echo esc_url(admin_url('admin.php?page=automarketer-ai-modules')); ?>">Go to Modules →</a>
            <?php else: ?>
                <a class="am-ov-step-link" href="<?php echo esc_url(admin_url('admin.php?page=automarketer-ai-modules&from=overview')); ?>">Choose your AI →</a>
            <?php endif; ?>
        </div>

        <div class="am-ov-step <?php echo (!$has_license || !$has_ai) ? 'is-locked' : ($has_facebook ? 'is-done' : ''); ?>">
            <div class="am-ov-step-num">
                <?php if (!$has_license || !$has_ai): ?>
                    <svg width="13" height="13" viewBox="0 0 13 13" fill="none"><rect x="2" y="5.5" width="9" height="6.5" rx="1.5" stroke="currentColor" stroke-width="1.3"/><path d="M4.5 5.5V3.5a2 2 0 014 0v2" stroke="currentColor" stroke-width="1.3" stroke-linecap="round"/></svg>
                <?php elseif ($has_facebook): ?>
                    ✓
                <?php else: ?>
                    3
                <?php endif; ?>
            </div>
            <div class="am-ov-step-title">
                <?php
                $has_ig_step = \AutoMarketerAI\FacebookAuth::getInstagramAccountId() !== null;
                if ($has_facebook && $has_ig_step):
                    echo 'Facebook &amp; Instagram connected';
                elseif ($has_facebook):
                    echo 'Facebook connected';
                else:
                    echo 'Connect Facebook';
                endif;
                ?>
            </div>
            <div class="am-ov-step-desc">
                <?php if ($has_facebook && $has_ig_step): ?>
                    Posts are going out on Facebook and Instagram every weekday.
                <?php elseif ($has_facebook): ?>
                    Posts are going out on <?php echo esc_html($fb_page_name); ?> every weekday.
                <?php else: ?>
                    Connect your Facebook Business Page and we'll write and schedule your posts every weekday.
                <?php endif; ?>
            </div>
            <?php if (!$has_license || !$has_ai): ?>
                <span class="am-ov-step-lock-badge">Complete step 2 first</span>
            <?php elseif ($has_facebook): ?>
                <a class="am-ov-step-link" href="<?php echo esc_url(admin_url('admin.php?page=automarketer-ai-social&channel=facebook')); ?>">Go to dashboard →</a>
            <?php else: ?>
                <a class="am-ov-step-link" href="<?php echo esc_url(admin_url('admin.php?page=automarketer-ai-social&channel=facebook')); ?>">Connect Facebook →</a>
            <?php endif; ?>
        </div>

    </div>

    <!-- ── Stats ─────────────────────────────────────────────── -->
    <?php if ($has_license): ?>
    <div class="am-ov-stats">
        <div class="am-ov-stat">
            <div class="am-ov-stat-num"><?php echo $posts_this_week; ?></div>
            <div class="am-ov-stat-label">post<?php echo $posts_this_week === 1 ? '' : 's'; ?> published this week</div>
        </div>
        <div class="am-ov-stat">
            <div class="am-ov-stat-num"><?php echo $pending_approval; ?></div>
            <div class="am-ov-stat-label">pending approval</div>
        </div>
        <div class="am-ov-stat">
            <div class="am-ov-stat-num am-ov-stat-num--sm"><?php echo esc_html($next_post_label); ?></div>
            <div class="am-ov-stat-label">next scheduled post</div>
        </div>
    </div>
    <?php endif; ?>



</div><!-- /#amai-overview -->


<!-- ══════════════════════════════════════════════════════════════
     LICENSE ACTIVATION MODAL
══════════════════════════════════════════════════════════════ -->
<?php if ($is_inactive): ?>
<div id="amai-license-modal">
    <div class="am-lic-wrap">
        <div class="am-lic-header">
            <div>
                <div class="am-lic-eyebrow">Free Plan</div>
                <div class="am-lic-title">Let's get you started</div>
            </div>
            <button class="am-lic-close" id="am-lic-close">&#10005;</button>
        </div>
        <div class="am-lic-body">
            <div class="am-lic-error" id="am-lic-error"></div>
            <div class="am-lic-privacy">
                We'll send you the occasional tip to help you get more out of AutoMarketer AI.
            </div>
            <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>" id="am-lic-form">
                <input type="hidden" name="action" value="amai_activate_free_license">
                <?php wp_nonce_field('amai_activate_free_license', '_wpnonce'); ?>
                <div class="am-lic-field">
                    <label class="am-lic-label" for="am-first-name">First Name <span class="am-required">*</span></label>
                    <input type="text" class="am-lic-input" name="first_name" id="am-first-name" placeholder="Your first name">
                </div>
                <div class="am-lic-field">
                    <label class="am-lic-label" for="am-email">Email Address <span class="am-required">*</span></label>
                    <input type="email" class="am-lic-input" name="email" id="am-email" placeholder="you@example.com">
                </div>
                <div class="am-lic-consent">
                    <input type="checkbox" name="consent" id="am-consent">
                    <label for="am-consent">Send me occasional tips and product updates.</label>
                </div>
            </form>
        </div>
        <div class="am-lic-footer">
            <button class="am-lic-btn-cancel" id="am-lic-cancel">Maybe later</button>
            <button class="am-lic-btn-submit" id="am-lic-submit">Activate Free Plan →</button>
        </div>
    </div>
</div>
<?php endif; ?>