<?php
namespace Modules\Financeiro;

use Gestoreasy\Core\Module\ModuleInterface;
use Gestoreasy\Core\Routing\Router;
use Gestoreasy\Core\Http\Request;
use Gestoreasy\Core\Http\Response;
use Gestoreasy\Core\Auth\AuthMiddleware;
use Gestoreasy\Core\Auth\Rbac;
use Modules\Financeiro\Controllers\FinanceiroViewsController;
use Modules\Financeiro\Controllers\ContasPagarController;
use Modules\Financeiro\Controllers\ContasReceberController;
use Modules\Financeiro\Controllers\BancosController;
use Modules\Financeiro\Controllers\RelatoriosController;

class Module implements ModuleInterface {
    public function register(Router $router): void {
        $views = new FinanceiroViewsController();
        $contasPagar = new ContasPagarController();
        $contasReceber = new ContasReceberController();
        $bancos = new BancosController();
        $relatorios = new RelatoriosController();

        // Views (HTML)
        $router->get('/financeiro/pagar', [$views, 'viewContasPagar'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['financeiro.read'])]);
        $router->get('/financeiro/receber', [$views, 'viewContasReceber'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['financeiro.read'])]);
        $router->get('/financeiro/fluxo', [$views, 'viewFluxoCaixa'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['financeiro.read'])]);
        $router->get('/financeiro/bancos', [$views, 'viewBancos'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['financeiro.read'])]);
        $router->get('/financeiro/relatorios', [$views, 'viewRelatorios'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['financeiro.read'])]);

        // API: Relatórios Financeiros
        $router->get('/api/financeiro/relatorios/dre', [$relatorios, 'dre'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.read'])]);
        $router->get('/api/financeiro/relatorios/dre/export.csv', [$relatorios, 'exportDreCsv'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.read'])]);
        // DRE Completo
        $router->get('/api/financeiro/relatorios/dre-completo', [$relatorios, 'dreCompleto'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.read'])]);
        $router->get('/api/financeiro/relatorios/dre-completo/export.csv', [$relatorios, 'exportDreCompletoCsv'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.read'])]);
        $router->get('/api/financeiro/relatorios/fluxo', [$relatorios, 'fluxo'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.read'])]);
        $router->get('/api/financeiro/relatorios/fluxo/export.csv', [$relatorios, 'exportFluxoCsv'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.read'])]);
        // Resumos: Contas a Receber/Pagar
        $router->get('/api/financeiro/relatorios/receber/resumo', [$relatorios, 'resumoReceber'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.read'])]);
        $router->get('/api/financeiro/relatorios/receber/resumo/export.csv', [$relatorios, 'exportResumoReceberCsv'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.read'])]);
        $router->get('/api/financeiro/relatorios/pagar/resumo', [$relatorios, 'resumoPagar'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.read'])]);
        $router->get('/api/financeiro/relatorios/pagar/resumo/export.csv', [$relatorios, 'exportResumoPagarCsv'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.read'])]);

        // API: Bancos (Contas e Movimentos)
        $router->get('/api/financeiro/bancos/contas', [$bancos, 'listAccounts'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.read'])]);
        $router->post('/api/financeiro/bancos/contas', [$bancos, 'createAccount'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.create'])]);
        $router->put('/api/financeiro/bancos/contas/{id}', [$bancos, 'updateAccount'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.update'])]);
        $router->get('/api/financeiro/bancos/movimentos', [$bancos, 'listMovimentos'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.read'])]);
        $router->post('/api/financeiro/bancos/movimentos', [$bancos, 'createMovimento'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.create'])]);
        $router->get('/api/financeiro/bancos/contas/{id}/extrato', [$bancos, 'extrato'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.read'])]);

        // API: Contas a Pagar
        $router->get('/api/financeiro/contas-pagar', [$contasPagar, 'list'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.read'])]);
        // Registrar rotas exatas antes das dinâmicas para evitar colisão com {id}
        $router->get('/api/financeiro/contas-pagar/export.csv', [$contasPagar, 'exportCsv'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.read'])]);
        $router->get('/api/financeiro/contas-pagar/{id}', [$contasPagar, 'getById'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.read'])]);
        $router->post('/api/financeiro/contas-pagar', [$contasPagar, 'create'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.create'])]);
        $router->put('/api/financeiro/contas-pagar/{id}', [$contasPagar, 'update'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.update'])]);
        $router->delete('/api/financeiro/contas-pagar/{id}', [$contasPagar, 'delete'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.delete'])]);
        $router->post('/api/financeiro/contas-pagar/{id}/pagar', [$contasPagar, 'pay'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.update'])]);
        // Extensões: histórico, aprovação, conciliação, notificações
        $router->get('/api/financeiro/contas-pagar/{id}/historico', [$contasPagar, 'history'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.read'])]);
        $router->get('/api/financeiro/contas-pagar/{id}/aprovacoes', [$contasPagar, 'approvalHistory'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.read'])]);
        $router->post('/api/financeiro/contas-pagar/{id}/aprovar', [$contasPagar, 'approve'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.approve'])]);
        $router->post('/api/financeiro/contas-pagar/{id}/conciliar', [$contasPagar, 'reconcile'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.reconcile'])]);
        $router->post('/api/financeiro/contas-pagar/notify-due', [$contasPagar, 'notifyDue'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.notify'])]);

        // Comprovante (HTML view)
        $router->get('/financeiro/contas-pagar/{id}/comprovante', [$views, 'viewComprovante'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['financeiro.read'])]);

        // API: Contas a Receber
        $router->get('/api/financeiro/contas-receber', [$contasReceber, 'list'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.read'])]);
        // Registrar rotas exatas antes das dinâmicas para evitar colisão com {id}
        $router->get('/api/financeiro/contas-receber/export.csv', [$contasReceber, 'exportCsv'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.read'])]);
        $router->get('/api/financeiro/contas-receber/{id}', [$contasReceber, 'getById'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.read'])]);
        $router->post('/api/financeiro/contas-receber', [$contasReceber, 'create'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.create'])]);
        $router->put('/api/financeiro/contas-receber/{id}', [$contasReceber, 'update'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.update'])]);
        $router->delete('/api/financeiro/contas-receber/{id}', [$contasReceber, 'delete'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.delete'])]);
        $router->post('/api/financeiro/contas-receber/{id}/receber', [$contasReceber, 'receive'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.update'])]);
    }

    // Implementações exigidas pela ModuleInterface
    public function name(): string {
        return 'Financeiro';
    }

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

    public function dependencies(): array {
        return ['Auth'];
    }
}