<?php
namespace Modules\Locadora;

use Gestoreasy\Core\Module\ModuleInterface;
use Gestoreasy\Core\Routing\Router;
use Gestoreasy\Core\Auth\AuthMiddleware;
use Gestoreasy\Core\Auth\Rbac;
use Gestoreasy\Core\View\View;
use Modules\Locadora\Controllers\LocadoraController;
use Modules\Locadora\Controllers\ContratosController;
use Modules\Locadora\Controllers\FrotaController;
use Modules\Fiscal\Services\CestService;

class Module implements ModuleInterface {
    public function register(Router $router): void {
        // Carregar NCMs específicos do setor
        try {
            $cestService = new CestService();
            $cestService->carregarNcmsSetoresAutomotivos();
        } catch (\Throwable $e) {
            // Se não conseguir carregar, continua normalmente
            error_log("Erro ao carregar NCMs do setor: " . $e->getMessage());
        }
        
        $ctrl = new LocadoraController();
        $contratosCtrl = new ContratosController();
        $frotaCtrl = new FrotaController();
        
        // ==== Views (HTML) ====
        $router->get('/locadora', [$ctrl, 'viewIndex'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['locadora.read'])]);
        $router->get('/locadora/frota', [$frotaCtrl, 'viewIndex'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['locadora.frota.read'])]);
        $router->get('/locadora/frota/create', [$frotaCtrl, 'viewCreate'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['locadora.frota.create'])]);
        $router->get('/locadora/contratos', [$contratosCtrl, 'viewIndex'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['locadora.contratos.read'])]);
        $router->get('/locadora/contratos/create', [$contratosCtrl, 'viewCreate'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['locadora.contratos.create'])]);
        $router->get('/locadora/contratos/{id}', [$contratosCtrl, 'viewShow'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['locadora.contratos.read'])]);
        
        // ==== API - Frota ====
        $router->get('/api/locadora/frota', [$frotaCtrl, 'list'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['locadora.frota.read'])]);
        $router->get('/api/locadora/frota/{id}', [$frotaCtrl, 'getById'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['locadora.frota.read'])]);
        $router->post('/api/locadora/frota', [$frotaCtrl, 'create'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['locadora.frota.create'])]);
        $router->put('/api/locadora/frota/{id}', [$frotaCtrl, 'update'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['locadora.frota.update'])]);
        $router->delete('/api/locadora/frota/{id}', [$frotaCtrl, 'delete'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['locadora.frota.delete'])]);
        $router->post('/api/locadora/frota/{id}/atualizar-km', [$frotaCtrl, 'atualizarKm'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['locadora.frota.update'])]);
        $router->get('/api/locadora/frota/{id}/historico-manutencao', [$frotaCtrl, 'getHistoricoManutencao'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['locadora.frota.read'])]);
        $router->post('/api/locadora/frota/{id}/manutencao', [$frotaCtrl, 'adicionarManutencao'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['locadora.frota.update'])]);
        
        // ==== API - Contratos ====
        $router->get('/api/locadora/contratos', [$contratosCtrl, 'list'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['locadora.contratos.read'])]);
        $router->get('/api/locadora/contratos/{id}', [$contratosCtrl, 'getById'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['locadora.contratos.read'])]);
        $router->post('/api/locadora/contratos', [$contratosCtrl, 'create'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['locadora.contratos.create'])]);
        $router->put('/api/locadora/contratos/{id}', [$contratosCtrl, 'update'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['locadora.contratos.update'])]);
        $router->post('/api/locadora/contratos/{id}/encerrar', [$contratosCtrl, 'encerrar'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['locadora.contratos.update'])]);
        $router->post('/api/locadora/contratos/{id}/renovar', [$contratosCtrl, 'renovar'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['locadora.contratos.update'])]);
        $router->get('/api/locadora/contratos/{id}/multas', [$contratosCtrl, 'getMultas'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['locadora.contratos.read'])]);
        $router->post('/api/locadora/contratos/{id}/multas', [$contratosCtrl, 'adicionarMulta'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['locadora.contratos.update'])]);
        
        // ==== API - Relatórios ====
        $router->get('/api/locadora/relatorios/disponibilidade', [$ctrl, 'relatorioDisponibilidade'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['locadora.read'])]);
        $router->get('/api/locadora/relatorios/faturamento', [$ctrl, 'relatorioFaturamento'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['locadora.read'])]);
        $router->get('/api/locadora/relatorios/manutencao', [$ctrl, 'relatorioManutencao'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['locadora.read'])]);
    }
    
    public function name(): string { 
        return 'Locadora'; 
    }
    
    public function version(): string { 
        return '1.0.0'; 
    }
    
    public function dependencies(): array { 
        return ['Produtos', 'Vendas', 'Clientes', 'Financeiro']; 
    }
}

