<?php
require_once 'admin/config/db.php';

$slug = isset($_GET['slug']) ? trim($_GET['slug']) : '';
if (!$slug) {
    header("Location: blog.php");
    exit;
}

// Fetch the post
$stmt = $pdo->prepare("
    SELECT p.*, c.name as category_name, c.slug as category_slug 
    FROM blog_posts p 
    LEFT JOIN blog_categories c ON p.category_id = c.id 
    WHERE p.slug = ? AND p.status = 'published'
");
$stmt->execute([$slug]);
$post = $stmt->fetch();

if (!$post) {
    header("Location: blog.php");
    exit;
}

// Fetch recent posts for sidebar
$recent_posts = $pdo->query("SELECT * FROM blog_posts WHERE status = 'published' ORDER BY created_at DESC LIMIT 5")->fetchAll();
$categories = $pdo->query("SELECT c.*, (SELECT COUNT(*) FROM blog_posts WHERE category_id = c.id AND status = 'published') as post_count FROM blog_categories c WHERE status = 'active'")->fetchAll();

$pageTitle = htmlspecialchars($post['title']) . " | MK Construction & Builder";
$pageDesc = substr(strip_tags($post['content']), 0, 160);
include 'includes/header.php';
?>

    <section class="section-padding bg-light">
        <div class="container">
            <div class="blog-layout" style="display: grid; grid-template-columns: 1fr 350px; gap: 40px; align-items: start;">
                <div class="blog-detail-content">
                    <article style="background: white; border-radius: 15px; overflow: hidden; box-shadow: var(--shadow-sm); padding: 40px;">
                        <img src="<?= htmlspecialchars($post['image_path']) ?>" alt="<?= htmlspecialchars($post['title']) ?>" style="width: 100%; border-radius: 10px; margin-bottom: 30px;">
                        <div class="blog-meta mb-3" style="color: var(--text-muted); font-size: 0.9rem;">
                            <span><i class="fa-regular fa-calendar"></i> <?= date('M d, Y', strtotime($post['created_at'])) ?></span> | 
                            <span><i class="fa-regular fa-folder"></i> <?= htmlspecialchars($post['category_name']) ?></span> | 
                            <span><i class="fa-regular fa-user"></i> <?= htmlspecialchars($post['author'] ?? 'Admin') ?></span>
                        </div>
                        <h1 style="font-family: var(--font-serif); font-size: 2.5rem; line-height: 1.2; margin-bottom: 25px;"><?= htmlspecialchars($post['title']) ?></h1>
                        <div class="content-body" style="line-height: 1.8; color: #4b5563; font-size: 1.1rem;">
                            <?= $post['content'] ?>
                        </div>
                    </article>
                </div>

                <aside class="sidebar">
                    <!-- Similar sidebar logic as blog.php -->
                    <div class="sidebar-widget" style="background: white; padding: 25px; border-radius: 10px; box-shadow: var(--shadow-sm); margin-bottom: 30px;">
                        <h3 style="border-left: 4px solid var(--primary-color); padding-left: 10px; margin-bottom: 20px;">Recent Posts</h3>
                        <?php foreach($recent_posts as $rp): ?>
                            <div class="rp-item" style="display: flex; gap: 15px; margin-bottom: 20px; align-items: center;">
                                <img src="<?= $rp['image_path'] ?>" style="width: 60px; height: 60px; border-radius: 8px; object-fit: cover;">
                                <div>
                                    <h4 style="font-size: 0.9rem; margin: 0;"><a href="blog-details.php?slug=<?= $rp['slug'] ?>" style="color: var(--dark-nav); text-decoration: none;"><?= htmlspecialchars($rp['title']) ?></a></h4>
                                    <small style="color: var(--text-muted);"><?= date('M d', strtotime($rp['created_at'])) ?></small>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                </aside>
            </div>
        </div>
    </section>

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