<?php

declare(strict_types=1);

require_once __DIR__ . '/../../library/auth.php';

require_once __DIR__ . '/../helpers/result-helper.php';

allow_roles(['admin', 'teacher']);

requirePostRequest();

verifyCsrfToken();

$school_id = (int) $_SESSION['school_id'];

$session_id = (int) ($_SESSION['session_id'] ?? 0);

/*
|--------------------------------------------------------------------------
| VALIDATE INPUTS
|--------------------------------------------------------------------------
*/

$result_id = validateId(
    $_POST['result_id'] ?? 0
);

if (
    $result_id <= 0 ||
    $session_id <= 0
) {

    jsonResponse(
        false,
        'Invalid request data!'
    );
}

try {

    /*
    |--------------------------------------------------------------------------
    | FETCH RESULT
    |--------------------------------------------------------------------------
    */

    $resultStmt = $pdo->prepare("
        SELECT

            er.id,
            er.total_subjects,
            er.total_marks,
            er.obtained_marks,
            er.percentage,
            er.overall_grade,
            er.rank_position,
            er.result_status,
            er.is_published,

            s.name,
            s.photo,

            ss.roll_no,

            c.class_name,

            et.exam_name

        FROM exam_results er

        INNER JOIN students s
            ON s.id = er.student_id
            AND s.school_id = er.school_id

        INNER JOIN student_sessions ss
            ON ss.student_id = er.student_id
            AND ss.school_id = er.school_id
            AND ss.session_id = er.session_id
            AND ss.class_id = er.class_id

        INNER JOIN classes c
            ON c.id = er.class_id
            AND c.school_id = er.school_id

        INNER JOIN exam_types et
            ON et.id = er.exam_type_id
            AND et.school_id = er.school_id

        WHERE er.school_id = ?
        AND er.session_id = ?
        AND er.id = ?

        LIMIT 1
    ");

    $resultStmt->execute([

        $school_id,
        $session_id,
        $result_id
    ]);

    $result = $resultStmt->fetch(PDO::FETCH_ASSOC);

    if (!$result) {

        throw new Exception(
            'Result not found!'
        );
    }

    /*
    |--------------------------------------------------------------------------
    | FETCH SUBJECTS
    |--------------------------------------------------------------------------
    */

    $subjectStmt = $pdo->prepare("
        SELECT

            erd.subject_id,
            erd.total_marks,
            erd.obtained_marks,
            erd.subject_grade,
            erd.result_status,

            sub.name AS subject_name,
            sub.subject_code

        FROM exam_result_details erd

        INNER JOIN subjects sub
            ON sub.id = erd.subject_id
            AND sub.school_id = erd.school_id

        WHERE erd.school_id = ?
        AND erd.session_id = ?
        AND erd.result_id = ?

        ORDER BY sub.name ASC
    ");

    $subjectStmt->execute([

        $school_id,
        $session_id,
        $result_id
    ]);

    $subjects = $subjectStmt->fetchAll(PDO::FETCH_ASSOC);

    /*
    |--------------------------------------------------------------------------
    | RESPONSE
    |--------------------------------------------------------------------------
    */

    jsonResponse(

        true,

        'Student result fetched successfully!',

        [

            'result' => $result,

            'subjects' => $subjects
        ]
    );

} catch (Throwable $e) {

    jsonResponse(
        false,
        $e->getMessage()
    );
}