<?php
// views/portal.php

require_once __DIR__ . '/../src/Helpers/Language.php';
require_once __DIR__ . '/../src/Helpers/Auth.php';
require_once __DIR__ . '/../src/Controllers/PortalController.php';

if (!Auth::check()) {
    header('Location: index.php?msg=please_login');
    exit;
}

$user = Auth::user();
$enrollments = PortalController::getStudentEnrollments($user['id']);
$liveClasses = PortalController::getLiveClasses();
$lang = Language::current();
?>

<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8 space-y-8" dir="<?= Language::dir() ?>">
  
  <!-- Profile & Welcome Banner -->
  <div class="bg-gradient-to-r from-teal-900 via-teal-950 to-emerald-950 text-white rounded-3xl p-6 md:p-8 shadow-xl border border-teal-800 relative overflow-hidden">
    <div class="relative z-10 flex flex-col md:flex-row items-start md:items-center justify-between gap-6">
      <div class="flex items-center gap-4">
        <div class="w-16 h-16 rounded-2xl bg-amber-500 text-teal-950 font-black text-2xl flex items-center justify-center shadow-lg border-2 border-amber-300">
          <?= mb_substr($user['name'], 0, 1, 'UTF-8') ?>
        </div>
        <div>
          <span class="bg-teal-800 text-amber-300 text-[10px] font-bold px-3 py-1 rounded-full uppercase tracking-wider">
            Verified Student Scholar
          </span>
          <h1 class="text-2xl md:text-3xl font-extrabold mt-1"><?= htmlspecialchars($user['name']) ?></h1>
          <p class="text-xs text-teal-200 mt-0.5"><?= htmlspecialchars($user['email']) ?> • <?= htmlspecialchars($user['phone']) ?></p>
        </div>
      </div>

      <!-- Quick Stats -->
      <div class="flex items-center gap-4 bg-teal-900/60 p-4 rounded-2xl border border-teal-800/80 w-full md:w-auto justify-around">
        <div class="text-center px-3">
          <span class="block text-2xl font-black text-amber-400"><?= count($enrollments) ?></span>
          <span class="text-[10px] text-teal-200 uppercase font-bold"><?= $lang === 'ar' ? 'المقررات المسجلة' : 'Enrolled' ?></span>
        </div>
        <div class="w-px h-8 bg-teal-800"></div>
        <div class="text-center px-3">
          <span class="block text-2xl font-black text-emerald-400">
            <?= count(array_filter($enrollments, fn($e) => $e['status'] === 'completed')) ?>
          </span>
          <span class="text-[10px] text-teal-200 uppercase font-bold"><?= Language::t('completed') ?></span>
        </div>
      </div>
    </div>
  </div>

  <?php if (isset($_GET['enrolled']) && $_GET['enrolled'] === 'success'): ?>
    <div class="bg-emerald-50 border border-emerald-300 text-emerald-800 px-4 py-3 rounded-2xl text-xs font-bold flex items-center gap-2">
      <span>🎉</span>
      <span>Congratulations! You have successfully enrolled in your course.</span>
    </div>
  <?php endif; ?>

  <div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
    
    <!-- Enrolled Courses List (2 Columns) -->
    <div class="lg:col-span-2 space-y-6">
      <h2 class="text-xl font-extrabold text-gray-900 flex items-center gap-2">
        <span>📖</span>
        <span><?= Language::t('enrolledCourses') ?></span>
      </h2>

      <?php if (empty($enrollments)): ?>
        <div class="bg-white p-8 rounded-3xl border border-gray-150 text-center space-y-3 shadow-sm">
          <span class="text-4xl">📚</span>
          <h3 class="text-base font-bold text-gray-700"><?= Language::t('noEnrollments') ?></h3>
          <a href="index.php" class="inline-block bg-teal-800 hover:bg-teal-900 text-white text-xs font-bold px-4 py-2.5 rounded-xl shadow-md transition-all">
            <?= Language::t('exploreCourses') ?>
          </a>
        </div>
      <?php else: ?>
        <div class="space-y-4">
          <?php foreach ($enrollments as $e): 
            $cTitle = $lang === 'ar' ? $e['title_ar'] : $e['title_en'];
            $cCategory = $lang === 'ar' ? $e['category_ar'] : $e['category_en'];
            $cInstructor = $lang === 'ar' ? $e['instructor_ar'] : $e['instructor_en'];
            $cModules = $lang === 'ar' ? $e['modules_ar'] : $e['modules_en'];
            $completedMods = $e['completed_modules'];
          ?>
            <div class="bg-white rounded-3xl p-6 border border-gray-150 shadow-md space-y-4">
              <div class="flex items-start justify-between gap-4">
                <div class="space-y-1">
                  <span class="bg-teal-50 text-teal-800 text-[10px] font-bold px-2.5 py-0.5 rounded-full uppercase">
                    <?= htmlspecialchars($cCategory) ?>
                  </span>
                  <h3 class="text-lg font-bold text-gray-900"><?= htmlspecialchars($cTitle) ?></h3>
                  <p class="text-xs text-gray-500">Instructor: <span class="font-semibold text-gray-700"><?= htmlspecialchars($cInstructor) ?></span></p>
                </div>
                <div class="text-right">
                  <span class="text-xs font-extrabold text-teal-800 bg-teal-100 px-3 py-1 rounded-xl">
                    <?= $e['progress'] ?>% <?= Language::t('progress') ?>
                  </span>
                </div>
              </div>

              <!-- Progress Bar -->
              <div class="w-full bg-gray-100 h-2.5 rounded-full overflow-hidden">
                <div class="bg-gradient-to-r from-teal-600 to-emerald-500 h-full transition-all duration-500" style="width: <?= $e['progress'] ?>%"></div>
              </div>

              <!-- Module Checklist -->
              <div class="bg-gray-50 p-4 rounded-2xl border border-gray-100 space-y-2">
                <h4 class="text-xs font-bold text-gray-700 uppercase tracking-wider mb-2">
                  <?= $lang === 'ar' ? 'جدول الفصول والتقدم' : 'Course Modules Progress' ?>
                </h4>
                <div class="space-y-2">
                  <?php foreach ($cModules as $idx => $mTitle): 
                    $isDone = in_array($idx, $completedMods);
                  ?>
                    <div class="flex items-center justify-between p-2.5 bg-white rounded-xl border border-gray-100 text-xs">
                      <span class="flex items-center gap-2 <?= $isDone ? 'line-through text-gray-400' : 'font-medium text-gray-800' ?>">
                        <span class="w-5 h-5 rounded-full <?= $isDone ? 'bg-emerald-500 text-white' : 'bg-gray-100 text-gray-500' ?> flex items-center justify-center text-[10px] font-bold">
                          <?= $idx + 1 ?>
                        </span>
                        <span><?= htmlspecialchars($mTitle) ?></span>
                      </span>
                      <button onclick="toggleModule('<?= $e['course_id'] ?>', <?= $idx ?>)" class="text-[11px] font-bold px-3 py-1 rounded-lg transition-all <?= $isDone ? 'bg-emerald-100 text-emerald-800 hover:bg-emerald-200' : 'bg-teal-700 text-white hover:bg-teal-800' ?>">
                        <?= $isDone ? Language::t('moduleCompleted') : Language::t('markModuleComplete') ?>
                      </button>
                    </div>
                  <?php endforeach; ?>
                </div>
              </div>

            </div>
          <?php endforeach; ?>
        </div>
      <?php endif; ?>
    </div>

    <!-- Live Classes & Schedule Sidebar (1 Column) -->
    <div id="live" class="space-y-6">
      <h2 class="text-xl font-extrabold text-gray-900 flex items-center gap-2">
        <span>📹</span>
        <span><?= Language::t('upcomingLive') ?></span>
      </h2>

      <div class="space-y-4">
        <?php foreach ($liveClasses as $live): 
          $lTitle = $lang === 'ar' ? $live['title_ar'] : $live['title_en'];
          $lDuration = $lang === 'ar' ? $live['duration_ar'] : $live['duration_en'];
        ?>
          <div class="bg-gradient-to-br from-teal-900 to-teal-950 text-white rounded-3xl p-5 border border-teal-800 shadow-md space-y-3">
            <div class="flex items-center justify-between text-[11px]">
              <span class="bg-amber-400 text-teal-950 font-extrabold px-2.5 py-0.5 rounded-full">
                <?= $live['platform'] ?>
              </span>
              <span class="text-teal-200 font-mono"><?= $live['class_date'] ?> @ <?= $live['class_time'] ?></span>
            </div>
            <h3 class="font-bold text-sm leading-snug text-white"><?= htmlspecialchars($lTitle) ?></h3>
            <p class="text-xs text-teal-300">Instructor: <?= htmlspecialchars($live['instructor_en']) ?></p>
            <a href="<?= htmlspecialchars($live['link']) ?>" target="_blank" class="block w-full text-center bg-emerald-500 hover:bg-emerald-400 text-teal-950 font-extrabold py-2.5 rounded-xl text-xs shadow-md transition-all">
              <?= Language::t('joinClass') ?> (<?= $live['platform'] ?>)
            </a>
          </div>
        <?php endforeach; ?>
      </div>
    </div>

  </div>
</div>
