<?php
require_once __DIR__ . "/../../bootstrap.php";
require_once __DIR__ . '/../../src/AdminAuth.php';

ini_set('display_errors', 0);
ini_set('log_errors', 1);
ini_set('error_log', 'error.log');

$studentMode = isset($_GET['mode']) && $_GET['mode'] === 'student';
if (!$studentMode) {
    AdminAuth::requireLogin();
}

require_once __DIR__ . '/../../src/Database.php';

$thesisId = null;
$thesis = null;
$files = [];
$error = null;

if (isset($_GET['id'])) {
    $thesisId = filter_var($_GET['id'], FILTER_VALIDATE_INT);

    if ($thesisId !== false && $thesisId > 0) {
        try {
            $db = new Database();
            $thesis = $db->getThesis($thesisId);

            if (!$thesis) {
                $error = "TFE non trouvé.";
            } else {
                $files = $db->getThesisFiles($thesisId);
            }
        } catch (Exception $e) {
            error_log("Error loading thesis: " . $e->getMessage());
            $error = "Erreur lors de la lecture des données.";
        }
    } else {
        error_log("Invalid thesis ID: " . $_GET['id']);
        $error = "Identifiant invalide.";
    }
} else {
    $error = "Aucun identifiant spécifié.";
}

function formatFileSize($bytes) {
    if ($bytes >= 1073741824) {
        return number_format($bytes / 1073741824, 2) . ' GB';
    } elseif ($bytes >= 1048576) {
        return number_format($bytes / 1048576, 2) . ' MB';
    } elseif ($bytes >= 1024) {
        return number_format($bytes / 1024, 2) . ' KB';
    } else {
        return $bytes . ' bytes';
    }
}

$pageTitle = "Récapitulatif TFE";
$isAdmin = true;
$bodyClass = $studentMode ? 'admin-body student-body' : 'admin-body';
require_once APP_ROOT . '/templates/head.php';
if (!$studentMode) {
    include APP_ROOT . '/templates/header.php';
}
include APP_ROOT . '/templates/admin/thanks.php';
require_once APP_ROOT . '/templates/admin/footer.php';
