<?php

namespace Modules\LotesSeries;

use Gestoreasy\Core\Module\ModuleInterface;
use Gestoreasy\Core\Routing\Router;
use Gestoreasy\Core\Http\AuthMiddleware;
use Gestoreasy\Core\Auth\Rbac;
use Modules\LotesSeries\Controllers\LotesController;
use Modules\LotesSeries\Controllers\SeriesController;
use Modules\LotesSeries\Controllers\RastreabilidadeController;
use Modules\LotesSeries\Controllers\LotesSeriesViewsController;

class Module implements ModuleInterface {
    public function register(Router $router): void {
        $lotesCtrl = new LotesController();
        $seriesCtrl = new SeriesController();
        $rastreabilidadeCtrl = new RastreabilidadeController();
        $viewsCtrl = new LotesSeriesViewsController();

        // Views
        $router->get('/lotes-series', [$viewsCtrl, 'index'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['lotes.read'])]);
        $router->get('/lotes-series/lotes', [$viewsCtrl, 'lotes'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['lotes.read'])]);
        $router->get('/lotes-series/series', [$viewsCtrl, 'series'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['series.read'])]);
        $router->get('/lotes-series/rastreabilidade', [$viewsCtrl, 'rastreabilidade'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['lotes.read'])]);

        // API - Lotes
        $router->get('/api/lotes', [$lotesCtrl, 'list'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['lotes.read'])]);
        $router->get('/api/lotes/{id}', [$lotesCtrl, 'get'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['lotes.read'])]);
        $router->post('/api/lotes', [$lotesCtrl, 'create'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['lotes.create'])]);
        $router->put('/api/lotes/{id}', [$lotesCtrl, 'update'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['lotes.update'])]);
        $router->delete('/api/lotes/{id}', [$lotesCtrl, 'delete'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['lotes.delete'])]);

        // API - Lotes: Validade e Alertas
        $router->get('/api/lotes/validade/proximos-vencer', [$lotesCtrl, 'getProximosVencer'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['lotes.read'])]);
        $router->get('/api/lotes/validade/vencidos', [$lotesCtrl, 'getVencidos'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['lotes.read'])]);

        // API - Lotes: FIFO/FEFO/LIFO
        $router->get('/api/lotes/{product_id}/sugerir', [$lotesCtrl, 'sugerirLote'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['lotes.read'])]);

        // API - Séries
        $router->get('/api/series', [$seriesCtrl, 'list'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['series.read'])]);
        $router->get('/api/series/{id}', [$seriesCtrl, 'get'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['series.read'])]);
        $router->post('/api/series', [$seriesCtrl, 'create'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['series.create'])]);
        $router->put('/api/series/{id}', [$seriesCtrl, 'update'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['series.update'])]);
        $router->delete('/api/series/{id}', [$seriesCtrl, 'delete'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['series.delete'])]);

        // API - Séries: Validação
        $router->post('/api/series/validar', [$seriesCtrl, 'validarSerie'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['series.read'])]);

        // API - Rastreabilidade
        $router->get('/api/rastreabilidade/lote/{lote_id}', [$rastreabilidadeCtrl, 'rastrearLote'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['lotes.read'])]);
        $router->get('/api/rastreabilidade/serie/{serie_id}', [$rastreabilidadeCtrl, 'rastrearSerie'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['series.read'])]);
        $router->get('/api/rastreabilidade/produto/{product_id}', [$rastreabilidadeCtrl, 'rastrearProduto'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['lotes.read'])]);
        $router->post('/api/rastreabilidade/recall', [$rastreabilidadeCtrl, 'criarRecall'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['lotes.admin'])]);
    }
}

