<?php
declare(strict_types=1);

/*
|--------------------------------------------------------------------------
| Teacher Dashboard
|--------------------------------------------------------------------------
*/

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

$pdo = db();

requireTeacher();

$teacher = currentTeacher($pdo);

if (!$teacher) {

    teacherLogout();

    redirectTo('teacher/login.php');
}

/*
|--------------------------------------------------------------------------
| Statistics
|--------------------------------------------------------------------------
*/

$totalCourses = safeCount(
    $pdo,
    "
    SELECT COUNT(*)
    FROM core_courses
    WHERE teacher_id = ?
    ",
    [
        $teacher['id']
    ]
);

$totalStudents = safeCount(
    $pdo,
    "
    SELECT COUNT(*)
    FROM core_course_students cs
    INNER JOIN core_courses c
    ON c.id = cs.course_id
    WHERE c.teacher_id = ?
    ",
    [
        $teacher['id']
    ]
);

$totalExams = safeCount(
    $pdo,
    "
    SELECT COUNT(*)
    FROM core_exams
    WHERE teacher_id = ?
    ",
    [
        $teacher['id']
    ]
);

$totalHomework = safeCount(
    $pdo,
    "
    SELECT COUNT(*)
    FROM core_homework
    WHERE teacher_id = ?
    ",
    [
        $teacher['id']
    ]
);

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

$recentCourses = [];

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

    $stmt = $pdo->prepare("

        SELECT *
        FROM core_courses

        WHERE teacher_id = ?

        ORDER BY id DESC

        LIMIT 5

    ");

    $stmt->execute([

        $teacher['id']

    ]);

    $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>
Teacher 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:flex;

    min-height:100vh;
}

/*
|--------------------------------------------------------------------------
| Sidebar
|--------------------------------------------------------------------------
*/

.sidebar{

    width:290px;

    background:
    linear-gradient(
        180deg,
        #0f172a,
        #1e293b
    );

    color:#fff;

    padding:30px 24px;
}

.logo{

    margin-bottom:35px;
}

.logo h1{

    font-size:30px;

    font-weight:900;

    margin-bottom:10px;
}

.logo p{

    color:#cbd5e1;

    line-height:1.8;
}

.menu{

    display:flex;

    flex-direction:column;

    gap:14px;
}

.menu a{

    text-decoration:none;

    color:#fff;

    padding:16px 18px;

    border-radius:18px;

    font-weight:700;

    transition:0.25s;
}

.menu a:hover{

    background:
    rgba(255,255,255,0.08);
}

/*
|--------------------------------------------------------------------------
| Main
|--------------------------------------------------------------------------
*/

.main{

    flex:1;

    padding:35px;
}

/*
|--------------------------------------------------------------------------
| Hero
|--------------------------------------------------------------------------
*/

.hero{

    background:
    linear-gradient(
        135deg,
        #2563eb,
        #1d4ed8
    );

    color:#fff;

    border-radius:34px;

    padding:40px;

    margin-bottom:30px;

    box-shadow:
    0 35px 70px rgba(37,99,235,0.18);
}

.hero h1{

    font-size:42px;

    margin-bottom:14px;

    font-weight:900;
}

.hero p{

    color:#dbeafe;

    line-height:2;
}

/*
|--------------------------------------------------------------------------
| Stats
|--------------------------------------------------------------------------
*/

.stats{

    display:grid;

    grid-template-columns:
    repeat(auto-fit,minmax(240px,1fr));

    gap:22px;

    margin-bottom:30px;
}

.stat{

    background:#fff;

    border-radius:28px;

    padding:28px;

    border:1px solid #e2e8f0;

    box-shadow:
    0 15px 40px rgba(15,23,42,0.05);
}

.stat span{

    display:block;

    color:#64748b;

    margin-bottom:12px;

    font-size:13px;
    font-weight:800;
}

.stat h2{

    font-size:42px;

    font-weight:900;

    color:#2563eb;
}

/*
|--------------------------------------------------------------------------
| Section
|--------------------------------------------------------------------------
*/

.section{

    background:#fff;

    border-radius:30px;

    padding:30px;

    border:1px solid #e2e8f0;

    box-shadow:
    0 15px 40px rgba(15,23,42,0.05);
}

.section h2{

    font-size:30px;

    margin-bottom:24px;

    font-weight:900;
}

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

.course{

    padding:22px;

    border-radius:22px;

    background:#f8fafc;

    margin-bottom:18px;
}

.course h3{

    font-size:22px;

    margin-bottom:10px;

    font-weight:900;
}

.course p{

    color:#64748b;

    line-height:1.9;
}

.badge{

    display:inline-block;

    margin-top:14px;

    background:#eff6ff;

    color:#2563eb;

    padding:10px 16px;

    border-radius:999px;

    font-size:12px;
    font-weight:900;
}

/*
|--------------------------------------------------------------------------
| Responsive
|--------------------------------------------------------------------------
*/

@media(max-width:950px){

    .wrapper{

        flex-direction:column;
    }

    .sidebar{

        width:100%;
    }
}

@media(max-width:700px){

    .main{

        padding:20px;
    }

    .hero h1{

        font-size:34px;
    }
}

</style>

</head>

<body>

<div class="wrapper">

    <div class="sidebar">

        <div class="logo">

            <h1>

                Teacher Panel

            </h1>

            <p>

                SMI Academic
                Management System

            </p>

        </div>

        <div class="menu">

            <a href="dashboard.php">

                Dashboard

            </a>

            <a href="courses.php">

                Courses

            </a>

            <a href="sessions.php">

                Sessions

            </a>

            <a href="attendance.php">

                Attendance

            </a>

            <a href="exams.php">

                Exams

            </a>

            <a href="homework.php">

                Homework

            </a>

            <a href="shadowing.php">

                Shadowing

            </a>

            <a href="obstacles.php">

                Obstacle Removal

            </a>

            <a href="materials.php">

                Materials

            </a>

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

                Logout

            </a>

        </div>

    </div>

    <div class="main">

        <div class="hero">

            <h1>

                Welcome,
                <?php
                echo e(
                    $teacher['full_name']
                );
                ?>

            </h1>

            <p>

                Manage your courses,
                students,
                attendance,
                exams,
                homework,
                and academic progress
                inside the SMI Teacher System.

            </p>

        </div>

        <div class="stats">

            <div class="stat">

                <span>
                    Total Courses
                </span>

                <h2>

                    <?php
                    echo $totalCourses;
                    ?>

                </h2>

            </div>

            <div class="stat">

                <span>
                    Total Students
                </span>

                <h2>

                    <?php
                    echo $totalStudents;
                    ?>

                </h2>

            </div>

            <div class="stat">

                <span>
                    Total Exams
                </span>

                <h2>

                    <?php
                    echo $totalExams;
                    ?>

                </h2>

            </div>

            <div class="stat">

                <span>
                    Homework Tasks
                </span>

                <h2>

                    <?php
                    echo $totalHomework;
                    ?>

                </h2>

            </div>

        </div>

        <div class="section">

            <h2>

                Recent Courses

            </h2>

            <?php if($recentCourses): ?>

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

                    <div class="course">

                        <h3>

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

                        </h3>

                        <p>

                            <?php
                            echo e(
                                $course['description']
                                ?? 'No description available.'
                            );
                            ?>

                        </p>

                        <span class="badge">

                            Course Code:
                            <?php
                            echo e(
                                $course['course_code']
                            );
                            ?>

                        </span>

                    </div>

                <?php endforeach; ?>

            <?php else: ?>

                <p>

                    No courses found yet.

                </p>

            <?php endif; ?>

        </div>

    </div>

</div>

</body>
</html>