<?php

declare(strict_types=1);

use App\Helpers\Router;
use App\Middlewares\CorsMiddleware;
use App\Middlewares\AuthMiddleware;
use App\Middlewares\RoleMiddleware;
use App\Middlewares\RateLimitMiddleware;
use App\Controllers\HealthController;
use App\Controllers\AuthController;
use App\Controllers\UserController;
use App\Controllers\EventController;
use App\Controllers\LeadController;
use App\Controllers\NoteController;
use App\Controllers\TaskController;
use App\Controllers\DashboardController;
use App\Controllers\ProductController;
use App\Controllers\MaterialController;
use App\Controllers\TemplateController;
use App\Controllers\ExportController;
use App\Controllers\ConfigurationController;
use App\Controllers\AuditController;
use App\Controllers\BackupController;
use App\Controllers\NotificationController;
use App\Controllers\RucController;
use App\Controllers\TechnicalProblemController;
use App\Controllers\MediaController;
use App\Controllers\CategoryController;
use App\Controllers\MaterialCategoryController;
use App\Controllers\SpeciesController;
use App\Controllers\PublicFormController;
use App\Middlewares\EventMiddleware;

$router = new Router();

$router->get('/api/health', [HealthController::class, 'index'], [CorsMiddleware::class]);
$router->get('/api/health/notifications', [HealthController::class, 'notifications'], [CorsMiddleware::class]);
$router->get('/api/health/test-notification', [HealthController::class, 'testNotification'], [CorsMiddleware::class]);

// CU-01 - Autenticación
$router->post('/api/auth/login', [AuthController::class, 'login'], [
    CorsMiddleware::class,
    [RateLimitMiddleware::class, 5, 300],
]);

$router->post('/api/auth/logout', [AuthController::class, 'logout'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

// CU-02 - Recuperar contraseña
$router->post('/api/auth/forgot-password', [AuthController::class, 'forgotPassword'], [
    CorsMiddleware::class,
    [RateLimitMiddleware::class, 3, 600],
]);

$router->post('/api/auth/reset-password', [AuthController::class, 'resetPassword'], [
    CorsMiddleware::class,
    [RateLimitMiddleware::class, 5, 600],
]);

// CU-03 - Activar cuenta
$router->post('/api/auth/activate-account', [AuthController::class, 'activateAccount'], [
    CorsMiddleware::class,
]);

$router->post('/api/auth/register', [AuthController::class, 'register'], [
    CorsMiddleware::class,
    [RateLimitMiddleware::class, 3, 600],
]);

// Usuario autenticado
$router->get('/api/auth/me', [AuthController::class, 'me'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

// CU-16 - Administración de usuarios (solo super_admin)
$router->get('/api/users', [UserController::class, 'index'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

$router->get('/api/users/:id', [UserController::class, 'show'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

$router->post('/api/users', [UserController::class, 'store'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

$router->put('/api/users/:id', [UserController::class, 'update'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

$router->patch('/api/users/:id/status', [UserController::class, 'toggleStatus'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

$router->post('/api/users/:id/resend-invite', [UserController::class, 'resendInvitation'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

$router->delete('/api/users/:id', [UserController::class, 'destroy'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

// Perfil de usuario (cualquier autenticado puede actualizar su propio perfil)
$router->put('/api/profile', [UserController::class, 'updateProfile'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

// Consulta RUC
$router->get('/api/ruc/:ruc', [RucController::class, 'lookup'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

// Consulta Representantes Legales por RUC
$router->get('/api/ruc/:ruc/representantes', [RucController::class, 'representatives'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

// CU-04, CU-17 - Gestión de Eventos
$router->get('/api/events', [EventController::class, 'index'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

$router->get('/api/events/my-events', [EventController::class, 'myEvents'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

$router->get('/api/events/:id', [EventController::class, 'show'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

$router->get('/api/events/:id/dashboard', [EventController::class, 'dashboard'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

$router->post('/api/events', [EventController::class, 'store'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

$router->put('/api/events/:id', [EventController::class, 'update'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

$router->patch('/api/events/:id/status', [EventController::class, 'toggleStatus'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

$router->post('/api/events/deselect', [EventController::class, 'deselect'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

$router->post('/api/events/:id/select', [EventController::class, 'select'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

$router->delete('/api/events/:id', [EventController::class, 'destroy'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

// CU-06, CU-07, CU-08 - Captura de Leads
$router->post('/api/leads/manual', [LeadController::class, 'storeManual'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    EventMiddleware::class,
]);

$router->post('/api/leads/scan-openai', [LeadController::class, 'storeScanOpenAi'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    EventMiddleware::class,
]);

$router->post('/api/leads/voice-openai', [LeadController::class, 'storeVoiceOpenAi'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    EventMiddleware::class,
]);

// Endpoint público: leads con vendedor y evento (sin auth)
$router->get('/api/public/leads', [LeadController::class, 'publicIndex'], [
    CorsMiddleware::class,
]);

// Endpoint público: formulario QR (sin auth, con rate limit)
$router->post('/api/public/form-leads', [PublicFormController::class, 'create'], [
    CorsMiddleware::class,
    [RateLimitMiddleware::class, 10, 300],
]);

// Endpoint público: validación RUC con ApiPeru (sin auth, con rate limit)
$router->get('/api/public/ruc/:ruc', [RucController::class, 'lookup'], [
    CorsMiddleware::class,
    [RateLimitMiddleware::class, 20, 300],
]);

// CU-09 - Listar leads
$router->get('/api/leads', [LeadController::class, 'index'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    EventMiddleware::class,
]);

// Referencias para autocomplete (antes de :id para no colisionar)
$router->get('/api/leads/references', [LeadController::class, 'references'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

// Duplicados (antes de :id para no colisionar)
$router->get('/api/leads/check-duplicates', [LeadController::class, 'checkDuplicates'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

// Motivos de pérdida (antes de :id para no colisionar)
$router->get('/api/leads/loss-reasons', [LeadController::class, 'lossReasons'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

// CU-12 - Detalle del lead
$router->get('/api/leads/:id', [LeadController::class, 'show'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    EventMiddleware::class,
]);

// CU-10 - Editar lead
$router->put('/api/leads/:id', [LeadController::class, 'update'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    EventMiddleware::class,
]);

// CU-11 - Cambiar estado del lead
$router->patch('/api/leads/:id/status', [LeadController::class, 'changeStatus'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    EventMiddleware::class,
]);

// Asignar lead a vendedor (solo admin)
$router->put('/api/leads/:id/assign', [LeadController::class, 'assign'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

// CU-12 - Eliminar lead
$router->delete('/api/leads/:id', [LeadController::class, 'destroy'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    EventMiddleware::class,
]);

// CU-13, CU-14 - Notas
$router->get('/api/leads/:id/notes', [NoteController::class, 'index'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    EventMiddleware::class,
]);

$router->post('/api/leads/:id/notes', [NoteController::class, 'storeText'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    EventMiddleware::class,
]);

$router->post('/api/leads/:id/voice-notes', [NoteController::class, 'storeVoice'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    EventMiddleware::class,
]);

$router->put('/api/leads/:id/notes/:noteId', [NoteController::class, 'update'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    EventMiddleware::class,
]);

$router->delete('/api/leads/:id/notes/:noteId', [NoteController::class, 'destroy'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    EventMiddleware::class,
]);

// Audio
$router->get('/api/notes/:noteId/audio', [NoteController::class, 'serveAudio'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

// Media (fotos/audios de leads)
$router->get('/api/media/:type/:filename', [MediaController::class, 'serve'], [
    CorsMiddleware::class,
]);

// Problemas técnicos
$router->get('/api/leads/:id/technical-problems', [TechnicalProblemController::class, 'index'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

$router->post('/api/leads/:id/technical-problems', [TechnicalProblemController::class, 'store'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

$router->put('/api/technical-problems/:problemId', [TechnicalProblemController::class, 'update'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

$router->patch('/api/technical-problems/:problemId/status', [TechnicalProblemController::class, 'toggleStatus'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

$router->delete('/api/technical-problems/:problemId', [TechnicalProblemController::class, 'destroy'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

// CU-15 - Tareas
$router->get('/api/leads/:id/tasks', [TaskController::class, 'index'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    EventMiddleware::class,
]);

$router->post('/api/leads/:id/tasks', [TaskController::class, 'store'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    EventMiddleware::class,
]);

$router->post('/api/tasks', [TaskController::class, 'storeAdmin'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

// Tareas del usuario (debe ir antes de /api/tasks/:id)
$router->get('/api/tasks/my-tasks', [TaskController::class, 'myTasks'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

$router->get('/api/tasks/all', [TaskController::class, 'allTasks'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

$router->put('/api/tasks/:id', [TaskController::class, 'update'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

$router->patch('/api/tasks/:id/status', [TaskController::class, 'changeStatus'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

$router->delete('/api/tasks/:id', [TaskController::class, 'destroy'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

$router->delete('/api/tasks/:id/attachments/:attachmentId', [TaskController::class, 'destroyAttachment'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

$router->get('/api/tasks/types', [TaskController::class, 'types'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

$router->post('/api/tasks/types/rename', [TaskController::class, 'renameType'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

$router->post('/api/tasks/types/delete', [TaskController::class, 'deleteType'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

// CU-05 - Dashboard
$router->get('/api/dashboard', [DashboardController::class, 'index'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    EventMiddleware::class,
]);

// CU-18 - Productos (listado, todos autenticados)
$router->get('/api/products', [ProductController::class, 'index'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

$router->get('/api/products/:id', [ProductController::class, 'show'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

// CU-18 - Productos (CRUD, solo super_admin)
$router->post('/api/products', [ProductController::class, 'store'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

$router->put('/api/products/:id', [ProductController::class, 'update'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

$router->delete('/api/products/:id', [ProductController::class, 'destroy'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

$router->patch('/api/products/:id/status', [ProductController::class, 'toggleStatus'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

// Productos de un lead
$router->get('/api/leads/:id/products', [ProductController::class, 'getLeadProducts'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    EventMiddleware::class,
]);

// Asociar/desasociar productos a leads
$router->post('/api/leads/:id/products', [ProductController::class, 'attachToLead'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    EventMiddleware::class,
]);

$router->delete('/api/leads/:id/products/:pid', [ProductController::class, 'detachFromLead'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    EventMiddleware::class,
]);

// Categorías para productos
$router->get('/api/categories', [CategoryController::class, 'index'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

$router->post('/api/categories', [CategoryController::class, 'store'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

$router->put('/api/categories/:id', [CategoryController::class, 'update'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

$router->delete('/api/categories/:id', [CategoryController::class, 'destroy'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

// Especies para productos
$router->get('/api/species', [SpeciesController::class, 'index'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

$router->get('/api/species/:id', [SpeciesController::class, 'show'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

$router->post('/api/species', [SpeciesController::class, 'store'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

$router->put('/api/species/:id', [SpeciesController::class, 'update'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

$router->delete('/api/species/:id', [SpeciesController::class, 'destroy'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

// CU-19 - Material Técnico
$router->get('/api/materials', [MaterialController::class, 'index'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

$router->get('/api/materials/:id', [MaterialController::class, 'show'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

$router->get('/api/materials/:id/download', [MaterialController::class, 'download'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

$router->get('/materials/shared/:token', [MaterialController::class, 'shared'], [
    CorsMiddleware::class,
]);

$router->get('/api/materials/shared/:token', [MaterialController::class, 'shared'], [
    CorsMiddleware::class,
]);

$router->post('/api/materials', [MaterialController::class, 'store'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

$router->put('/api/materials/:id', [MaterialController::class, 'update'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

$router->post('/api/materials/:id/update', [MaterialController::class, 'updateWithFile'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

$router->delete('/api/materials/:id', [MaterialController::class, 'destroy'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

// Categorías de materiales
$router->get('/api/material-categories', [MaterialCategoryController::class, 'index'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

$router->post('/api/material-categories', [MaterialCategoryController::class, 'store'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

$router->put('/api/material-categories/:id', [MaterialCategoryController::class, 'update'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

$router->delete('/api/material-categories/:id', [MaterialCategoryController::class, 'destroy'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

// CU-20 - Plantillas WhatsApp
$router->get('/api/templates', [TemplateController::class, 'index'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

$router->get('/api/templates/:id', [TemplateController::class, 'show'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

$router->post('/api/templates', [TemplateController::class, 'store'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

$router->put('/api/templates/:id', [TemplateController::class, 'update'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

$router->delete('/api/templates/:id', [TemplateController::class, 'destroy'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

$router->post('/api/templates/validate-variables', [TemplateController::class, 'validateVariables'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

// CU-21 - Exportar Leads
$router->post('/api/leads/export', [ExportController::class, 'exportLeads'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    EventMiddleware::class,
]);

$router->post('/api/leads/export-sheets', [ExportController::class, 'exportToSheets'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    EventMiddleware::class,
]);

// CU-22 - Configuraciones
$router->get('/api/configurations', [ConfigurationController::class, 'index'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

$router->put('/api/configurations/:key', [ConfigurationController::class, 'update'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

// CU-23 - Auditoría
$router->get('/api/audit-logs', [AuditController::class, 'index'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

// CU-24 - Respaldos
$router->get('/api/backups', [BackupController::class, 'index'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

$router->post('/api/backups', [BackupController::class, 'store'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

$router->get('/api/backups/:id/download', [BackupController::class, 'download'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

$router->post('/api/backups/:id/restore', [BackupController::class, 'restore'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
    [RoleMiddleware::class, 'super_admin'],
]);

// CU-25 - Notificaciones
$router->get('/api/notifications', [NotificationController::class, 'index'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

$router->patch('/api/notifications/:id/read', [NotificationController::class, 'markAsRead'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

$router->patch('/api/notifications/read-all', [NotificationController::class, 'markAllAsRead'], [
    CorsMiddleware::class,
    AuthMiddleware::class,
]);

$router->dispatch();
