<?php
declare(strict_types=1);
// ==================================================
// Nuclear Fancy Pants Edition™ -- NUCLEAR+++
// File: /home/ploi/automarketer-ai.com/public/wp-content/plugins/automarketer-ai/templates/content.php
// Description: Content hub admin page -- Content Health, Link Checker, Outbound Links, Competitor Gap, Content Clusters tabs - NUCLEAR+++
// Last Edited: 2026-05-18
// ==================================================
defined('ABSPATH') || exit;

$tab = sanitize_key((string) ($_GET['tab'] ?? 'health'));
if (!in_array($tab, ['health', 'links', 'outbound', 'gap', 'cluster'], true)) {
    $tab = 'health';
}

$base_url        = admin_url('admin.php?page=automarketer-ai-content');
$upgrade_url     = admin_url('admin.php?page=automarketer-ai-billing');
$modules_url     = admin_url('admin.php?page=automarketer-ai-modules');
$current_plan    = (string) get_option('automarketer_ai_plan_id', 'free');
$is_starter_plus = in_array($current_plan, ['starter', 'starter_pro', 'unlimited_ai', 'agency'], true);
$is_unlimited    = in_array($current_plan, ['unlimited_ai', 'agency'], true);
?>
<div class="wrap amai-content-hub-wrap">
    <div class="amai-content-hub-header">
        <h1 class="amai-content-hub-title">Content</h1>
        <p class="amai-content-hub-subtitle">Monitor content health, catch broken links, manage outbound links, and find content gaps.</p>
    </div>
    <div class="amai-content-hub-tabs-wrap">
        <div class="amai-content-hub-tabs">
            <a href="<?php echo esc_url($base_url . '&tab=health'); ?>"
               class="amai-content-hub-tab<?php echo $tab === 'health' ? ' amai-content-hub-tab--active' : ''; ?>">
                Content Health
            </a>
            <a href="<?php echo esc_url($base_url . '&tab=links'); ?>"
               class="amai-content-hub-tab<?php echo $tab === 'links' ? ' amai-content-hub-tab--active' : ''; ?>">
                Link Checker
            </a>
            <a href="<?php echo esc_url($base_url . '&tab=gap'); ?>"
               class="amai-content-hub-tab<?php echo $tab === 'gap' ? ' amai-content-hub-tab--active' : ''; ?>">
                Competitor Gap
            </a>
            <a href="<?php echo esc_url($base_url . '&tab=cluster'); ?>"
               class="amai-content-hub-tab<?php echo $tab === 'cluster' ? ' amai-content-hub-tab--active' : ''; ?>">
                Content Clusters
            </a>
        </div>
    </div>
    <div class="amai-content-hub-body">
        <?php if ($tab === 'health') : ?>
            <?php \AutoMarketerAI\ContentFreshness::render(); ?>

        <?php elseif ($tab === 'links') : ?>
            <?php if ($is_starter_plus) : ?>
                <?php \AutoMarketerAI\LinkChecker::render(); ?>
            <?php else : ?>
                <?php amai_content_upgrade_prompt('Link Checker', 'Starter', $upgrade_url,
                    'Automatically scan every published post for broken links. Fix them before Google notices.',
                    ['Scans all published posts', 'Flags 404s and dead links', 'One-click fix suggestions', 'Re-scan after fixes']
                ); ?>
            <?php endif; ?>

        <?php elseif ($tab === 'gap') : ?>
            <?php if ($is_starter_plus) : ?>
                <?php include AMAI_PLUGIN_DIR . 'templates/competitor-gap.php'; ?>
            <?php else : ?>
                <?php amai_content_upgrade_prompt('Competitor Gap', 'Starter', $upgrade_url,
                    'Find topics your competitors rank for that you have not covered yet. Turn their strengths into your content calendar.',
                    ['AI-powered competitor analysis', 'Topic gap identification', 'One-click add to content queue', 'Feeds your Mon/Wed/Fri blog schedule']
                ); ?>
            <?php endif; ?>

        <?php elseif ($tab === 'cluster') : ?>
            <?php if ($is_unlimited) : ?>
                <?php \AutoMarketerAI\ClusterBuilder::render(); ?>
            <?php else : ?>
                <?php amai_content_upgrade_prompt('Content Cluster Builder', 'Unlimited AI', $upgrade_url,
                    'Build topical authority automatically. One topic becomes a pillar post plus 5 supporting posts, all internally linked and scheduled across Tue/Thu/Sat slots.',
                    ['1 pillar post + 5 supporting posts per cluster', 'Automatic internal linking between all posts', 'Scheduled across Tue/Thu/Sat gaps', 'Respects your draft/autopublish setting', 'Google rewards topical depth - this builds it on autopilot']
                ); ?>
            <?php endif; ?>

        <?php endif; ?>
    </div>
</div>
<?php

// --------------------------------------------------
// Upgrade prompt renderer
// --------------------------------------------------
function amai_content_upgrade_prompt(string $feature, string $plan, string $upgrade_url, string $description, array $bullets): void
{
    $plan_label = $plan === 'Starter' ? 'Starter plan and above' : 'Unlimited AI plan and above';
    ?>
    <div class="amai-upgrade-prompt">
        <div class="amai-upgrade-prompt-inner">
            <div class="amai-upgrade-prompt-eyebrow"><?php echo esc_html($plan_label); ?></div>
            <h2 class="amai-upgrade-prompt-title"><?php echo esc_html($feature); ?></h2>
            <p class="amai-upgrade-prompt-desc"><?php echo esc_html($description); ?></p>
            <ul class="amai-upgrade-prompt-list">
                <?php foreach ($bullets as $bullet) : ?>
                <li class="amai-upgrade-prompt-item">
                    <span class="amai-upgrade-prompt-check">&#10003;</span>
                    <?php echo esc_html($bullet); ?>
                </li>
                <?php endforeach; ?>
            </ul>
            <a href="<?php echo esc_url($upgrade_url); ?>" class="amai-upgrade-prompt-btn">
                Upgrade to <?php echo esc_html($plan); ?>
            </a>
        </div>
    </div>
    <?php
}