<?php
namespace Modules\GestorContratos;

use Gestoreasy\Core\Module\ModuleInterface;
use Gestoreasy\Core\Routing\Router;
use Gestoreasy\Core\Auth\AuthMiddleware;
use Gestoreasy\Core\Auth\Rbac;
use Modules\GestorContratos\Controllers\ContratosController;
use Modules\GestorContratos\Controllers\ModelosController;
use Modules\GestorContratos\Controllers\AssinaturaController;
use Modules\GestorContratos\Controllers\ContratosViewsController;

class Module implements ModuleInterface {
    public function register(Router $router): void {
        $contratosCtrl = new ContratosController();
        $modelosCtrl = new ModelosController();
        $assinaturaCtrl = new AssinaturaController();
        $viewsCtrl = new ContratosViewsController();

        // Views administrativas (HTML)
        $router->get('/contratos', [$viewsCtrl, 'viewIndex'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['contratos.read'])]);
        $router->get('/contratos/novo', [$viewsCtrl, 'viewCreate'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['contratos.create'])]);
        $router->get('/contratos/{id}', [$viewsCtrl, 'viewDetail'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['contratos.read'])]);
        $router->get('/contratos/modelos', [$viewsCtrl, 'viewModelos'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['contratos.modelos.read'])]);
        $router->get('/contratos/config', [$viewsCtrl, 'viewConfig'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['contratos.config'])]);

        // View pública de assinatura (sem autenticação)
        $router->get('/contratos/assinar/{id}', [$viewsCtrl, 'viewAssinar'], []);

        // API - Contratos
        $router->get('/api/contratos', [$contratosCtrl, 'list'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['contratos.read'])]);
        $router->get('/api/contratos/{id}', [$contratosCtrl, 'get'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['contratos.read'])]);
        $router->post('/api/contratos', [$contratosCtrl, 'create'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['contratos.create'])]);
        $router->put('/api/contratos/{id}', [$contratosCtrl, 'update'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['contratos.update'])]);
        $router->post('/api/contratos/{id}/enviar', [$contratosCtrl, 'enviarParaAssinatura'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['contratos.update'])]);
        $router->post('/api/contratos/{id}/arquivar', [$contratosCtrl, 'arquivar'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['contratos.update'])]);
        $router->post('/api/contratos/{id}/cancelar', [$contratosCtrl, 'cancelar'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['contratos.update'])]);
        $router->get('/api/contratos/{id}/historico', [$contratosCtrl, 'historico'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['contratos.auditoria'])]);
        $router->get('/api/contratos/dashboard', [$contratosCtrl, 'dashboard'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['contratos.read'])]);

        // API - Dados para preenchimento
        $router->get('/api/contratos/dados', [$contratosCtrl, 'buscarDados'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['contratos.read'])]);

        // API - Modelos
        $router->get('/api/contratos/modelos', [$modelosCtrl, 'list'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['contratos.modelos.read'])]);
        $router->get('/api/contratos/modelos/{id}', [$modelosCtrl, 'get'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['contratos.modelos.read'])]);
        $router->post('/api/contratos/modelos', [$modelosCtrl, 'create'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['contratos.modelos.create'])]);
        $router->put('/api/contratos/modelos/{id}', [$modelosCtrl, 'update'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['contratos.modelos.update'])]);
        $router->delete('/api/contratos/modelos/{id}', [$modelosCtrl, 'delete'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['contratos.modelos.delete'])]);
        $router->post('/api/contratos/modelos/{id}/duplicar', [$modelosCtrl, 'duplicar'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['contratos.modelos.create'])]);
        $router->post('/api/contratos/modelos/{id}/preencher', [$modelosCtrl, 'preencher'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['contratos.modelos.read'])]);

        // API - Assinatura (pública para assinatura, protegida para geração de links)
        $router->get('/api/contratos/assinatura/{id}', [$assinaturaCtrl, 'visualizar'], []); // Pública
        $router->post('/api/contratos/assinatura/{id}', [$assinaturaCtrl, 'assinar'], []); // Pública
        $router->get('/api/contratos/assinatura/{id}/link', [$assinaturaCtrl, 'gerarLink'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['contratos.read'])]);

        // API - Integrações
        $router->post('/api/contratos/gerar/venda/{venda_id}', [$contratosCtrl, 'gerarDeVenda'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['contratos.create', 'vendas.read'])]);
        $router->post('/api/contratos/gerar/financeiro/{financeiro_id}', [$contratosCtrl, 'gerarDeFinanceiro'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['contratos.create', 'financeiro.read'])]);
        $router->post('/api/contratos/{id}/vincular/financeiro', [$contratosCtrl, 'vincularFinanceiro'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['contratos.update'])]);

        // API - PDF
        $router->get('/api/contratos/{id}/pdf', [$contratosCtrl, 'gerarPdf'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['contratos.read'])]);
        $router->get('/api/contratos/{id}/download-pdf', [$contratosCtrl, 'downloadPdf'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['contratos.read'])]);

        // API - Assinatura Digital Avançada (própria)
        $router->post('/api/contratos/{id}/validar-assinatura', [$contratosCtrl, 'validarAssinatura'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['contratos.read'])]);
        $router->get('/api/contratos/{id}/certificado-digital', [$contratosCtrl, 'obterCertificadoDigital'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['contratos.read'])]);

        // API - Renovação
        $router->post('/api/contratos/{id}/renovar', [$contratosCtrl, 'renovar'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['contratos.create'])]);

        // API - Configurações
        $router->get('/api/contratos/config', [$contratosCtrl, 'getConfig'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['contratos.config'])]);
        $router->put('/api/contratos/config', [$contratosCtrl, 'updateConfig'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['contratos.config'])]);
    }

    public function name(): string {
        return 'GestorContratos';
    }

    public function version(): string {
        return '1.0.0';
    }

    public function dependencies(): array {
        return ['Clientes', 'Vendas', 'Financeiro'];
    }
}

