<?php
/**
 * Thanks page for share-link submissions.
 * Displays a centered confirmation with a link to create another thesis via the same link.
 */
if (!defined('APP_ROOT')) {
    require_once __DIR__ . '/../../bootstrap.php';
    App::boot();
}

$thesisId = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($thesisId <= 0) {
    http_response_code(400);
    die('ID de TFE invalide.');
}

// Verify the thesis exists
$db = Database::getInstance();
$thesis = $db->getThesis($thesisId);
if (!$thesis) {
    http_response_code(404);
    die('TFE introuvable.');
}

// Was the confirmation e-mail sent?
$emailSent = !empty($_SESSION['share_email_sent']);
unset($_SESSION['share_email_sent']);

// Get the share link slug from the referer path
$pathParts = explode('/', trim($_SERVER['REQUEST_URI'] ?? '', '/'));
$slug = count($pathParts) >= 2 ? $pathParts[0] : null;

$pageTitle = 'Merci — TFE enregistré';
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= htmlspecialchars($pageTitle) ?></title>
    <link rel="stylesheet" href="<?= App::assetV('/assets/css/common.css') ?>">
    <link rel="stylesheet" href="<?= App::assetV('/assets/css/form.css') ?>">
</head>
<body class="student-body">
    <main id="main-content" class="thanks-student-page">
        <div class="thanks-success">
            <h1>✅ Merci !</h1>
            <p class="thanks-message">Votre TFE a bien été enregistré sur la plateforme.</p>
            <?php if ($emailSent): ?>
            <p class="flash-success">📧 Un e-mail de confirmation a été envoyé avec un récapitulatif de votre soumission.</p>
            <?php endif; ?>
            <?php if ($thesis): ?>
            <p><strong><?= htmlspecialchars($thesis['title']) ?></strong><?php if (!empty($thesis['authors'])): ?> — <?= htmlspecialchars($thesis['authors']) ?><?php endif; ?></p>
            <?php endif; ?>
            <?php if ($slug): ?>
            <a href="/partage/<?= urlencode($slug) ?>" class="btn-new-form">+ Soumettre un autre TFE</a>
            <?php endif; ?>
        </div>
    </main>
</body>
</html>
