<?php

require_once __DIR__ . '/../../../config/helpers.php';

$route = getApiRouteSegments();
$method = getMethod();
$id = isset($route[2]) ? (int) $route[2] : null;

if ($id && $method === 'GET') {
    require __DIR__ . '/show.php';
    return;
}

if (!$id && $method === 'GET') {
    require __DIR__ . '/list.php';
    return;
}

if (!$id && $method === 'POST') {
    require __DIR__ . '/create.php';
    return;
}

if ($id && in_array($method, ['PUT', 'PATCH'], true)) {
    require __DIR__ . '/update.php';
    return;
}

if ($id && $method === 'DELETE') {
    require __DIR__ . '/delete.php';
    return;
}

jsonResponse(false, 'Users route not found', [
    'method' => $method,
    'id' => $id,
    'segments' => $route,
], 404);
