<?php
// File: article_single.php
include 'header.php';

// --- HELPER: Extract YouTube ID ---
function getYoutubeId($url) {
    if (empty($url)) return null;
    preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match);
    return isset($match[1]) ? $match[1] : null;
}

// Check if an ID is provided
if (!isset($_GET['id'])) {
    header("Location: news.php");
    exit();
}

$article_id = filter_var($_GET['id'], FILTER_SANITIZE_NUMBER_INT);

// Fetch the specific article
// CHANGE 1: Added 'video_url' to the SELECT statement
$article = null;
$stmt = $conn->prepare("SELECT title, content, image_path, category, created_at, video_url FROM articles WHERE id = ?");
$stmt->bind_param("i", $article_id);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows > 0) {
    $article = $result->fetch_assoc();
} else {
    // No article found, redirect
    header("Location: news.php");
    exit();
}
$stmt->close();
?>

<div class="bg-white py-12 md:py-20">
    <div class="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8">
        
        <div class="mb-8 text-center">
            <span class="inline-block bg-ucf-green-light text-ucf-green-dark text-sm font-semibold px-4 py-1 rounded-full uppercase tracking-wider">
                <?php echo htmlspecialchars($article['category']); ?>
            </span>
            <h1 class="text-4xl md:text-5xl font-extrabold text-ucf-charcoal mt-4 leading-tight">
                <?php echo htmlspecialchars($article['title']); ?>
            </h1>
            <p class="text-lg text-gray-500 mt-3">
                Published on <?php echo date('F d, Y', strtotime($article['created_at'])); ?>
            </p>
        </div>

        <div class="w-full bg-gray-100 rounded-lg shadow-lg overflow-hidden mb-8">
            <?php 
                $video_id = null;
                if (!empty($article['video_url'])) {
                    $video_id = getYoutubeId($article['video_url']);
                }
            ?>

            <?php if ($video_id): ?>
                <div class="relative w-full" style="padding-bottom: 56.25%;"> <iframe 
                        class="absolute top-0 left-0 w-full h-full"
                        src="https://www.youtube.com/embed/<?php echo $video_id; ?>?rel=0" 
                        frameborder="0" 
                        allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" 
                        allowfullscreen>
                    </iframe>
                </div>
            <?php else: ?>
                <img src="<?php echo htmlspecialchars($article['image_path']); ?>" 
                     alt="<?php echo htmlspecialchars($article['title']); ?>" 
                     class="w-full h-auto max-h-[600px] object-contain mx-auto"
                     onerror="this.style.display='none'"> <?php endif; ?>
        </div>

        <div class="prose prose-lg max-w-none text-gray-700 leading-relaxed">
            <?php 
                // Using nl2br to respect line breaks from the textarea
                echo nl2br(htmlspecialchars($article['content'])); 
            ?>
        </div>

        <div class="mt-12 text-center">
            <a href="news.php" class="inline-flex items-center bg-ucf-green text-white font-bold py-2 px-4 rounded-lg hover:bg-ucf-green-dark transition-colors">
                <i class="fas fa-arrow-left mr-2"></i>
                Back to All News
            </a>
        </div>

    </div>
</div>

<?php include 'footer.php'; ?>