<?php
namespace Modules\Motopecas;

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

