<?php
/**
 * Course Detail Page — Gmind Institute
 *
 * Single course detail with image, description, syllabus,
 * enroll CTA, related courses, and breadcrumb.
 *
 * Expected PHP variables:
 *   $course          — single course record array
 *   $relatedCourses  — array of related course records
 *   $csrfToken       — CSRF token string
 *   $currentPage     — string 'courses'
 *   $title           — page title
 */

$course         = $course         ?? [];
$relatedCourses = $relatedCourses ?? [];
$basePath = defined('BASE_PATH') ? BASE_PATH : '/gmind';

$courseName   = getBilingual($course, 'name') ?? 'Course Not Found';
$courseCat    = $course['category']    ?? 'computer';
$courseDur    = $course['duration']    ?? 'N/A';
$courseFee    = (float) ($course['fee'] ?? 0);
$courseImg    = !empty($course['image']) ? upload_url($course['image']) : $basePath . '/images/course-default.jpg';
// Get description — try bilingual first, then fallback to raw field, then 'content' column
$courseDesc = getBilingual($course, 'description');
if (empty(trim(strip_tags($courseDesc)))) {
    // Bilingual returned empty — try raw description field directly
    $courseDesc = $course['description'] ?? '';
}
if (empty(trim(strip_tags($courseDesc)))) {
    // Still empty — try 'content' column as last resort
    $courseDesc = $course['content'] ?? '';
}

// Get syllabus — try bilingual first, then raw field
$courseSyllabus = $course['syllabus'] ?? '';
if (empty(trim(strip_tags($courseSyllabus)))) {
    $courseSyllabus = $course['content'] ?? '';
}
include __DIR__ . '/../layouts/header.php';

?>
<!-- ============================================================
     Breadcrumb
     ============================================================ -->
<nav class="breadcrumb-bar" aria-label="Breadcrumb">
    <div class="container">
        <ol class="breadcrumb-list">
            <li><a href="<?= $basePath ?>/"><?= __('course.detail.breadcrumb_home') ?></a></li>
            <li><a href="<?= $basePath ?>/courses"><?= __('course.detail.breadcrumb_courses') ?></a></li>
            <li aria-current="page"><?= htmlspecialchars($courseName, ENT_QUOTES, 'UTF-8') ?></li>
        </ol>
    </div>
</nav>

<!-- ============================================================
     Course Detail Section
     ============================================================ -->
<section class="course-detail-section section-white" aria-label="Course details">
    <div class="container">
        <div class="course-detail-layout">
            <!-- Main Content -->
            <div class="course-detail-main">
                <div class="course-detail-image">
                    <img
                        src="<?= htmlspecialchars($courseImg, ENT_QUOTES, 'UTF-8') ?>"
                        alt="<?= htmlspecialchars($courseName, ENT_QUOTES, 'UTF-8') ?>"
                        loading="lazy"
                    >
                </div>

                <div class="course-detail-header">
                    <span class="course-card-badge badge-<?= htmlspecialchars($courseCat, ENT_QUOTES, 'UTF-8') ?>">
                        <?= htmlspecialchars(ucfirst($courseCat), ENT_QUOTES, 'UTF-8') ?>
                    </span>
                    <h1><?= htmlspecialchars($courseName, ENT_QUOTES, 'UTF-8') ?></h1>
                    <div class="course-detail-meta">
                        <span>
                            <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"><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></svg>
                            <?= __('course.detail.duration') ?>: <?= htmlspecialchars($courseDur, ENT_QUOTES, 'UTF-8') ?>
                        </span>
                        <span class="fee">
                            <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"><line x1="12" y1="1" x2="12" y2="23"/><path d="M17 5H9.5a3.5 3.5 0 000 7h5a3.5 3.5 0 010 7H6"/></svg>
                            <?= __('course.detail.fee') ?>: <?= $courseFee > 0 ? '₹' . number_format($courseFee, 0) : __('courses.contact_us') ?>
                        </span>
                    </div>
                </div>

                <!-- Description -->
                <div class="course-detail-block">
                    <h2><?= __('course.detail.description') ?></h2>
                    <div class="course-detail-content">
                        <?php if (!empty($courseDesc)): ?>
                            <?= $courseDesc // Description may contain HTML from admin ?>
                        <?php else: ?>
                            <p style="color:#999; font-style:italic;"><?= __('courses.contact_for_details') ?? 'Contact us for detailed course information.' ?></p>
                        <?php endif; ?>
                    </div>
                </div>

                <!-- Syllabus -->
                <?php if (!empty($courseSyllabus)): ?>
                <div class="course-detail-block">
                    <h2><?= __('course.detail.syllabus') ?></h2>
                    <div class="course-syllabus">
                        <?= $courseSyllabus // Syllabus may contain HTML from admin ?>
                    </div>
                </div>
                <?php endif; ?>
            </div>

            <!-- Sidebar -->
            <aside class="course-detail-sidebar">
                <div class="course-sidebar-card">
                    <h3><?= __('course.detail.highlights') ?></h3>
                    <ul class="course-highlights">
                        <li>
                            <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"><polyline points="20 6 9 17 4 12"/></svg>
                            <?= htmlspecialchars($courseDur, ENT_QUOTES, 'UTF-8') ?> <?= __('course.detail.duration') ?>
                        </li>
                        <li>
                            <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"><polyline points="20 6 9 17 4 12"/></svg>
                            <?= __('course.detail.certification') ?>
                        </li>
                        <li>
                            <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"><polyline points="20 6 9 17 4 12"/></svg>
                            <?= __('course.detail.expert_faculty') ?>
                        </li>
                        <li>
                            <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"><polyline points="20 6 9 17 4 12"/></svg>
                            <?= __('course.detail.job_assistance') ?>
                        </li>
                        <li>
                            <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"><polyline points="20 6 9 17 4 12"/></svg>
                            <?= __('course.detail.exam_prep') ?>
                        </li>
                    </ul>
                    <div class="course-sidebar-fee">
                        <span class="fee-label"><?= __('course.detail.fee') ?></span>
                        <span class="fee-amount"><?= $courseFee > 0 ? '₹' . number_format($courseFee, 0) : __('courses.contact_us') ?></span>
                    </div>
                    <a href="<?= $basePath ?>/admission" class="btn-enroll btn-enroll-scarlet"><?= __('course.detail.enroll_now') ?></a>
                    <p class="course-sidebar-note"><?= __('course.detail.installment_note') ?></p>
                </div>
            </aside>
        </div>
    </div>
</section>

<!-- ============================================================
     Related Courses Section
     ============================================================ -->
<?php if (!empty($relatedCourses)): ?>
<section class="related-courses-section section-offwhite" aria-label="Related courses">
    <div class="container">
        <div class="section-heading">
            <h2><?= __('course.detail.related') ?></h2>
            <p><?= __('course.detail.related_subtitle') ?></p>
        </div>

        <div class="courses-grid courses-grid-3">
            <?php foreach ($relatedCourses as $rc): ?>
            <div class="course-card">
                <div class="course-card-image">
                    <img
                        src="<?= htmlspecialchars(!empty($rc['image']) ? upload_url($rc['image']) : $basePath . '/images/course-default.jpg', ENT_QUOTES, 'UTF-8') ?>"
                        alt="<?= htmlspecialchars(getBilingual($rc, 'name') ?? 'Course', ENT_QUOTES, 'UTF-8') ?>"
                        loading="lazy"
                    >
                    <span class="course-card-badge badge-<?= htmlspecialchars($rc['category'] ?? 'computer', ENT_QUOTES, 'UTF-8') ?>">
                        <?= htmlspecialchars(ucfirst($rc['category'] ?? 'Course'), ENT_QUOTES, 'UTF-8') ?>
                    </span>
                </div>
                <div class="course-card-body">
                    <h3 class="course-card-title"><?= htmlspecialchars(getBilingual($rc, 'name') ?? 'Course', ENT_QUOTES, 'UTF-8') ?></h3>
                    <div class="course-card-meta">
                        <span>
                            <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></svg>
                            <?= htmlspecialchars($rc['duration'] ?? 'N/A', ENT_QUOTES, 'UTF-8') ?>
                        </span>
                        <span class="fee">
                            <?php
                                $rFee = (float) ($rc['fee'] ?? 0);
                                echo $rFee > 0 ? '₹' . number_format($rFee, 0) : __('courses.contact_us');
                            ?>
                        </span>
                    </div>
                    <a href="<?= $basePath ?>/courses/<?= htmlspecialchars($rc['id'] ?? $rc['slug'] ?? '#', ENT_QUOTES, 'UTF-8') ?>" class="btn-enroll"><?= __('courses.view_details') ?></a>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
    </div>
</section>
<?php endif; include __DIR__ . '/../layouts/footer.php';
?>
