<?php
// File: news_single.php
// Shows a single news/article by id

ini_set('display_errors', 1);
error_reporting(E_ALL);

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
require_once 'db.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;
}

// Validate id param
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
if ($id <= 0) {
    http_response_code(404);
    echo "<div class='max-w-4xl mx-auto py-24'><h1 class='text-3xl font-bold'>Invalid Article ID</h1></div>";
    include 'footer.php';
    exit();
}

// Fetch article
// CHANGE 1: Added 'video_url' to SELECT
$stmt = $conn->prepare("SELECT id, title, content, created_at, image_path, video_url FROM articles WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();

if (!$result || $result->num_rows === 0) {
    http_response_code(404);
    echo "<div class='max-w-4xl mx-auto py-24'><h1 class='text-3xl font-bold'>Article Not Found</h1></div>";
    include 'footer.php';
    exit();
}

$article = $result->fetch_assoc();
$stmt->close();

// --- PREPARE MEDIA ---
$video_id = null;
if (!empty($article['video_url'])) {
    $video_id = getYoutubeId($article['video_url']);
}
?>

<div class="max-w-4xl mx-auto py-12 px-4">
    
    <div class="w-full rounded-lg overflow-hidden shadow-lg mb-8 bg-gray-100">
        <?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 elseif (!empty($article['image_path'])): ?>
            <img src="<?php echo htmlspecialchars($article['image_path']); ?>" 
                 alt="<?php echo htmlspecialchars($article['title']); ?>" 
                 class="w-full h-auto object-cover">
        <?php endif; ?>
    </div>

    <h1 class="text-4xl font-bold text-ucf-charcoal mb-2"><?php echo htmlspecialchars($article['title']); ?></h1>
    <p class="text-sm text-gray-500 mb-8">Published on <?php echo date('F j, Y', strtotime($article['created_at'])); ?></p>

    <div class="prose max-w-none text-gray-800 leading-relaxed">
        <?php
        // Output content safely
        echo $article['content'];
        ?>
    </div>

    <div class="mt-8">
        <a href="news.php" class="inline-block bg-ucf-green text-white px-4 py-2 rounded hover:bg-ucf-green-dark">Back to News</a>
    </div>
</div>

<?php include 'footer.php'; ?>