<?php
require_once __DIR__ . '/../config.php';

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

if (
    !isset($_SESSION['admin_logged_in']) ||
    $_SESSION['admin_logged_in'] !== true
) {
    header('Location: login.php');
    exit;
}

$userId = (int)($_SESSION['admin_user_id'] ?? 0);
$termsVersion = $_POST['terms_version'] ?? '1.0';

if ($userId <= 0) {
    die('Invalid user.');
}

$stmt = $pdo->prepare("
    UPDATE admin_users
    SET
        tos_accepted_at = NOW(),
        tos_version = ?
    WHERE id = ?
");

$stmt->execute([
    $termsVersion,
    $userId
]);

header('Location: /admin/dashboard');
exit;