<?php
namespace Modules\Autopecas;

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\Autopecas\Controllers\AutopecasController;
use Modules\Autopecas\Controllers\OrdemServicoController;
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 AutopecasController();
        $osCtrl = new OrdemServicoController();
        
        // ==== Views (HTML) ====
        $router->get('/autopecas', [$ctrl, 'viewIndex'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['autopecas.read'])]);
        $router->get('/autopecas/create', [$ctrl, 'viewCreate'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['autopecas.create'])]);
        $router->get('/autopecas/{id}', [$ctrl, 'viewShow'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['autopecas.read'])]);
        $router->get('/autopecas/{id}/edit', [$ctrl, 'viewEdit'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['autopecas.update'])]);
        
        // Ordem de Serviço
        $router->get('/autopecas/ordens-servico', [$osCtrl, 'viewIndex'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['autopecas.os.read'])]);
        $router->get('/autopecas/ordens-servico/create', [$osCtrl, 'viewCreate'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['autopecas.os.create'])]);
        $router->get('/autopecas/ordens-servico/{id}', [$osCtrl, 'viewShow'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['autopecas.os.read'])]);
        
        // ==== API - Peças ====
        $router->get('/api/autopecas/pecas', [$ctrl, 'listPecas'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['autopecas.read'])]);
        $router->get('/api/autopecas/pecas/{id}', [$ctrl, 'getPeca'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['autopecas.read'])]);
        $router->post('/api/autopecas/pecas', [$ctrl, 'createPeca'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['autopecas.create'])]);
        $router->put('/api/autopecas/pecas/{id}', [$ctrl, 'updatePeca'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['autopecas.update'])]);
        $router->delete('/api/autopecas/pecas/{id}', [$ctrl, 'deletePeca'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['autopecas.delete'])]);
        
        // Busca de compatibilidade
        $router->get('/api/autopecas/pecas/{id}/compatibilidade', [$ctrl, 'getCompatibilidade'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['autopecas.read'])]);
        $router->post('/api/autopecas/pecas/{id}/compatibilidade', [$ctrl, 'addCompatibilidade'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['autopecas.update'])]);
        $router->delete('/api/autopecas/pecas/{id}/compatibilidade/{compatId}', [$ctrl, 'removeCompatibilidade'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['autopecas.update'])]);
        
        // Busca por modelo/marca/ano
        $router->get('/api/autopecas/pecas/buscar', [$ctrl, 'buscarPorModelo'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['autopecas.read'])]);
        
        // ==== API - Ordem de Serviço ====
        $router->get('/api/autopecas/ordens-servico', [$osCtrl, 'list'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['autopecas.os.read'])]);
        $router->get('/api/autopecas/ordens-servico/{id}', [$osCtrl, 'getById'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['autopecas.os.read'])]);
        $router->post('/api/autopecas/ordens-servico', [$osCtrl, 'create'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['autopecas.os.create'])]);
        $router->put('/api/autopecas/ordens-servico/{id}', [$osCtrl, 'update'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['autopecas.os.update'])]);
        $router->post('/api/autopecas/ordens-servico/{id}/finalizar', [$osCtrl, 'finalizar'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['autopecas.os.update'])]);
        $router->post('/api/autopecas/ordens-servico/{id}/cancelar', [$osCtrl, 'cancelar'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['autopecas.os.update'])]);
        
        // Itens da OS
        $router->post('/api/autopecas/ordens-servico/{id}/itens', [$osCtrl, 'addItem'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['autopecas.os.update'])]);
        $router->put('/api/autopecas/ordens-servico/{id}/itens/{itemId}', [$osCtrl, 'updateItem'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['autopecas.os.update'])]);
        $router->delete('/api/autopecas/ordens-servico/{id}/itens/{itemId}', [$osCtrl, 'removeItem'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['autopecas.os.update'])]);
    }
    
    public function name(): string { 
        return 'Autopecas'; 
    }
    
    public function version(): string { 
        return '1.0.0'; 
    }
    
    public function dependencies(): array { 
        return ['Produtos', 'Vendas', 'Clientes']; 
    }
}

