<?php
// File: blog-detail.php
include("config/db.php");

// 1. Get the slug from the URL
$slug = isset($_GET['slug']) ? $conn->real_escape_string($_GET['slug']) : '';

if (empty($slug)) {
    header("Location: blogs.php"); // Redirect back if no slug provided
    exit();
}

// 2. Fetch the specific blog post from the database
$sql = "SELECT * FROM blogs WHERE slug = '$slug' AND status = 'Published'";
$result = $conn->query($sql);

if ($result->num_rows === 0) {
    die("Error: Article not found."); // Stop if the slug doesn't exist
}

$post = $result->fetch_assoc();

include('includes/header.php');
?>

<div class="container" style="max-width: 900px; margin: 40px auto; padding: 25px; background: #fff; border-radius: 12px; box-shadow: 0 4px 20px rgba(0,0,0,0.1);">
    
    <?php if (!empty($post['thumbnail_path'])): ?>
        <img src="<?= $post['thumbnail_path'] ?>" style="width: 100%; border-radius: 8px; margin-bottom: 25px; max-height: 450px; object-fit: cover;">
    <?php endif; ?>

    <h1 style="color: #6f42c1; font-size: 2.2em; margin-bottom: 10px;"><?= htmlspecialchars($post['title']) ?></h1>
    <p style="color: #888; font-size: 0.95em; margin-bottom: 20px;">
        <i class="fas fa-user"></i> Author: <?= htmlspecialchars($post['author']) ?> | 
        <i class="fas fa-calendar-alt"></i> Date: <?= date('d M, Y', strtotime($post['published_date'])) ?>
    </p>
    <hr style="border: 0; border-top: 1px solid #eee; margin-bottom: 25px;">
    
    <div class="blog-post-content" style="line-height: 1.8; font-size: 1.15em; color: #333;">
        <?= $post['content'] ?> 
    </div>

    <div style="margin-top: 50px; text-align: center; border-top: 1px solid #eee; padding-top: 25px;">
        <a href="blogs.php" style="background: #dc3545; color: #fff; padding: 12px 25px; text-decoration: none; border-radius: 6px; font-weight: bold;">← Back to All Articles</a>
    </div>
</div>

<?php include('includes/footer.php'); ?>