<?php

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

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

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

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

$repo = new UserRepository($pdo);
$user = $repo->findById($id);

if (!$user) {
    jsonResponse(false, 'User not found', null, 404);
}

jsonResponse(true, 'User fetched successfully', $user);
