<?php
ini_set('display_errors', 1); error_reporting(E_ALL);
session_start();
require_once 'db_connect.php';

if (!isset($_GET['id']) || empty($_GET['id'])) {
    header("Location: courses.php");
    exit;
}

$course_id = (int)$_GET['id'];

try {
    // 1. Fetch Course, Teacher Name and Total Enrollments dynamically
    $stmt = $pdo->prepare("
        SELECT c.*, t.name as teacher_name, 
               (SELECT COUNT(*) FROM enrollments e WHERE e.course_id = c.id AND e.status='active') as total_students 
        FROM courses c 
        LEFT JOIN teachers t ON c.teacher_id = t.id 
        WHERE c.id = ? AND c.status = 'active'
    ");
    $stmt->execute([$course_id]);
    $course = $stmt->fetch(PDO::FETCH_ASSOC);

    if (!$course) {
        die("<div style='text-align:center; padding:100px; font-family:sans-serif;'><h2>Course not found.</h2><a href='courses.php'>Go Back</a></div>");
    }

    $title = $course['title'];
    $desc = $course['description'];
    $fee = $course['fee'];
    $duration = $course['duration'];
    $original_price = $fee + 50; 
    $img_url = $course['image_url'] ?? 'assets/images/logo.png';
    
    // Dynamically fetched data
    $teacher_name = $course['teacher_name'] ?? 'TBA';
    $total_students = $course['total_students'];

    // DYNAMIC DETAILS SPECIFIC TO THIS COURSE
    $c_lang = $course['language'] ?? 'English, Bengali';
    $c_platform = $course['platform'] ?? 'Zoom, Google Meet';
    $c_level = $course['level'] ?? 'Beginner to Advanced';
    $c_assessment = $course['assessment'] ?? 'Quizzes & Assignments';
    $c_cert = $course['has_certificate'] ?? 'Yes';

    // Settings
    $settings = [];
    $stmt_set = $pdo->query("SELECT setting_key, setting_value FROM settings");
    while ($row = $stmt_set->fetch(PDO::FETCH_ASSOC)) { $settings[$row['setting_key']] = $row['setting_value']; }
    
    // We use the assigned teacher's name instead of the global admin name if available
    $admin_name = ($teacher_name !== 'TBA') ? $teacher_name : ($settings['course_admin_name'] ?? 'Administrator');

} catch (PDOException $e) {
    die("Database Error.");
}

$page_title = htmlspecialchars($title) . " | Ahmad Academy";
include 'header.php'; 
?>

<style>
    :root { --primary: #064e3b; --primary-dark: #022c22; --accent: #d4af37; --bg-soft: #f8fafc; --text-dark: #1e293b; --text-muted: #64748b; --border-light: #e2e8f0; }

    .course-details-wrapper { background-color: var(--bg-soft); color: var(--text-dark); padding: 60px 0 100px; font-family: 'Outfit', sans-serif; min-height: 100vh; }
    .course-title { font-size: 42px; font-weight: 800; color: var(--primary-dark); margin-bottom: 20px; }
    .course-short-desc { font-size: 17px; line-height: 1.6; color: var(--text-muted); margin-bottom: 40px; max-width: 95%; text-align: justify;}

    .meta-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 25px; margin-bottom: 40px; padding-bottom: 40px; border-bottom: 1px solid var(--border-light); }
    .meta-item { display: flex; align-items: center; gap: 15px; }
    .meta-icon-circle { width: 45px; height: 45px; background: rgba(6, 78, 59, 0.05); border: 1px solid rgba(6, 78, 59, 0.1); border-radius: 50%; display: flex; align-items: center; justify-content: center; color: var(--primary); font-size: 18px; }
    .meta-text span { display: block; }
    .meta-text .label { font-size: 13px; color: var(--text-muted); }
    .meta-text .value { font-size: 15px; font-weight: 700; color: var(--text-dark); }
    .rating-stars { color: var(--accent); font-size: 14px; }

    .custom-nav-tabs { border-bottom: 1px solid var(--border-light); gap: 10px; margin-bottom: 30px; }
    .custom-nav-tabs .nav-link { color: var(--text-muted); border: none; background: transparent; padding: 12px 25px; font-weight: 600; font-size: 16px; border-bottom: 3px solid transparent; transition: 0.3s; }
    .custom-nav-tabs .nav-link:hover { color: var(--primary); }
    .custom-nav-tabs .nav-link.active { color: var(--primary-dark); background: #ffffff; border-bottom: 3px solid var(--primary); border-top-left-radius: 8px; border-top-right-radius: 8px; box-shadow: 0 -4px 10px rgba(0,0,0,0.02); }

    .tab-content-area { background: #ffffff; padding: 35px; border-radius: 0 12px 12px 12px; border: 1px solid var(--border-light); border-top: none; box-shadow: 0 10px 30px rgba(0,0,0,0.02); }
    .overview-list { list-style: none; padding: 0; margin: 0; }
    .overview-list li { position: relative; padding-left: 20px; margin-bottom: 15px; font-size: 16px; color: var(--text-dark); line-height: 1.6; }
    .overview-list li::before { content: '•'; position: absolute; left: 0; color: var(--primary); font-weight: bold; font-size: 18px; }
    .overview-list li strong { color: var(--primary-dark); font-weight: 700; }

    .floating-card-wrapper { position: sticky; top: 100px; }
    .pricing-card { background: #ffffff; border: 1px solid var(--border-light); border-radius: 16px; overflow: hidden; box-shadow: 0 20px 40px rgba(0,0,0,0.06); }
    .card-img-area { background: linear-gradient(135deg, var(--primary-dark), var(--primary)); padding: 40px 20px; text-align: center; display: flex; align-items: center; justify-content: center; height: 250px; border-bottom: 3px solid var(--accent); }
    .card-img-area img { max-width: 80%; max-height: 150px; object-fit: contain; filter: drop-shadow(0 4px 6px rgba(0,0,0,0.3)); }
    .card-content-area { padding: 30px; }
    .price-display { margin-bottom: 25px; display: flex; align-items: baseline; gap: 15px; }
    .current-price { font-size: 42px; font-weight: 800; color: var(--primary-dark); margin: 0; line-height: 1; }
    .old-price { font-size: 20px; color: #94a3b8; text-decoration: line-through; font-weight: 500; }
    
    .btn-enroll-now { background-color: var(--primary) !important; color: #ffffff !important; border: none !important; width: 100%; padding: 16px; border-radius: 10px; font-weight: 800; font-size: 18px; margin-bottom: 30px; transition: all 0.3s ease; box-shadow: 0 8px 20px rgba(6, 78, 59, 0.25) !important; display: block; text-align: center; text-decoration: none !important; }
    .btn-enroll-now:hover { background-color: var(--primary-dark) !important; color: var(--accent) !important; transform: translateY(-3px); box-shadow: 0 12px 25px rgba(6, 78, 59, 0.4) !important; }
    
    .card-info-list { list-style: none; padding: 0; margin: 0; }
    .card-info-list li { display: flex; justify-content: space-between; align-items: center; padding: 14px 0; border-bottom: 1px dashed var(--border-light); color: var(--text-muted); font-size: 15px; font-weight: 500; }
    .card-info-list li:last-child { border-bottom: none; }
    .card-info-list li i { width: 25px; color: var(--primary); font-size: 16px; }
    .card-info-list li .info-val { color: var(--text-dark); font-weight: 700; }

    @media (max-width: 991px) { .course-title { font-size: 32px; } .floating-card-wrapper { position: relative; top: 0; margin-top: 40px; } .meta-grid { grid-template-columns: 1fr 1fr; } .tab-content-area { padding: 25px 20px; } }
</style>

<div class="course-details-wrapper">
    <div class="container">
        <div class="row gx-lg-5">
            <div class="col-lg-8">
                
                <h1 class="course-title"><?= htmlspecialchars($title); ?></h1>
                <div class="course-short-desc">
                    <?= nl2br(htmlspecialchars($desc)); ?>
                </div>

                <div class="meta-grid">
                    <div class="meta-item">
                        <div class="meta-icon-circle"><span style="font-size: 14px; font-weight: bold;"><?= substr($admin_name, 0, 1) ?></span></div>
                        <div class="meta-text">
                            <span class="label">Instructor</span>
                            <span class="value"><?= htmlspecialchars($admin_name) ?></span>
                        </div>
                    </div>
                    <div class="meta-item">
                        <div class="meta-text">
                            <span class="label">Rating</span>
                            <span class="value">4.9 <span class="rating-stars ms-1"><i class="fas fa-star"></i><i class="fas fa-star"></i><i class="fas fa-star"></i><i class="fas fa-star"></i><i class="fas fa-star-half-alt"></i></span></span>
                        </div>
                    </div>
                    <div class="meta-item">
                        <div class="meta-text">
                            <span class="label">Language</span>
                            <span class="value"><i class="fas fa-language me-2" style="color: var(--primary);"></i> <?= htmlspecialchars(explode(',', $c_lang)[0]); ?></span>
                        </div>
                    </div>
                    <div class="meta-item">
                        <div class="meta-text">
                            <span class="label">Certification</span>
                            <span class="value"><i class="fas fa-certificate me-2" style="color: var(--primary);"></i> <?= htmlspecialchars($c_cert); ?></span>
                        </div>
                    </div>
                    <div class="meta-item">
                        <div class="meta-text">
                            <span class="label">Students</span>
                            <span class="value"><i class="fas fa-users me-2" style="color: var(--primary);"></i> <?= $total_students; ?> Enrolled</span>
                        </div>
                    </div>
                    <div class="meta-item">
                        <div class="meta-text">
                            <span class="label">Duration</span>
                            <span class="value"><i class="far fa-clock me-2" style="color: var(--primary);"></i> <?= htmlspecialchars($duration); ?></span>
                        </div>
                    </div>
                </div>

                <ul class="nav nav-tabs custom-nav-tabs" id="courseTabs" role="tablist">
                    <li class="nav-item"><button class="nav-link active" id="overview-tab" data-bs-toggle="tab" data-bs-target="#overview" type="button">Overview</button></li>
                    <li class="nav-item"><button class="nav-link" id="curriculum-tab" data-bs-toggle="tab" data-bs-target="#curriculum" type="button">Curriculum</button></li>
                    <li class="nav-item"><button class="nav-link" id="reviews-tab" data-bs-toggle="tab" data-bs-target="#reviews" type="button">Reviews</button></li>
                </ul>

                <div class="tab-content tab-content-area" id="courseTabsContent">
                    <div class="tab-pane fade show active" id="overview" role="tabpanel">
                        <ul class="overview-list">
                            <li>Duration: <strong><?= htmlspecialchars($duration); ?></strong></li>
                            <li>Language: <strong><?= htmlspecialchars($c_lang); ?></strong></li>
                            <li>Assessment: <strong><?= htmlspecialchars($c_assessment); ?></strong></li>
                            <li>Platform: <strong><?= htmlspecialchars($c_platform); ?></strong></li>
                            <li>Level: <strong><?= htmlspecialchars($c_level); ?></strong></li>
                            <li>Certificate Provided: <strong><?= htmlspecialchars($c_cert); ?></strong></li>
                        </ul>
                    </div>
                    <div class="tab-pane fade" id="curriculum" role="tabpanel"><p class="text-muted mt-2">Curriculum details are being updated.</p></div>
                    <div class="tab-pane fade" id="reviews" role="tabpanel"><p class="text-muted mt-2">No reviews yet.</p></div>
                </div>

            </div>

            <div class="col-lg-4">
                <div class="floating-card-wrapper">
                    <div class="pricing-card">
                        
                        <div class="card-img-area">
                            <img src="<?= htmlspecialchars($img_url) ?>" alt="<?= htmlspecialchars($title); ?>">
                        </div>

                        <div class="card-content-area">
                            <div class="price-display">
                                <h2 class="current-price">$<?= number_format($fee, 2); ?></h2>
                                <span class="old-price">$<?= number_format($original_price, 2); ?></span>
                            </div>
                            
                            <a href="admission.php?course=<?= $course_id; ?>" class="btn btn-enroll-now text-decoration-none">Enroll Now</a>

                            <ul class="card-info-list">
                                <li><span><i class="fas fa-user-graduate"></i> Students</span><span class="info-val"><?= $total_students; ?></span></li>
                                <li><span><i class="fas fa-language"></i> Language</span><span class="info-val"><?= htmlspecialchars(explode(',', $c_lang)[0]); ?></span></li>
                                <li><span><i class="far fa-clock"></i> Duration</span><span class="info-val"><?= htmlspecialchars($duration); ?></span></li>
                                <li><span><i class="fas fa-signal"></i> Level</span><span class="info-val"><?= htmlspecialchars(explode(' ', $c_level)[0]); ?></span></li>
                                <li><span><i class="far fa-calendar-alt"></i> Access</span><span class="info-val">Lifetime</span></li>
                                <li><span><i class="fas fa-certificate"></i> Certificate</span><span class="info-val"><?= htmlspecialchars($c_cert); ?></span></li>
                            </ul>
                        </div>

                    </div>
                </div>
            </div>

        </div>
    </div>
</div>

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