<?php

declare(strict_types=1);

require_once __DIR__ . '/../../includes/bootstrap.php';

auth_start_session();
auth_require_role(['admin']);

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(405);
    echo 'Method Not Allowed';
    exit;
}

$id = (int)($_POST['id'] ?? 0);
if ($id <= 0) {
    http_response_code(400);
    echo 'Invalid ID';
    exit;
}

try {
    $pdo = Database::pdo();
    $pdo->beginTransaction();

    $stmt = $pdo->prepare("DELETE FROM users WHERE user_id = :id AND user_type = 'customer'");
    $stmt->execute(['id' => $id]);

    $pdo->commit();
} catch (Throwable $e) {
    if (isset($pdo) && $pdo instanceof PDO && $pdo->inTransaction()) {
        $pdo->rollBack();
    }
    http_response_code(500);
    echo $e->getMessage();
    exit;
}

header('Location: /pest/public/admin/customers.php');
exit;
