<?php
declare(strict_types=1);
require_once __DIR__ . "/../includes/attestation_qr.php";

header("Content-Type: text/html; charset=UTF-8");

$encodedPayload = trim((string) ($_GET["d"] ?? ""));
$signature = trim((string) ($_GET["s"] ?? ""));

$payload = null;
$isValid = false;
$error = "";

if ($encodedPayload === "" || $signature === "") {
    $error = "Lien de verification incomplet.";
} else {
    $signatureIsValid = attestationVerifySignature($encodedPayload, $signature);
    $decodedPayload = attestationDecodePayload($encodedPayload);

    if (!$signatureIsValid || !is_array($decodedPayload)) {
        $error = "Ce QR code est invalide ou a ete modifie.";
    } else {
        $requiredKeys = [
            "n",
            "p",
            "dn",
            "ln",
            "mf",
            "no",
            "dd",
        ];

        $missing = [];
        foreach ($requiredKeys as $key) {
            if (trim((string) ($decodedPayload[$key] ?? "")) === "") {
                $missing[] = $key;
            }
        }

        if ($missing !== []) {
            $error = "Donnees de verification incompletes.";
        } else {
            $isValid = true;
            $payload = $decodedPayload;
        }
    }
}

function h(string $value): string
{
    return htmlspecialchars($value, ENT_QUOTES, "UTF-8");
}
?>
<!doctype html>
<html lang="fr">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Verification d'attestation</title>
    <link rel="stylesheet" href="/PIS/assets/css/style.css">
    <style>
        .verify-shell { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px 16px; }
        .verify-card { width: min(760px, 96vw); background: #fff; border-radius: 18px; border: 1px solid #d4def0; box-shadow: 0 16px 36px rgba(15, 42, 86, 0.14); padding: 22px; }
        .verify-title { margin: 0 0 8px; font-size: 30px; color: #0f2a56; }
        .verify-subtitle { margin: 0 0 18px; color: #334155; }
        .status-box { display: inline-block; font-weight: 800; letter-spacing: 0.6px; border-radius: 999px; padding: 8px 14px; margin-bottom: 16px; }
        .status-valid { background: #dcfce7; color: #166534; border: 1px solid #86efac; }
        .status-invalid { background: #fee2e2; color: #991b1b; border: 1px solid #fca5a5; }
        .verify-grid { display: grid; grid-template-columns: 220px 1fr; gap: 10px 16px; margin-top: 8px; }
        .verify-label { font-weight: 700; color: #0f2a56; }
        .verify-value { color: #0f172a; word-break: break-word; }
        .verify-error { margin: 8px 0 0; color: #991b1b; font-weight: 600; }
        .verify-meta { margin-top: 18px; padding-top: 12px; border-top: 1px solid #e2e8f0; color: #475569; font-size: 13px; }
        @media (max-width: 640px) {
            .verify-card { padding: 18px; }
            .verify-title { font-size: 24px; }
            .verify-grid { grid-template-columns: 1fr; gap: 6px; }
            .verify-label { margin-top: 8px; }
        }
    </style>
</head>
<body>
<main class="verify-shell">
    <section class="verify-card">
        <h1 class="verify-title">Verification d'attestation</h1>
        <p class="verify-subtitle">Contrôle du QR code emis par le Pôle Informatique et Services.</p>

        <?php if ($isValid): ?>
            <div class="status-box status-valid">STATUT : VALIDE</div>
            <div class="verify-grid">
                <div class="verify-label">Numero d'attestation</div>
                <div class="verify-value"><?= h((string) $payload["no"]) ?></div>
                <div class="verify-label">Date de delivrance</div>
                <div class="verify-value"><?= h((string) $payload["dd"]) ?></div>
                <div class="verify-label">Nom</div>
                <div class="verify-value"><?= h((string) $payload["n"]) ?></div>
                <div class="verify-label">Prenom</div>
                <div class="verify-value"><?= h((string) $payload["p"]) ?></div>
                <div class="verify-label">Date de naissance</div>
                <div class="verify-value"><?= h((string) $payload["dn"]) ?></div>
                <div class="verify-label">Lieu de naissance</div>
                <div class="verify-value"><?= h((string) $payload["ln"]) ?></div>
                <div class="verify-label">Module de formation</div>
                <div class="verify-value"><?= h((string) $payload["mf"]) ?></div>
            </div>
            <div class="verify-label"> Ecrivez ou appelez sur ce numero pour confirmer l'authenticité de ce document.</div>
            <div class="verify-value">(+228)90893394</div>
        <?php else: ?>
            <div class="status-box status-invalid">STATUT : INVALIDE</div>
            <p class="verify-error"><?= h($error) ?></p>
        <?php endif; ?>

        <div class="verify-meta">
            Verification effectuee le <?= h(date("d/m/Y H:i")) ?>.
        </div>
    </section>
</main>
</body>
</html>
