<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/functions.php';
require_once __DIR__ . '/includes/middleware.php';

if (!isset($pdo)) {
    $pdo = require_once __DIR__ . '/config/database.php';
}

$slug = $_GET['slug'] ?? '';
if (empty($slug)) {
    redirect(url('/'));
}

// Fetch landing page details
$stmt = $pdo->prepare("
    SELECT sl.*, s.service_name, s.slug as service_slug, s.id as service_id, s.cac_fee, s.service_fee, l.name as location_name 
    FROM seo_landings sl
    JOIN services s ON sl.service_id = s.id
    JOIN locations l ON sl.location_id = l.id
    WHERE sl.slug = ? AND sl.status = 'active'
");
$stmt->execute([$slug]);
$page = $stmt->fetch();

if (!$page) {
    http_response_code(404);
    include __DIR__ . '/404.php'; // Or redirect to home
    exit;
}

$meta = [
    'title' => $page['title'],
    'description' => $page['description'],
    'canonical' => url('l/' . $page['slug'])
];

include __DIR__ . '/includes/header.php';
$totalPrice = (float)$page['cac_fee'] + (float)$page['service_fee'];
?>

<style>
    .landing-hero {
        padding: 80px 0;
        background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%);
        border-radius: 24px;
        margin-bottom: 64px;
        text-align: center;
    }
    
    .landing-h1 {
        font-size: 48px;
        font-weight: 800;
        color: var(--text);
        margin: 0 0 20px;
        letter-spacing: -0.03em;
    }
    
    .feature-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
        gap: 24px;
        margin-bottom: 64px;
    }
    
    .trust-banner {
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 32px;
        padding: 24px;
        background: #fff;
        border: 1px solid var(--border);
        border-radius: 16px;
        margin-bottom: 64px;
        flex-wrap: wrap;
    }
    
    .pricing-card {
        max-width: 500px;
        margin: 0 auto 64px;
        background: #fff;
        border: 2px solid var(--primary);
        border-radius: 20px;
        padding: 40px;
        box-shadow: var(--shadow-lg);
        position: relative;
    }
    
    .pricing-badge {
        position: absolute;
        top: -15px;
        left: 50%;
        transform: translateX(-50%);
        background: var(--primary);
        color: #fff;
        padding: 6px 16px;
        border-radius: 20px;
        font-weight: 700;
        font-size: 14px;
    }
</style>

<div class="landing-hero reveal">
    <div class="container" style="max-width: 800px;">
        <div class="badge" style="margin-bottom: 24px; background: #dcfce7; color: #16a34a;">
            <?=ui_icon('approved', 14)?> Official CAC Registration Partner in <?=e($page['location_name'])?>
        </div>
        <h1 class="landing-h1"><?=e($page['h1_title'])?></h1>
        <p class="lead" style="font-size: 20px; margin-bottom: 32px;">
            Get your business legally registered with the Corporate Affairs Commission (CAC) without the stress. Fast, reliable, and 100% transparent service for entrepreneurs in <strong><?=e($page['location_name'])?></strong>.
        </p>
        <div style="display:flex; gap:16px; justify-content:center; flex-wrap:wrap">
            <a href="<?=cta_url('customer/new_application.php?service_id=' . $page['service_id'])?>" class="btn primary" style="padding:16px 32px; font-size:16px">Start My Registration</a>
            <a href="#how-it-works" class="btn ghost" style="padding:16px 32px; font-size:16px">How it Works</a>
        </div>
    </div>
</div>

<div class="trust-banner reveal">
    <div style="display:flex; align-items:center; gap:8px">
        <?=ui_icon('check', 20)?> <span style="font-weight:600">24-48 Hour Processing</span>
    </div>
    <div style="display:flex; align-items:center; gap:8px">
        <?=ui_icon('check', 20)?> <span style="font-weight:600">Certified by Experts</span>
    </div>
    <div style="display:flex; align-items:center; gap:8px">
        <?=ui_icon('check', 20)?> <span style="font-weight:600">TIN & SCUML Support</span>
    </div>
    <div style="display:flex; align-items:center; gap:8px">
        <?=ui_icon('check', 20)?> <span style="font-weight:600">Secure Payments</span>
    </div>
</div>

<?php if (!empty($page['content_json'])): ?>
    <div class="container custom-content-wrap reveal">
        <?= $page['content_json'] ?>
    </div>
<?php else: ?>
    <section class="section">
        <div class="grid grid-2" style="align-items:center; gap:64px">
            <div class="reveal">
                <h2>Why Founders in <?=e($page['location_name'])?> Choose Go Legal</h2>
                <p class="lead">Registering a business with CAC shouldn't take weeks of chasing agents. We've simplified the entire process into 3 digital steps.</p>
                <ul class="list-clean" style="margin-top:32px; line-height:2.5">
                    <li><?=ui_icon('approved', 20)?> <strong>Expert Document Review</strong>: We audit your application to prevent rejections.</li>
                    <li><?=ui_icon('approved', 20)?> <strong>No Office Visits</strong>: Complete everything from your home or office in <?=e($page['location_name'])?>.</li>
                    <li><?=ui_icon('approved', 20)?> <strong>Digital Delivery</strong>: Get your certificate and TIN delivered to your email.</li>
                    <li><?=ui_icon('approved', 20)?> <strong>Flat-Rate Pricing</strong>: No hidden fees, no "negotiation" with agents.</li>
                </ul>
            </div>
            <div class="reveal">
                <div class="card glass" style="padding:48px; border:none; background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%); color:#fff">
                    <h3 style="color:#fff; margin-top:0">Ready to start?</h3>
                    <p style="color:rgba(255,255,255,0.7); margin-bottom:32px">Join 200+ businesses already registered and protected. It takes less than 5 minutes to submit your details.</p>
                    <div class="pricing-card" style="margin:0; border-color:rgba(255,255,255,0.2); background:rgba(255,255,255,0.05); color:#fff">
                        <div class="pricing-badge">All-Inclusive Bundle</div>
                        <div class="muted" style="color:rgba(255,255,255,0.5); text-transform:uppercase; font-size:12px; font-weight:700; margin-bottom:8px">Starting At</div>
                        <div style="font-size:48px; font-weight:800; margin-bottom:24px">₦<?=e(money($totalPrice))?></div>
                        <ul class="list-clean" style="color:rgba(255,255,255,0.8); margin-bottom:32px; font-size:14px">
                            <li><?=ui_icon('check', 14)?> CAC Official Filing Fees</li>
                            <li><?=ui_icon('check', 14)?> Name Reservation</li>
                            <li><?=ui_icon('check', 14)?> Professional Document Prep</li>
                            <li><?=ui_icon('check', 14)?> Digital Delivery</li>
                        </ul>
                        <a href="<?=url('customer/new_application.php?service_id=' . $page['service_id'])?>" class="btn primary" style="width:100%; justify-content:center; padding:16px">Register My Business Now</a>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <section class="section" id="how-it-works" style="background:#fff; border-radius:24px; padding:64px">
        <h2 style="text-align:center; margin-bottom:48px">How it Works in <?=e($page['location_name'])?></h2>
        <div class="grid grid-3">
            <div class="reveal">
                <div class="stats-value" style="font-size:24px; margin-bottom:16px">01</div>
                <h3>Pick Service</h3>
                <p class="muted">Select "<?=e($page['service_name'])?>" and provide your preferred business names.</p>
            </div>
            <div class="reveal">
                <div class="stats-value" style="font-size:24px; margin-bottom:16px">02</div>
                <h3>Upload Docs</h3>
                <p class="muted">Submit your ID and director details securely through our portal.</p>
            </div>
            <div class="reveal">
                <div class="stats-value" style="font-size:24px; margin-bottom:16px">03</div>
                <h3>Get Registered</h3>
                <p class="muted">We file with CAC and deliver your certificate once approved.</p>
            </div>
        </div>
    </section>
<?php endif; ?>

<?php include __DIR__ . '/includes/footer.php'; ?>
