<?php
// tools/test_token.php

session_start();
require_once __DIR__ . '/../../includes/init.php';
require_once __DIR__ . '/../../auth/AuthController.php';

$auth = new AuthController();
if (!$auth->isLoggedIn()) {
    header('Location: ' . $adminLoginPath);
    exit;
}

try {
    $db = Database::getInstance();
    $email = 'test@example.com';
    $token = bin2hex(random_bytes(16));
    $expires = (new DateTime('+1 hour'))->format('Y-m-d H:i:s');

    $insert = $db->prepare('INSERT INTO password_resets (email, reset_token, expires_at, used) VALUES (?, ?, ?, 0)');
    $insert->execute([$email, $token, $expires]);
    ErrorLogger::logSqlSuccess("Test-Token eingefuegt | table: password_resets | context: " . json_encode(["email" => $email], JSON_UNESCAPED_UNICODE));

    echo "Token erfolgreich eingefügt: $token\n";

    $select = $db->prepare('SELECT * FROM password_resets WHERE email = ? ORDER BY id DESC LIMIT 1');
    $select->execute([$email]);
    ErrorLogger::logSqlSuccess("Test-Token geladen | table: password_resets | context: " . json_encode(["email" => $email], JSON_UNESCAPED_UNICODE));
    $entry = $select->fetch();

    echo "Letzter Eintrag:\n";
    print_r($entry);
} catch (Exception $e) {
    ErrorLogger::logSqlError("Fehler bei Test-Token DB-Operation | table: password_resets | error: " . $e->getMessage());
    echo 'Fehler: ' . $e->getMessage();
}
