<?php

declare(strict_types=1);

use App\Controllers\AuthController;
use App\Controllers\BootstrapController;
use App\Controllers\StudentsController;
use App\Controllers\InstructorsController;
use App\Controllers\AppointmentsController;
use App\Controllers\LessonTypesController;
use App\Controllers\TemplatesController;
use App\Middleware\JwtMiddleware;

return function (\Slim\App $app): void {
    // Auth routes (public)
    $app->post('/api/v1/auth/login', [AuthController::class, 'login']);
    $app->post('/api/v1/auth/logout', [AuthController::class, 'logout']);

    // Protected routes
    $app->get('/api/v1/auth/me', [AuthController::class, 'me'])
        ->add(JwtMiddleware::class);

    $app->get('/api/v1/bootstrap', [BootstrapController::class, 'handle'])
        ->add(JwtMiddleware::class);

    // CRUD routes
    $app->get('/api/v1/students', [StudentsController::class, 'index'])
        ->add(JwtMiddleware::class);
    $app->post('/api/v1/students', [StudentsController::class, 'store'])
        ->add(JwtMiddleware::class);
    $app->patch('/api/v1/students/{id}', [StudentsController::class, 'update'])
        ->add(JwtMiddleware::class);
    $app->delete('/api/v1/students/{id}', [StudentsController::class, 'destroy'])
        ->add(JwtMiddleware::class);

    $app->get('/api/v1/instructors', [InstructorsController::class, 'index'])
        ->add(JwtMiddleware::class);
    $app->post('/api/v1/instructors', [InstructorsController::class, 'store'])
        ->add(JwtMiddleware::class);
    $app->patch('/api/v1/instructors/{id}', [InstructorsController::class, 'update'])
        ->add(JwtMiddleware::class);

    $app->get('/api/v1/appointments', [AppointmentsController::class, 'index'])
        ->add(JwtMiddleware::class);
    $app->post('/api/v1/appointments', [AppointmentsController::class, 'store'])
        ->add(JwtMiddleware::class);
    $app->patch('/api/v1/appointments/{id}', [AppointmentsController::class, 'update'])
        ->add(JwtMiddleware::class);
    $app->delete('/api/v1/appointments/{id}', [AppointmentsController::class, 'destroy'])
        ->add(JwtMiddleware::class);

    // Reference data
    $app->get('/api/v1/lesson-types', [LessonTypesController::class, 'index'])
        ->add(JwtMiddleware::class);
    $app->get('/api/v1/license-class-templates', [TemplatesController::class, 'index'])
        ->add(JwtMiddleware::class);

    // Fallback 404
    $app->any('/api/v1/{path}', function ($request, $response) {
        return $this->get('response')->withStatus(404)->withJson(['message' => 'Not Found']);
    });
};