<?php
declare(strict_types=1);

/*
|--------------------------------------------------------------------------
| Student Reports
|--------------------------------------------------------------------------
*/

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

$pdo = db();

requireStudent();

$student = currentStudent($pdo);

if (!$student) {

    studentLogout();

    redirectTo('login.php');
}

/*
|--------------------------------------------------------------------------
| Reports
|--------------------------------------------------------------------------
*/

$reports = [];

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

    $stmt = $pdo->prepare("

        SELECT *
        FROM core_reports

        WHERE student_id = ?

        ORDER BY created_at DESC

    ");

    $stmt->execute([

        $student['id']

    ]);

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

?>

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

<head>

<meta charset="UTF-8">

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

<title>
Reports
</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;
}

.page{

    padding:35px;
}

.topbar{

    display:flex;

    justify-content:space-between;

    align-items:center;

    margin-bottom:30px;
}

.page-title{

    font-size:42px;

    font-weight:900;
}

.back-btn{

    text-decoration:none;

    background:#eff6ff;

    color:#2563eb;

    padding:14px 20px;

    border-radius:18px;

    font-weight:800;
}

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

.hero{

    background:
    linear-gradient(135deg,#0f172a,#1d4ed8);

    color:#fff;

    border-radius:34px;

    padding:42px;

    margin-bottom:30px;

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

.hero h1{

    font-size:46px;

    margin-bottom:16px;

    font-weight:900;
}

.hero p{

    color:#dbeafe;

    line-height:2;
}

/*
|--------------------------------------------------------------------------
| Grid
|--------------------------------------------------------------------------
*/

.grid{

    display:grid;

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

    gap:24px;
}

/*
|--------------------------------------------------------------------------
| Card
|--------------------------------------------------------------------------
*/

.card{

    background:#fff;

    border-radius:30px;

    overflow:hidden;

    border:1px solid #e2e8f0;

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

    transition:0.25s;
}

.card:hover{

    transform:translateY(-4px);
}

.card-header{

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

    color:#fff;

    padding:28px;
}

.card-header h2{

    font-size:28px;

    margin-bottom:10px;

    font-weight:900;
}

.card-header p{

    color:#dbeafe;

    line-height:1.9;
}

.card-body{

    padding:28px;
}

.info{

    display:flex;

    justify-content:space-between;

    align-items:center;

    background:#f8fafc;

    border-radius:16px;

    padding:15px 18px;

    margin-bottom:14px;
}

.info span{

    color:#64748b;

    font-weight:700;
}

.info strong{

    font-weight:800;
}

.view-btn{

    display:block;

    text-align:center;

    text-decoration:none;

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

    color:#fff;

    padding:18px;

    border-radius:18px;

    font-weight:900;

    margin-top:22px;
}

.empty{

    background:#fff;

    border-radius:30px;

    padding:70px;

    text-align:center;

    border:1px dashed #cbd5e1;
}

.empty h2{

    font-size:34px;

    margin-bottom:18px;
}

.empty p{

    color:#64748b;

    line-height:1.9;
}

@media(max-width:700px){

    .page{

        padding:20px;
    }

    .page-title{

        font-size:32px;
    }

    .hero h1{

        font-size:34px;
    }
}

</style>

</head>

<body>

<div class="page">

    <div class="topbar">

        <h1 class="page-title">

            Reports

        </h1>

        <a
        href="dashboard.php"
        class="back-btn">

            ← Dashboard

        </a>

    </div>

    <div class="hero">

        <h1>

            Academic Reports

        </h1>

        <p>

            View your performance reports,
            monthly progress,
            attendance summaries,
            and official SMI evaluations.

        </p>

    </div>

    <?php if($reports): ?>

        <div class="grid">

            <?php foreach($reports as $report): ?>

                <div class="card">

                    <div class="card-header">

                        <h2>

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

                        </h2>

                        <p>

                            <?php
                            echo e(
                                $report['description']
                                ?? 'Official student report.'
                            );
                            ?>

                        </p>

                    </div>

                    <div class="card-body">

                        <div class="info">

                            <span>
                                Report Type
                            </span>

                            <strong>

                                <?php
                                echo e(
                                    ucfirst(
                                        $report['report_type']
                                        ?? 'General'
                                    )
                                );
                                ?>

                            </strong>

                        </div>

                        <div class="info">

                            <span>
                                Created
                            </span>

                            <strong>

                                <?php
                                echo formatDate(
                                    $report['created_at']
                                );
                                ?>

                            </strong>

                        </div>

                        <div class="info">

                            <span>
                                Status
                            </span>

                            <strong>

                                Available

                            </strong>

                        </div>

                        <a
                        href="../<?php echo e($report['file_path']); ?>"
                        target="_blank"
                        class="view-btn">

                            Open Report

                        </a>

                    </div>

                </div>

            <?php endforeach; ?>

        </div>

    <?php else: ?>

        <div class="empty">

            <h2>

                No Reports Yet

            </h2>

            <p>

                Your reports will appear here
                after teachers or admins generate them.

            </p>

        </div>

    <?php endif; ?>

</div>

</body>
</html>