<?php
/**
 * News & Updates Page
 * Path: news.php
 */
include_once 'header.php';

if (!function_exists('get_youtube_embed_url')) {
    function get_youtube_embed_url($url) {
        $video_id = '';
        if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/\s]{11})%i', $url, $match)) {
            $video_id = $match[1];
        }
        return $video_id ? "https://www.youtube.com/embed/{$video_id}" : $url;
    }
}

$blogs = get_blogs(20);
$featured = null;
$regular  = [];

foreach ($blogs as $post) {
    if (!$featured && !empty($post['is_featured'])) {
        $featured = $post;
    } else {
        $regular[] = $post;
    }
}
if (!$featured && !empty($blogs)) {
    $featured = $blogs[0];
    $regular  = array_slice($blogs, 1);
}

function blog_thumb($url) {
    if (empty($url)) return 'https://images.unsplash.com/photo-1581092160607-ee22621dd758?w=600';
    return $url;
}

function blog_detail_url($post) {
    if (!empty($post['id'])) return 'news-details.php?id=' . intval($post['id']);
    return 'news.php';
}
?>

<section class="relative bg-gradient-to-r from-slate-950 to-blue-900 text-white py-14 md:py-20">
    <div class="max-w-7xl mx-auto px-4 text-center">
        <nav class="text-xs text-sky-400 font-bold uppercase mb-3">
            <a href="index.php" class="hover:text-white">Home</a> <span class="mx-2">/</span> News & Updates
        </nav>
        <h1 class="text-3xl md:text-5xl font-black uppercase">Articles & Advisory Updates</h1>
        <div class="w-16 h-1 bg-secondary mx-auto mt-4"></div>
    </div>
</section>

<section class="py-16 md:py-24 bg-gray-50">
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">

        <?php if (empty($blogs)): ?>
        <div class="text-center py-20 bg-white rounded-3xl border border-gray-100">
            <i class="fa-solid fa-newspaper text-4xl text-gray-200 mb-4 block"></i>
            <p class="text-gray-500">কোনো নিউজ পোস্ট নেই।</p>
        </div>
        <?php else: ?>

        <div class="grid grid-cols-1 lg:grid-cols-12 gap-10">

            <!-- Featured Post -->
            <?php if ($featured): ?>
            <div class="lg:col-span-5">
                <a href="<?php echo blog_detail_url($featured); ?>" class="bg-white rounded-2xl overflow-hidden border border-gray-100 shadow-sm h-full flex flex-col hover:shadow-lg transition-all group block">
                    <div class="relative bg-black aspect-video">
                        <?php if (!empty($featured['video_url'])): ?>
                        <iframe class="absolute inset-0 w-full h-full pointer-events-none" src="<?php echo get_youtube_embed_url($featured['video_url']); ?>"
                            title="Featured Video" frameborder="0" allowfullscreen></iframe>
                        <?php else: ?>
                        <img src="<?php echo htmlspecialchars(blog_thumb($featured['thumbnail_url'])); ?>"
                            class="w-full h-full object-cover group-hover:scale-105 transition-transform duration-500" alt="">
                        <?php endif; ?>
                    </div>
                    <div class="p-6 space-y-3 flex-1 flex flex-col">
                        <span class="text-[10px] font-black uppercase text-secondary tracking-wider">
                            <i class="fa-solid fa-star mr-1"></i> Featured
                        </span>
                        <h2 class="text-xl font-black text-primary uppercase leading-tight group-hover:text-secondary transition-colors"><?php echo htmlspecialchars($featured['title']); ?></h2>
                        <p class="text-sm text-gray-500 leading-relaxed flex-1"><?php echo htmlspecialchars($featured['excerpt']); ?></p>
                        <span class="text-secondary font-bold text-sm inline-flex items-center pt-2">
                            Read Full Article <i class="fa-solid fa-arrow-right ml-2"></i>
                        </span>
                    </div>
                </a>
            </div>
            <?php endif; ?>

            <!-- Regular Posts Grid -->
            <div class="<?php echo $featured ? 'lg:col-span-7' : 'lg:col-span-12'; ?>">
                <div class="grid grid-cols-1 sm:grid-cols-2 gap-6">
                    <?php foreach ($regular as $post): ?>
                    <a href="<?php echo blog_detail_url($post); ?>" class="bg-white rounded-2xl overflow-hidden border border-gray-100 hover:shadow-lg hover:-translate-y-1 transition-all group block">
                        <div class="aspect-video overflow-hidden bg-gray-200">
                            <img src="<?php echo htmlspecialchars(blog_thumb($post['thumbnail_url'])); ?>"
                                class="w-full h-full object-cover group-hover:scale-105 transition-transform duration-500" alt="">
                        </div>
                        <div class="p-5 space-y-2">
                            <h3 class="font-black text-primary uppercase text-sm leading-tight line-clamp-2 group-hover:text-secondary transition-colors">
                                <?php echo htmlspecialchars($post['title']); ?>
                            </h3>
                            <p class="text-xs text-gray-500 line-clamp-3 leading-relaxed"><?php echo htmlspecialchars($post['excerpt']); ?></p>
                            <?php if (!empty($post['created_at'])): ?>
                            <p class="text-[10px] text-gray-400 pt-1">
                                <i class="fa-regular fa-calendar mr-1"></i>
                                <?php echo date('d M Y', strtotime($post['created_at'])); ?>
                            </p>
                            <?php endif; ?>
                            <span class="text-secondary font-bold text-xs inline-flex items-center pt-1">
                                Read More <i class="fa-solid fa-arrow-right ml-1"></i>
                            </span>
                        </div>
                    </a>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
        <?php endif; ?>
    </div>
</section>

<?php include_once 'footer.php'; ?>
