<?php
// Safe clearing script: deletes all rows from admins and users.
// WARNING: this will remove all user and admin records. Be sure you want this.
require __DIR__ . '/../db_config.php';

echo "Clearing admins and users tables...\n";

$conn->begin_transaction();
try {
    // remove admins first (FK if any)
    if (!$conn->query('DELETE FROM admins')) throw new Exception('Failed to clear admins: ' . $conn->error);
    // remove users
    if (!$conn->query('DELETE FROM users')) throw new Exception('Failed to clear users: ' . $conn->error);

    // reset AUTO_INCREMENT to 1
    $conn->query('ALTER TABLE admins AUTO_INCREMENT = 1');
    $conn->query('ALTER TABLE users AUTO_INCREMENT = 1');

    $conn->commit();
    echo "Tables cleared and auto-increment reset.\n";
    exit(0);
} catch (Exception $e) {
    $conn->rollback();
    echo "Clear failed: " . $e->getMessage() . "\n";
    exit(1);
}
