<?php
/**
 * News / Blog Details Page
 * Path: news-details.php?id=123
 */
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;
    }
}

$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$post = $id > 0 ? get_blog_by_id($id) : null;

if (!$post) {
    echo "<main class='flex-1 p-16 text-center'>
            <h2 class='text-2xl font-black text-gray-700 uppercase'>News Not Found</h2>
            <p class='text-gray-400 mt-2'>এই নিউজ পোস্টটি পাওয়া যায়নি।</p>
            <a href='news.php' class='mt-6 inline-block bg-primary text-white py-3 px-6 rounded-lg font-bold'>← Back to News</a>
          </main>";
    include_once 'footer.php';
    exit;
}

$thumb = !empty($post['thumbnail_url'])
    ? $post['thumbnail_url']
    : 'https://images.unsplash.com/photo-1581092160607-ee22621dd758?w=1200';

$site_phone = get_site_setting('site_phone');
$related = array_filter(get_blogs(6), function ($b) use ($post) {
    return empty($b['id']) || (int)$b['id'] !== (int)$post['id'];
});
$related = array_slice($related, 0, 3);
?>

<section class="relative bg-gradient-to-r from-slate-950 to-blue-900 text-white py-12 md:py-16">
    <div class="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8 space-y-4">
        <nav class="text-xs text-sky-400 font-bold uppercase tracking-wider">
            <a href="index.php" class="hover:text-white">Home</a>
            <span class="mx-2 text-slate-500">/</span>
            <a href="news.php" class="hover:text-white">News</a>
            <span class="mx-2 text-slate-500">/</span>
            <span class="text-slate-300"><?php echo htmlspecialchars($post['title']); ?></span>
        </nav>
        <?php if (!empty($post['is_featured'])): ?>
        <span class="inline-flex items-center gap-1 text-[10px] font-black uppercase bg-secondary/90 text-white px-3 py-1 rounded-full">
            <i class="fa-solid fa-star"></i> Featured
        </span>
        <?php endif; ?>
        <h1 class="text-2xl md:text-4xl font-black uppercase tracking-tight leading-tight"><?php echo htmlspecialchars($post['title']); ?></h1>
        <?php if (!empty($post['created_at'])): ?>
        <p class="text-sm text-sky-200/80">
            <i class="fa-regular fa-calendar mr-1"></i>
            <?php echo date('d F Y', strtotime($post['created_at'])); ?>
        </p>
        <?php endif; ?>
        <div class="w-12 h-1 bg-secondary"></div>
    </div>
</section>

<section class="py-12 md:py-16 bg-gray-50">
    <div class="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8">
        <article class="bg-white rounded-2xl border border-gray-100 shadow-sm overflow-hidden">
            <div class="relative bg-black aspect-video">
                <?php if (!empty($post['video_url'])): ?>
                <iframe class="absolute inset-0 w-full h-full"
                    src="<?php echo htmlspecialchars(get_youtube_embed_url($post['video_url'])); ?>"
                    title="<?php echo htmlspecialchars($post['title']); ?>"
                    frameborder="0" allowfullscreen></iframe>
                <?php else: ?>
                <img src="<?php echo htmlspecialchars($thumb); ?>" class="w-full h-full object-cover" alt="">
                <?php endif; ?>
            </div>

            <div class="p-6 md:p-10 space-y-6">
                <?php if (!empty($post['excerpt'])): ?>
                <p class="text-lg text-gray-600 font-medium leading-relaxed border-l-4 border-secondary pl-4">
                    <?php echo htmlspecialchars($post['excerpt']); ?>
                </p>
                <?php endif; ?>

                <div class="prose prose-slate max-w-none text-gray-700 leading-relaxed text-sm md:text-base space-y-4 blog-content">
                    <?php
                    // Allow basic HTML from admin; escape if looks like plain text only
                    $content = $post['content'] ?? '';
                    if ($content !== strip_tags($content)) {
                        echo $content;
                    } else {
                        echo nl2br(htmlspecialchars($content));
                    }
                    ?>
                </div>

                <div class="pt-6 border-t border-gray-100 flex flex-wrap gap-3">
                    <a href="news.php" class="inline-flex items-center px-5 py-2.5 rounded-xl bg-gray-100 hover:bg-gray-200 text-gray-700 text-sm font-bold transition-all">
                        <i class="fa-solid fa-arrow-left mr-2"></i> All News
                    </a>
                    <a href="contact.php" class="inline-flex items-center px-5 py-2.5 rounded-xl bg-primary hover:bg-secondary text-white text-sm font-bold transition-all">
                        <i class="fa-solid fa-paper-plane mr-2"></i> Request Quote
                    </a>
                    <a href="tel:<?php echo htmlspecialchars($site_phone); ?>" class="inline-flex items-center px-5 py-2.5 rounded-xl border border-primary/20 text-primary text-sm font-bold hover:bg-blue-50 transition-all">
                        <i class="fa-solid fa-phone mr-2"></i> Call Now
                    </a>
                </div>
            </div>
        </article>

        <?php if (!empty($related)): ?>
        <div class="mt-12">
            <h2 class="text-xl font-black text-primary uppercase mb-6">More Updates</h2>
            <div class="grid grid-cols-1 sm:grid-cols-3 gap-5">
                <?php foreach ($related as $r):
                    if (empty($r['id'])) continue;
                    $rthumb = !empty($r['thumbnail_url']) ? $r['thumbnail_url'] : 'https://images.unsplash.com/photo-1581092160607-ee22621dd758?w=600';
                ?>
                <a href="news-details.php?id=<?php echo (int)$r['id']; ?>" 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($rthumb); ?>" class="w-full h-full object-cover group-hover:scale-105 transition-transform duration-500" alt="">
                    </div>
                    <div class="p-4">
                        <h3 class="font-black text-primary uppercase text-sm line-clamp-2 group-hover:text-secondary transition-colors">
                            <?php echo htmlspecialchars($r['title']); ?>
                        </h3>
                    </div>
                </a>
                <?php endforeach; ?>
            </div>
        </div>
        <?php endif; ?>
    </div>
</section>

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