<?php
namespace Modules\Fornecedores;

use Gestoreasy\Core\Module\ModuleInterface;
use Gestoreasy\Core\Routing\Router;
use Gestoreasy\Core\Auth\AuthMiddleware;
use Gestoreasy\Core\Auth\Rbac;
use Modules\Fornecedores\Controllers\SuppliersCrudController;
use Gestoreasy\Core\Http\Request;
use Gestoreasy\Core\Http\Response;

class Module implements ModuleInterface {
    public function register(Router $router): void {
        $ctrl = new SuppliersCrudController();

        $perm = function(string $op){
            return function (Request $r) use ($op) {
                $mw = Rbac::requirePermissions(["cadastros.fornecedores.$op"]);
                return $mw($r);
            };
        };

        // Views
        $router->get('/fornecedores', [$ctrl, 'viewIndex'], [AuthMiddleware::requireAuthHtml(), $perm('read')]);
        $router->get('/fornecedores/create', [$ctrl, 'viewCreate'], [AuthMiddleware::requireAuthHtml(), $perm('create')]);
        $router->get('/fornecedores/{id}', [$ctrl, 'viewShow'], [AuthMiddleware::requireAuthHtml(), $perm('read')]);
        $router->get('/fornecedores/{id}/edit', [$ctrl, 'viewEdit'], [AuthMiddleware::requireAuthHtml(), $perm('update')]);

        // API CRUD
        $router->get('/api/fornecedores', [$ctrl, 'list'], [AuthMiddleware::requireAuth(), $perm('read')]);
        $router->get('/api/fornecedores/{id}', [$ctrl, 'getById'], [AuthMiddleware::requireAuth(), $perm('read')]);
        $router->post('/api/fornecedores', [$ctrl, 'create'], [AuthMiddleware::requireAuth(), $perm('create')]);
        $router->put('/api/fornecedores/{id}', [$ctrl, 'update'], [AuthMiddleware::requireAuth(), $perm('update')]);
        $router->delete('/api/fornecedores/{id}', [$ctrl, 'delete'], [AuthMiddleware::requireAuth(), $perm('delete')]);

        // Helpers
        $router->get('/api/fornecedores/cep/{cep}', [$ctrl, 'cepLookup'], [AuthMiddleware::requireAuth(), $perm('read')]);
        $router->get('/api/fornecedores/cnpj/{cnpj}', [$ctrl, 'cnpjLookup'], [AuthMiddleware::requireAuth(), $perm('read')]);
        $router->get('/api/fornecedores/geocode', [$ctrl, 'geocode'], [AuthMiddleware::requireAuth(), $perm('read')]);
    }
    public function name(): string { return 'Fornecedores'; }
    public function version(): string { return '0.1.0'; }
    public function dependencies(): array { return []; }
}