<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/functions.php';
require_once __DIR__ . '/includes/middleware.php';
$pdo = require __DIR__ . '/config/database.php';

// Fetch published posts
$stmt = $pdo->prepare("
    SELECT p.*, s.seo_title, s.meta_description 
    FROM blog_posts p 
    LEFT JOIN blog_seo s ON p.id = s.post_id 
    WHERE p.status = 'published' AND (p.published_at IS NULL OR p.published_at <= ?) 
    ORDER BY p.published_at DESC, p.created_at DESC
");
$stmt->execute([date('c')]);
$posts = $stmt->fetchAll();

$meta = [
    'title' => 'Go Legal Blog | Legal Insights & Business Registration Tips in Nigeria',
    'description' => 'Stay updated with the latest legal insights, business registration tips, and compliance guides for Nigerian entrepreneurs and companies.',
    'canonical' => url('blog')
];

include __DIR__ . '/includes/header.php';
?>

<section class="section" style="padding-top: 48px;">
    <div style="text-align: center; max-width: 700px; margin: 0 auto 64px;">
        <h1 style="font-size: 48px; font-weight: 800; letter-spacing: -0.04em; margin-bottom: 16px;">Legal <span class="gradient-text">Insights.</span></h1>
        <p class="lead">Everything you need to know about registering and running a compliant business in Nigeria.</p>
    </div>

    <?php if (empty($posts)): ?>
        <div class="card" style="text-align: center; padding: 64px 20px;">
            <div style="font-size: 48px; margin-bottom: 24px;">📝</div>
            <h3>Coming Soon!</h3>
            <p class="muted">We're currently drafting some amazing legal guides for you. Check back shortly!</p>
            <a href="<?=url('/')?>" class="btn primary" style="margin-top: 24px;">Return Home</a>
        </div>
    <?php else: ?>
        <div class="grid grid-3" style="gap: 32px;">
            <?php foreach ($posts as $post): ?>
                <article class="card reveal" style="padding: 0; overflow: hidden; display: flex; flex-direction: column;">
                    <?php if ($post['featured_image']): ?>
                        <a href="<?=url('blog/' . e($post['slug']))?>" style="display: block; height: 200px; overflow: hidden;">
                            <img src="<?=url($post['featured_image'])?>" alt="<?=e($post['title'])?>" style="width: 100%; height: 100%; object-fit: cover; transition: transform 0.3s ease;">
                        </a>
                    <?php endif; ?>
                    <div style="padding: 24px; flex: 1; display: flex; flex-direction: column;">
                        <div style="margin-bottom: 12px; font-size: 12px; font-weight: 600; color: var(--primary); text-transform: uppercase; letter-spacing: 0.05em;">
                            <?=date('M j, Y', strtotime($post['published_at'] ?? $post['created_at']))?>
                        </div>
                        <h3 style="margin: 0 0 12px; font-size: 20px; line-height: 1.4;">
                            <a href="<?=url('blog/' . e($post['slug']))?>" style="text-decoration: none; color: var(--text);"><?=e($post['title'])?></a>
                        </h3>
                        <p class="muted" style="font-size: 14px; line-height: 1.6; margin-bottom: 24px; flex: 1;">
                            <?=e(substr($post['excerpt'] ?: strip_tags((string)$post['content']), 0, 120))?>...
                        </p>
                        <a href="<?=url('blog/' . e($post['slug']))?>" class="btn secondary" style="width: fit-content; font-size: 13px;">Read Article</a>
                    </div>
                </article>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>
</section>

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