<?php
namespace Modules\Compras;

use Gestoreasy\Core\Module\ModuleInterface;
use Gestoreasy\Core\Routing\Router;
use Gestoreasy\Core\Http\Request;
use Gestoreasy\Core\Http\Response;
use Gestoreasy\Core\Tenant\ConnectionManager;
use Gestoreasy\Core\Auth\AuthMiddleware;
use Gestoreasy\Core\Auth\Rbac;
use Gestoreasy\Core\Audit\AuditLogger;
use Gestoreasy\Core\Validation\Br;
use Modules\Compras\Controllers\SuppliersController;
use Modules\Compras\Controllers\PurchaseInvoicesController;
use Modules\Compras\Controllers\PurchaseOrdersController;
use Modules\Compras\Controllers\PurchasesViewsController;
use Modules\Compras\Controllers\PurchaseReportsController;

class Module implements ModuleInterface {
    public function register(Router $router): void {
        // MVC controllers
        $suppliers = new SuppliersController();
        $invoices = new PurchaseInvoicesController();
        $orders = new PurchaseOrdersController();
        $views = new PurchasesViewsController();
        $reports = new PurchaseReportsController();

        // Views (HTML)
        $router->get('/compras/ordens', [$views, 'viewOrders'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['compras.read'])]);
        $router->get('/compras/cotacoes', [$views, 'viewQuotes'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['cotacoes.read'])]);

        // Fornecedores
        $router->get('/api/compras/fornecedores', [$suppliers, 'list'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['compras.read'])]);
        $router->get('/api/compras/fornecedores/{id}', [$suppliers, 'getById'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['compras.read'])]);
        $router->post('/api/compras/fornecedores', [$suppliers, 'create'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['compras.create'])]);
        $router->put('/api/compras/fornecedores/{id}', [$suppliers, 'update'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['compras.update'])]);
        $router->delete('/api/compras/fornecedores/{id}', [$suppliers, 'delete'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['compras.delete'])]);

        // Notas de compra
        $router->get('/api/compras/notas', [$invoices, 'list'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['compras.read'])]);
        $router->get('/api/compras/notas/{id}', [$invoices, 'getById'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['compras.read'])]);
        $router->post('/api/compras/notas', [$invoices, 'create'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['compras.create'])]);
        $router->put('/api/compras/notas/{id}', [$invoices, 'update'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['compras.update'])]);
        $router->delete('/api/compras/notas/{id}', [$invoices, 'delete'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['compras.delete'])]);

        // Ordens de compra
        $router->get('/api/compras/ordens', [$orders, 'list'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['compras.read'])]);
        $router->get('/api/compras/ordens/{id}', [$orders, 'getById'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['compras.read'])]);
        $router->post('/api/compras/ordens', [$orders, 'create'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['compras.create'])]);
        $router->put('/api/compras/ordens/{id}', [$orders, 'update'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['compras.update'])]);
        $router->delete('/api/compras/ordens/{id}', [$orders, 'delete'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['compras.delete'])]);
        // Fluxo de aprovação
        $router->post('/api/compras/ordens/{id}/enviar', [$orders, 'submit'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['compras.update'])]);
        $router->post('/api/compras/ordens/{id}/aprovar', [$orders, 'approve'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['compras.update'])]);
        $router->post('/api/compras/ordens/{id}/rejeitar', [$orders, 'reject'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['compras.update'])]);
        $router->post('/api/compras/ordens/{id}/cancelar', [$orders, 'cancel'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['compras.update'])]);
        $router->get('/api/compras/ordens/{id}/relatorio', [$orders, 'report'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['compras.read'])]);
        // Integração com cotações para comparação de preços
        $router->get('/api/compras/ordens/compare', [$orders, 'compare'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['compras.read'])]);

        // Relatórios de Compras (APIs)
        $router->get('/api/compras/relatorios/por-fornecedor', [$reports, 'porFornecedor'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['compras.read'])]);
        $router->get('/api/compras/relatorios/por-fornecedor/export.csv', [$reports, 'exportPorFornecedorCsv'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['compras.read'])]);
        $router->get('/api/compras/relatorios/ordens-por-status', [$reports, 'ordensPorStatus'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['compras.read'])]);
        $router->get('/api/compras/relatorios/itens-top', [$reports, 'itensTop'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['compras.read'])]);
        $router->get('/api/compras/relatorios/abc', [$reports, 'curvaAbc'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['compras.read'])]);
    }
    public function name(): string { return 'Compras'; }
    public function version(): string { return '0.1.0'; }
    public function dependencies(): array { return []; }
}