<?php

require_once __DIR__ . '/../../../config/database.php';
require_once __DIR__ . '/../../../middleware/admin.php';
require_once __DIR__ . '/UserRepository.php';

if (getMethod() !== 'DELETE') {
    jsonResponse(false, 'Method not allowed', null, 405);
}

global $pdo;
requireAdmin();
$repo = new UserRepository($pdo);
$route = getApiRouteSegments();
$id = isset($route[2]) ? (int) $route[2] : 0;

if ($id <= 0) {
    jsonResponse(false, 'Invalid user id', null, 422);
}

$current = $repo->findById($id);
if (!$current) {
    jsonResponse(false, 'User not found', null, 404);
}

$repo->delete($id);
jsonResponse(true, 'User deleted successfully');
