<?php

declare(strict_types=1);

use App\Controllers\AuthController;
use App\Controllers\CotizacionController;
use App\Controllers\PedidoController;
use App\Controllers\RepartidorController;
use App\Controllers\ReporteController;
use App\Controllers\TiendaController;
use App\Controllers\TrackingController;
use App\Controllers\UsuarioController;
use App\Controllers\ZonaController;
use App\Middleware\JwtAuthMiddleware;
use App\Middleware\RoleMiddleware;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\App;
use Slim\Routing\RouteCollectorProxy;

return function (App $app) {
    $container = $app->getContainer();

    $app->get('/health', function (Request $request, Response $response) {
        $response->getBody()->write(json_encode(['status' => 'ok', 'timestamp' => time()]));

        return $response->withHeader('Content-Type', 'application/json');
    });

    // --- Rutas públicas (sin JWT) ---
    $app->post('/auth/login', [AuthController::class, 'login']);
    $app->post('/auth/refresh', [AuthController::class, 'refresh']);
    $app->get('/pedidos/{id}/publico', [PedidoController::class, 'verPublico']);
    $app->get('/pedidos/{id}/tracking/publico', [TrackingController::class, 'verUbicacion']);

    // --- Rutas autenticadas (JWT requerido) ---
    $app->group('', function (RouteCollectorProxy $group) use ($container) {
        $group->post('/push/registrar-token', [AuthController::class, 'registrarTokenPush']);

        $group->post('/cotizacion', [CotizacionController::class, 'cotizar']);

        $group->get('/pedidos', [PedidoController::class, 'listar']);
        $group->get('/pedidos/{id}', [PedidoController::class, 'ver']);
        $group->post('/pedidos', [PedidoController::class, 'crear']);
        $group->post('/pedidos/{id}/asignar', [PedidoController::class, 'asignarRepartidor']);
        $group->post('/pedidos/{id}/estado', [PedidoController::class, 'cambiarEstado']);

        $group->post('/tracking/{id}/ubicacion', [TrackingController::class, 'registrarUbicacion']);

        $group->get('/tiendas', [TiendaController::class, 'listar']);
        $group->get('/tiendas/{id}', [TiendaController::class, 'ver']);
        $group->post('/tiendas', [TiendaController::class, 'crear']);
        $group->put('/tiendas/{id}', [TiendaController::class, 'actualizar']);

        $group->get('/repartidores', [RepartidorController::class, 'listar']);
        $group->get('/repartidores/me', [RepartidorController::class, 'yo']);
        $group->get('/repartidores/{id}', [RepartidorController::class, 'ver']);
        $group->get('/repartidores/zona/{zonaId}/disponibles', [RepartidorController::class, 'disponiblesEnZona']);
        $group->post('/repartidores/disponibilidad', [RepartidorController::class, 'actualizarDisponibilidad']);

        $group->get('/usuarios/rol/{rol}', [UsuarioController::class, 'listarPorRol']);

        $group->group('/reportes', function (RouteCollectorProxy $rep) {
            $rep->get('/resumen', [ReporteController::class, 'resumen']);
            $rep->get('/ventas', [ReporteController::class, 'ventas']);
            $rep->get('/comisiones', [ReporteController::class, 'comisiones']);
        })->add(new RoleMiddleware(['admin'], $container->get(ResponseFactoryInterface::class)));

        $group->group('/zonas', function (RouteCollectorProxy $zonas) {
            $zonas->get('', [ZonaController::class, 'listar']);
            $zonas->get('/{id}', [ZonaController::class, 'ver']);
            $zonas->post('', [ZonaController::class, 'crear']);
            $zonas->put('/{id}', [ZonaController::class, 'actualizar']);
            $zonas->delete('/{id}', [ZonaController::class, 'eliminar']);
        })->add(new RoleMiddleware(['admin'], $container->get(ResponseFactoryInterface::class)));

        $group->group('/repartidores', function (RouteCollectorProxy $r) {
            $r->post('', [RepartidorController::class, 'crear']);
        })->add(new RoleMiddleware(['admin'], $container->get(ResponseFactoryInterface::class)));

        $group->group('/usuarios', function (RouteCollectorProxy $u) {
            $u->post('', [UsuarioController::class, 'crear']);
        })->add(new RoleMiddleware(['admin'], $container->get(ResponseFactoryInterface::class)));
    })->add($container->get(JwtAuthMiddleware::class));
};
