<?php

declare(strict_types=1);

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

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

allow_roles(['admin']);

requirePostRequest();

verifyCsrfToken();

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

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

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

$passing_percentage = (float) (
    $_POST['passing_percentage'] ?? 33
);

$show_rank = (int) (
    $_POST['show_rank'] ?? 1
);

$show_grade = (int) (
    $_POST['show_grade'] ?? 1
);

$show_percentage = (int) (
    $_POST['show_percentage'] ?? 1
);

$auto_publish_results = (int) (
    $_POST['auto_publish_results'] ?? 0
);

$remarks_enabled = (int) (
    $_POST['remarks_enabled'] ?? 1
);

/*
|--------------------------------------------------------------------------
| VALIDATIONS
|--------------------------------------------------------------------------
*/

if (
    $passing_percentage < 1 ||
    $passing_percentage > 100
) {

    jsonResponse(
        false,
        'Passing percentage must be between 1 and 100!'
    );
}

try {

    /*
    |--------------------------------------------------------------------------
    | SAVE SETTINGS
    |--------------------------------------------------------------------------
    */

    $stmt = $pdo->prepare("
        INSERT INTO exam_result_settings (

            school_id,
            passing_percentage,
            show_rank,
            show_grade,
            show_percentage,
            auto_publish_results,
            remarks_enabled

        ) VALUES (

            ?, ?, ?, ?, ?, ?, ?

        )

        ON DUPLICATE KEY UPDATE

            passing_percentage = VALUES(passing_percentage),
            show_rank = VALUES(show_rank),
            show_grade = VALUES(show_grade),
            show_percentage = VALUES(show_percentage),
            auto_publish_results = VALUES(auto_publish_results),
            remarks_enabled = VALUES(remarks_enabled),
            updated_at = CURRENT_TIMESTAMP
    ");

    $stmt->execute([

        $school_id,
        $passing_percentage,
        $show_rank,
        $show_grade,
        $show_percentage,
        $auto_publish_results,
        $remarks_enabled
    ]);

    /*
    |--------------------------------------------------------------------------
    | AUDIT LOG
    |--------------------------------------------------------------------------
    */

    $auditStmt = $pdo->prepare("
        INSERT INTO audit_logs (

            school_id,
            user_id,
            module,
            action,
            message,
            ip_address,
            user_agent

        ) VALUES (

            ?, ?, ?, ?, ?, ?, ?
        )
    ");

    $auditStmt->execute([

        $school_id,
        $user_id,
        'Exams',
        'Save Result Settings',
        'Result settings updated successfully',

        $_SERVER['REMOTE_ADDR'] ?? '',

        $_SERVER['HTTP_USER_AGENT'] ?? ''
    ]);

    jsonResponse(
        true,
        'Result settings saved successfully!'
    );

} catch (Throwable $e) {

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