<?php
require_once __DIR__ . '/../config/db.php';
require_once __DIR__ . '/../auth/protect_admin.php';

// Ambil seluruh UID yang assigned
$uids = $pdo->query("SELECT uid FROM siswa WHERE uid IS NOT NULL")->fetchAll(PDO::FETCH_COLUMN);

if ($uids) {
    // Kembalikan ke pool (hindari duplicate key)
    $stmt = $pdo->prepare("
        INSERT INTO uid_pool (uid)
        VALUES (:uid)
        ON DUPLICATE KEY UPDATE first_seen = first_seen
    ");

    foreach ($uids as $u) {
        $stmt->execute([':uid' => $u]);
    }

    // Set UID siswa menjadi NULL
    $pdo->query("UPDATE siswa SET uid = NULL");
}

echo json_encode(['status' => 'ok']);
exit;
