<?php
namespace Modules\Estoque;

use Gestoreasy\Core\Module\ModuleInterface;
use Gestoreasy\Core\Routing\Router;
use Gestoreasy\Core\Auth\AuthMiddleware;
use Gestoreasy\Core\Auth\Rbac;
use Modules\Estoque\Controllers\InventoryController;
use Modules\Estoque\Controllers\MovementsController;
use Modules\Estoque\Controllers\InventoryViewsController;
use Modules\Estoque\Controllers\InventorySettingsController;
use Gestoreasy\Core\Http\StockPolicyMiddleware;

class Module implements ModuleInterface {
    public function register(Router $router): void {
        $inv = new InventoryController();
        $mov = new MovementsController();
        $views = new InventoryViewsController();
        $settings = new InventorySettingsController();

        // Views (HTML)
        $router->get('/estoque/thresholds', [$views, 'viewThresholds'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['estoque.read'])]);
        $router->get('/estoque/movimentos', [$views, 'viewMovements'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['estoque.read'])]);
        $router->get('/estoque/configuracoes', [$settings, 'viewSettings'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['estoque.read'])]);

        // Estoque: thresholds e consulta
        $router->get('/api/estoque/thresholds', [$inv, 'listThresholds'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['estoque.read'])]);
        // Colocar rota específica antes da dinâmica para evitar colisão com {product_id}
        $router->get('/api/estoque/thresholds/report', [$inv, 'reportThresholds'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['estoque.read'])]);
        $router->get('/api/estoque/thresholds/{product_id}', [$inv, 'getThreshold'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['estoque.read'])]);
        $router->post('/api/estoque/thresholds', [$inv, 'setThreshold'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['estoque.update'])]);
        $router->delete('/api/estoque/thresholds/{product_id}', [$inv, 'deleteThreshold'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['estoque.update'])]);
        $router->get('/api/estoque/niveis', [$inv, 'levels'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['estoque.read'])]);
        $router->get('/api/estoque/niveis/export.csv', [$inv, 'exportCsvLevels'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['estoque.read'])]);
        $router->get('/api/estoque/alertas', [$inv, 'alerts'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['estoque.read'])]);
        // Relatórios e envio de alertas
        $router->post('/api/estoque/alertas/dispatch', [$inv, 'dispatchAlerts'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['estoque.update'])]);

        // Configurações de estoque (API)
        $router->get('/api/estoque/configuracoes', [$settings, 'getSettings'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['estoque.read'])]);
        $router->post('/api/estoque/configuracoes', [$settings, 'updateSettings'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['estoque.update'])]);

        // Movimentos de estoque: entrada/saída com rastreabilidade
        $router->get('/api/estoque/movimentos', [$mov, 'list'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['estoque.read'])]);
        $router->get('/api/estoque/movimentos/export.csv', [$mov, 'exportCsv'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['estoque.read'])]);
        $router->post('/api/estoque/movimentos/entrada', [$mov, 'entrada'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['estoque.update'])]);
        $router->post('/api/estoque/movimentos/saida', [$mov, 'saida'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['estoque.update']), StockPolicyMiddleware::inject()]);
    }
    public function name(): string { return 'Estoque'; }
    public function version(): string { return '0.1.0'; }
    public function dependencies(): array { return ['Produtos']; }
}