<?php
namespace Modules\GestorY;

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\GestorY\Controllers\GestorYController;
use Modules\GestorY\Controllers\GestorYViewsController;

class Module implements ModuleInterface {
    public function register(Router $router): void {
        $ctrl = new GestorYController();
        $views = new GestorYViewsController();
        
        // Views (HTML)
        $router->get('/gestor-y', [$views, 'viewIndex'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['gestory.read'])]);
        $router->get('/gestor-y/pesquisa', [$views, 'viewPesquisa'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['gestory.read'])]);
        $router->get('/gestor-y/config', [$views, 'viewConfig'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['gestory.update'])]);
        $router->get('/gestor-y/logs', [$views, 'viewLogs'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['gestory.read'])]);

        // API - Pesquisa
        $router->get('/api/gestor-y/pesquisar', [$ctrl, 'pesquisarPecas'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['gestory.read'])]);
        $router->get('/api/gestor-y/peca/{codigo}', [$ctrl, 'getPecaDetalhes'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['gestory.read'])]);
        
        // API - Busca de Imagens
        $router->get('/api/gestor-y/buscar-imagem', [$ctrl, 'buscarImagem'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['gestory.read'])]);
        
        // API - Manuais Yamaha
        $router->get('/api/gestor-y/modelos', [$ctrl, 'listarModelos'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['gestory.read'])]);
        $router->get('/api/gestor-y/manual', [$ctrl, 'buscarManual'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['gestory.read'])]);
        $router->post('/api/gestor-y/baixar-manual', [$ctrl, 'baixarManual'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['gestory.update'])]);
        $router->post('/api/gestor-y/extrair-pecas-manual', [$ctrl, 'extrairPecasManual'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['gestory.update'])]);
        
        // API - Ordem de Compra
        $router->post('/api/gestor-y/ordem-compra', [$ctrl, 'criarOrdemCompra'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['gestory.update'])]);
        $router->get('/api/gestor-y/ordens-compra', [$ctrl, 'listarOrdensCompra'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['gestory.read'])]);
        $router->get('/api/gestor-y/ordem-compra/{id}', [$ctrl, 'getOrdemCompra'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['gestory.read'])]);
        
        // API - Importação
        $router->post('/api/gestor-y/importar', [$ctrl, 'importarPeca'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['gestory.update'])]);
        $router->post('/api/gestor-y/importar-lote', [$ctrl, 'importarLote'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['gestory.update'])]);
        
        // API - Configuração
        $router->get('/api/gestor-y/config', [$ctrl, 'getConfig'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['gestory.read'])]);
        $router->put('/api/gestor-y/config', [$ctrl, 'updateConfig'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['gestory.update'])]);
        
        // API - Atualização automática
        $router->post('/api/gestor-y/atualizar', [$ctrl, 'executarAtualizacao'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['gestory.update'])]);
        
        // API - Estatísticas
        $router->get('/api/gestor-y/stats', [$ctrl, 'getStats'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['gestory.read'])]);
        
        // API - Logs
        $router->get('/api/gestor-y/logs', [$ctrl, 'listLogs'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['gestory.read'])]);
    }

    public function name(): string { return 'GestorY'; }
    public function version(): string { return '1.0.0'; }
    public function dependencies(): array { return ['Produtos']; }
}

