<?php
declare(strict_types=1);

/*
|--------------------------------------------------------------------------
| Admin Dashboard
|--------------------------------------------------------------------------
*/

require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/admin-auth.php';

$pdo = db();

requireAdmin();

$admin = currentAdmin($pdo);

/*
|--------------------------------------------------------------------------
| Safe Counts
|--------------------------------------------------------------------------
*/

$totalStudents = tableExists($pdo, 'students')
    ? safeCount($pdo, "SELECT COUNT(*) FROM students")
    : 0;

$totalTeachers = tableExists($pdo, 'core_teachers')
    ? safeCount($pdo, "SELECT COUNT(*) FROM core_teachers")
    : 0;

$totalCourses = tableExists($pdo, 'core_courses')
    ? safeCount($pdo, "SELECT COUNT(*) FROM core_courses")
    : 0;

$totalJoinRequests = tableExists($pdo, 'core_course_join_requests')
    ? safeCount($pdo, "SELECT COUNT(*) FROM core_course_join_requests WHERE status = 'pending'")
    : (tableExists($pdo, 'core_join_requests') ? safeCount($pdo, "SELECT COUNT(*) FROM core_join_requests WHERE status = 'pending'") : 0);

$totalExams = tableExists($pdo, 'core_exams')
    ? safeCount($pdo, "SELECT COUNT(*) FROM core_exams")
    : 0;

$totalHomework = tableExists($pdo, 'core_homework')
    ? safeCount($pdo, "SELECT COUNT(*) FROM core_homework")
    : 0;

$totalMaterials = tableExists($pdo, 'core_materials')
    ? safeCount($pdo, "SELECT COUNT(*) FROM core_materials")
    : 0;

$totalCertificates = tableExists($pdo, 'core_certificates')
    ? safeCount($pdo, "SELECT COUNT(*) FROM core_certificates")
    : 0;

/*
|--------------------------------------------------------------------------
| Recent Students
|--------------------------------------------------------------------------
*/

$recentStudents = [];

if (tableExists($pdo, 'students')) {

    $stmt = $pdo->query("
        SELECT id, full_name, student_code, final_level, status, created_at
        FROM students
        ORDER BY id DESC
        LIMIT 6
    ");

    $recentStudents = $stmt->fetchAll();
}

/*
|--------------------------------------------------------------------------
| Recent Courses
|--------------------------------------------------------------------------
*/

$recentCourses = [];

if (tableExists($pdo, 'core_courses')) {

    $stmt = $pdo->query("
        SELECT c.*, t.full_name AS teacher_name
        FROM core_courses c
        LEFT JOIN core_teachers t ON t.id = c.teacher_id
        ORDER BY c.id DESC
        LIMIT 6
    ");

    $recentCourses = $stmt->fetchAll();
}

?>

<!DOCTYPE html>
<html lang="en">

<head>

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Admin Dashboard</title>

<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800;900&display=swap" rel="stylesheet">

<style>

*{
    margin:0;
    padding:0;
    box-sizing:border-box;
}

body{
    font-family:'Inter',sans-serif;
    background:#f3f6fb;
    color:#0f172a;
}

.wrapper{
    display:grid;
    grid-template-columns:300px 1fr;
    min-height:100vh;
}

.sidebar{
    background:linear-gradient(180deg,#0f172a,#1e293b);
    color:#fff;
    padding:30px 24px;
}

.brand{
    margin-bottom:34px;
}

.brand h1{
    font-size:32px;
    font-weight:900;
    margin-bottom:10px;
}

.brand p{
    color:#cbd5e1;
    line-height:1.8;
    font-size:14px;
}

.menu{
    display:flex;
    flex-direction:column;
    gap:10px;
}

.menu a{
    color:#cbd5e1;
    text-decoration:none;
    padding:15px 18px;
    border-radius:18px;
    font-weight:800;
    transition:0.25s;
}

.menu a:hover,
.menu a.active{
    background:rgba(255,255,255,0.08);
    color:#fff;
}

.main{
    padding:35px;
}

.topbar{
    display:flex;
    justify-content:space-between;
    align-items:center;
    margin-bottom:30px;
    gap:20px;
}

.page-title{
    font-size:42px;
    font-weight:900;
}

.logout{
    background:#fee2e2;
    color:#b91c1c;
    padding:14px 22px;
    border-radius:18px;
    text-decoration:none;
    font-weight:900;
}

.hero{
    background:linear-gradient(135deg,#2563eb,#7c3aed);
    color:#fff;
    border-radius:38px;
    padding:42px;
    margin-bottom:30px;
    box-shadow:0 30px 70px rgba(37,99,235,0.18);
    position:relative;
    overflow:hidden;
}

.hero::before{
    content:'';
    position:absolute;
    width:320px;
    height:320px;
    border-radius:50%;
    background:rgba(255,255,255,0.08);
    right:-100px;
    top:-120px;
}

.hero h2{
    font-size:46px;
    font-weight:900;
    margin-bottom:14px;
    position:relative;
}

.hero p{
    color:#e0e7ff;
    line-height:2;
    max-width:850px;
    position:relative;
}

.stats{
    display:grid;
    grid-template-columns:repeat(auto-fit,minmax(220px,1fr));
    gap:22px;
    margin-bottom:30px;
}

.stat{
    background:#fff;
    border-radius:30px;
    padding:28px;
    border:1px solid #e2e8f0;
    box-shadow:0 18px 45px rgba(15,23,42,0.05);
}

.stat span{
    display:block;
    color:#64748b;
    font-size:13px;
    font-weight:900;
    margin-bottom:14px;
}

.stat h3{
    font-size:42px;
    font-weight:900;
    color:#2563eb;
}

.grid{
    display:grid;
    grid-template-columns:1fr 1fr;
    gap:24px;
}

.card{
    background:#fff;
    border-radius:32px;
    padding:30px;
    border:1px solid #e2e8f0;
    box-shadow:0 18px 45px rgba(15,23,42,0.05);
}

.card h2{
    font-size:28px;
    font-weight:900;
    margin-bottom:22px;
}

.item{
    background:#f8fafc;
    border:1px solid #e2e8f0;
    border-radius:22px;
    padding:18px;
    margin-bottom:14px;
}

.item h3{
    font-size:18px;
    font-weight:900;
    margin-bottom:8px;
}

.item p{
    color:#64748b;
    line-height:1.8;
    font-size:14px;
}

.badge{
    display:inline-block;
    margin-top:10px;
    padding:8px 12px;
    border-radius:999px;
    background:#dbeafe;
    color:#1d4ed8;
    font-size:12px;
    font-weight:900;
}

.empty{
    color:#64748b;
    padding:40px;
    text-align:center;
    border:1px dashed #cbd5e1;
    border-radius:24px;
}

@media(max-width:1100px){
    .wrapper{
        grid-template-columns:1fr;
    }

    .sidebar{
        position:relative;
    }

    .grid{
        grid-template-columns:1fr;
    }
}

@media(max-width:700px){
    .main{
        padding:20px;
    }

    .topbar{
        flex-direction:column;
        align-items:flex-start;
    }

    .page-title{
        font-size:34px;
    }

    .hero h2{
        font-size:34px;
    }
}

</style>

</head>

<body>

<div class="wrapper">

    <aside class="sidebar">

        <div class="brand">

            <h1>SMI Admin</h1>

            <p>
                Student Gateway<br>
                Core Periods Management
            </p>

        </div>

        <nav class="menu">

            <a href="dashboard.php" class="active">Dashboard</a>
            <a href="students.php">Students</a>
            <a href="teachers.php">Teachers</a>
            <a href="courses.php">Courses</a>
            <a href="enrollments.php">Enrollments</a>
            <a href="join-requests.php">Join Requests</a>
            <a href="sessions.php">Sessions</a>
            <a href="attendance.php">Attendance</a>
            <a href="exams.php">Exams</a>
            <a href="grade-items.php">Grade Items</a>
            <a href="final-grades.php">Final Grades</a>
            <a href="materials.php">Materials</a>
            <a href="certificates.php">Certificates</a>
            <a href="reports.php">Reports</a>
            <a href="settings.php">Settings</a>
            <a href="system-health.php">System Health</a>
            <a href="activity-logs.php">Activity Logs</a>

        </nav>

    </aside>

    <main class="main">

        <div class="topbar">

            <h1 class="page-title">
                Admin Dashboard
            </h1>

            <a href="../logout.php" class="logout">
                Logout
            </a>

        </div>

        <section class="hero">

            <h2>
                Welcome, <?php echo e($admin['name'] ?? $admin['full_name'] ?? 'Admin'); ?>
            </h2>

            <p>
                Full control center for SMI Core Periods:
                students, teachers, courses, attendance, exams,
                homework, materials, certificates, and academic reports.
            </p>

        </section>

        <section class="stats">

            <div class="stat">
                <span>Total Students</span>
                <h3><?php echo $totalStudents; ?></h3>
            </div>

            <div class="stat">
                <span>Total Teachers</span>
                <h3><?php echo $totalTeachers; ?></h3>
            </div>

            <div class="stat">
                <span>Total Courses</span>
                <h3><?php echo $totalCourses; ?></h3>
            </div>

            <div class="stat">
                <span>Pending Requests</span>
                <h3><?php echo $totalJoinRequests; ?></h3>
            </div>

            <div class="stat">
                <span>Total Exams</span>
                <h3><?php echo $totalExams; ?></h3>
            </div>

            <div class="stat">
                <span>Homework</span>
                <h3><?php echo $totalHomework; ?></h3>
            </div>

            <div class="stat">
                <span>Materials</span>
                <h3><?php echo $totalMaterials; ?></h3>
            </div>

            <div class="stat">
                <span>Certificates</span>
                <h3><?php echo $totalCertificates; ?></h3>
            </div>

        </section>

        <section class="grid">

            <div class="card">

                <h2>Recent Students</h2>

                <?php if($recentStudents): ?>

                    <?php foreach($recentStudents as $student): ?>

                        <div class="item">

                            <h3>
                                <?php echo e($student['full_name']); ?>
                            </h3>

                            <p>
                                Code:
                                <?php echo e($student['student_code'] ?? 'N/A'); ?>
                                <br>
                                Level:
                                <?php echo e($student['final_level'] ?? 'N/A'); ?>
                            </p>

                            <span class="badge">
                                <?php echo e($student['status'] ?? 'unknown'); ?>
                            </span>

                        </div>

                    <?php endforeach; ?>

                <?php else: ?>

                    <div class="empty">No students found.</div>

                <?php endif; ?>

            </div>

            <div class="card">

                <h2>Recent Courses</h2>

                <?php if($recentCourses): ?>

                    <?php foreach($recentCourses as $course): ?>

                        <div class="item">

                            <h3>
                                <?php echo e($course['title']); ?>
                            </h3>

                            <p>
                                Teacher:
                                <?php echo e($course['teacher_name'] ?? 'N/A'); ?>
                                <br>
                                Code:
                                <?php echo e($course['course_code'] ?? 'N/A'); ?>
                            </p>

                            <span class="badge">
                                <?php echo e($course['status'] ?? 'active'); ?>
                            </span>

                        </div>

                    <?php endforeach; ?>

                <?php else: ?>

                    <div class="empty">No courses found.</div>

                <?php endif; ?>

            </div>

        </section>

    </main>

</div>

</body>
</html>