<?php
/**
 * Single Blog Post Page — Gmind Institute
 *
 * Featured image, title, author, content, share buttons,
 * related posts, and breadcrumb.
 *
 * Expected PHP variables:
 *   $post          — single blog post record
 *   $relatedPosts  — array of related blog posts
 *   $csrfToken     — CSRF token string
 *   $currentPage   — string 'blog'
 *   $title         — page title
 */

$post         = $post         ?? [];
$relatedPosts = $relatedPosts ?? [];

$postTitle   = $post['title']       ?? 'Post Not Found';
$postImage   = $post['image']       ?? '/images/blog-default.jpg';
$postDate    = $post['created_at']  ?? '';
$postAuthor  = $post['author_name'] ?? $post['author'] ?? 'Gmind Institute';
$postContent = $post['content']     ?? '';
$postSlug    = $post['slug']        ?? $post['id'] ?? '#';
?>

<?php include __DIR__ . '/../layouts/header.php'; ?>

<!-- ============================================================
     Breadcrumb
     ============================================================ -->
<nav class="breadcrumb-bar" aria-label="Breadcrumb">
    <div class="container">
        <ol class="breadcrumb-list">
            <li><a href="<?= url('/') ?>">Home</a></li>
            <li><a href="<?= url('/blog') ?>">Blog</a></li>
            <li aria-current="page"><?= htmlspecialchars(mb_substr($postTitle, 0, 50), ENT_QUOTES, 'UTF-8') ?><?= mb_strlen($postTitle) > 50 ? '...' : '' ?></li>
        </ol>
    </div>
</nav>

<!-- ============================================================
     Blog Post Section
     ============================================================ -->
<section class="blog-post-section section-white" aria-label="Blog post">
    <div class="container">
        <article class="blog-post-article">
            <!-- Featured Image -->
            <div class="blog-post-image">
                <img
                    src="<?= htmlspecialchars($postImage, ENT_QUOTES, 'UTF-8') ?>"
                    alt="<?= htmlspecialchars($postTitle, ENT_QUOTES, 'UTF-8') ?>"
                    loading="lazy"
                >
            </div>

            <!-- Post Header -->
            <header class="blog-post-header">
                <h1><?= htmlspecialchars($postTitle, ENT_QUOTES, 'UTF-8') ?></h1>
                <div class="blog-post-meta">
                    <span class="blog-post-date">
                        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="3" y="4" width="18" height="18" rx="2" ry="2"/><line x1="16" y1="2" x2="16" y2="6"/><line x1="8" y1="2" x2="8" y2="6"/><line x1="3" y1="10" x2="21" y2="10"/></svg>
                        <?= htmlspecialchars(date('F d, Y', strtotime($postDate)), ENT_QUOTES, 'UTF-8') ?>
                    </span>
                    <span class="blog-post-author">
                        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M20 21v-2a4 4 0 00-4-4H8a4 4 0 00-4 4v2"/><circle cx="12" cy="7" r="4"/></svg>
                        <?= htmlspecialchars($postAuthor, ENT_QUOTES, 'UTF-8') ?>
                    </span>
                </div>
            </header>

            <!-- Post Content -->
            <div class="blog-post-content">
                <?= $postContent // Content may contain HTML from admin ?>
            </div>

            <!-- Share Buttons -->
            <div class="blog-share">
                <h3>Share this post</h3>
                <div class="share-buttons">
                    <?php
                    $shareUrl = urlencode((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http') . '://' . ($_SERVER['HTTP_HOST'] ?? 'gmind.in') . '/blog/' . $postSlug);
                    $shareTitle = urlencode($postTitle);
                    ?>
                    <a href="https://www.facebook.com/sharer/sharer.php?u=<?= $shareUrl ?>" target="_blank" rel="noopener noreferrer" class="share-btn share-facebook" aria-label="Share on Facebook">
                        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="M18 2h-3a5 5 0 00-5 5v3H7v4h3v8h4v-8h3l1-4h-4V7a1 1 0 011-1h3z"/></svg>
                        Facebook
                    </a>
                    <a href="https://twitter.com/intent/tweet?url=<?= $shareUrl ?>&text=<?= $shareTitle ?>" target="_blank" rel="noopener noreferrer" class="share-btn share-twitter" aria-label="Share on Twitter">
                        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="M18.244 2.25h3.308l-7.227 8.26 8.502 11.24H16.17l-5.214-6.817L4.99 21.75H1.68l7.73-8.835L1.254 2.25H8.08l4.713 6.231zm-1.161 17.52h1.833L7.084 4.126H5.117z"/></svg>
                        Twitter
                    </a>
                    <a href="https://api.whatsapp.com/send?text=<?= $shareTitle ?>%20<?= $shareUrl ?>" target="_blank" rel="noopener noreferrer" class="share-btn share-whatsapp" aria-label="Share on WhatsApp">
                        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.01-.57-.01-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347m-5.421 7.403h-.004a9.87 9.87 0 01-5.031-1.378l-.361-.214-3.741.982.998-3.648-.235-.374a9.86 9.86 0 01-1.51-5.26c.001-5.45 4.436-9.884 9.888-9.884 2.64 0 5.122 1.03 6.988 2.898a9.825 9.825 0 012.893 6.994c-.003 5.45-4.437 9.884-9.885 9.884m8.413-18.297A11.815 11.815 0 0012.05 0C5.495 0 .16 5.335.157 11.892c0 2.096.547 4.142 1.588 5.945L.057 24l6.305-1.654a11.882 11.882 0 005.683 1.448h.005c6.554 0 11.89-5.335 11.893-11.893a11.821 11.821 0 00-3.48-8.413z"/></svg>
                        WhatsApp
                    </a>
                </div>
            </div>
        </article>
    </div>
</section>

<!-- ============================================================
     Related Posts Section
     ============================================================ -->
<?php if (!empty($relatedPosts)): ?>
<section class="related-posts-section section-offwhite" aria-label="Related posts">
    <div class="container">
        <div class="section-heading">
            <h2>Related Posts</h2>
        </div>

        <div class="blog-grid">
            <?php foreach ($relatedPosts as $rp): ?>
            <article class="blog-card">
                <div class="blog-card-image">
                    <img
                        src="<?= htmlspecialchars($rp['image'] ?? '/images/blog-default.jpg', ENT_QUOTES, 'UTF-8') ?>"
                        alt="<?= htmlspecialchars($rp['title'] ?? 'Blog Post', ENT_QUOTES, 'UTF-8') ?>"
                        loading="lazy"
                    >
                </div>
                <div class="blog-card-body">
                    <div class="blog-card-meta">
                        <span class="blog-card-date">
                            <?= htmlspecialchars(date('M d, Y', strtotime($rp['created_at'] ?? 'now')), ENT_QUOTES, 'UTF-8') ?>
                        </span>
                    </div>
                    <h3 class="blog-card-title">
                        <a href="<?= url('/blog/' . $rp['slug'] ?? $rp['id'] ?? '#') ?>">
                            <?= htmlspecialchars($rp['title'] ?? 'Untitled', ENT_QUOTES, 'UTF-8') ?>
                        </a>
                    </h3>
                    <p class="blog-card-excerpt">
                        <?= htmlspecialchars(mb_substr(strip_tags($rp['content'] ?? ''), 0, 150), ENT_QUOTES, 'UTF-8') ?>...
                    </p>
                    <a href="<?= url('/blog/' . $rp['slug'] ?? $rp['id'] ?? '#') ?>" class="blog-card-link">Read More</a>
                </div>
            </article>
            <?php endforeach; ?>
        </div>
    </div>
</section>
<?php endif; ?>

<?php include __DIR__ . '/../layouts/footer.php'; ?>
