<?php

namespace Modules\Integracoes\TikTokSeller;

use Gestoreasy\Core\Module\ModuleInterface;
use Gestoreasy\Core\Routing\Router;
use Gestoreasy\Core\Http\AuthMiddleware;
use Gestoreasy\Core\Auth\Rbac;
use Modules\Integracoes\TikTokSeller\Controllers\TikTokSellerController;
use Modules\Integracoes\TikTokSeller\Controllers\TikTokSellerViewsController;

class Module implements ModuleInterface {
    public function register(Router $router): void {
        $apiCtrl = new TikTokSellerController();
        $viewsCtrl = new TikTokSellerViewsController();

        // Views
        $router->get('/integracoes/tiktok-seller', [$viewsCtrl, 'dashboard'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['integracoes.tiktok.read'])]);
        $router->get('/integracoes/tiktok-seller/config', [$viewsCtrl, 'config'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['integracoes.tiktok.read'])]);

        // OAuth Callback
        $router->get('/integracoes/tiktok-seller/callback', [$apiCtrl, 'oauthCallback'], [AuthMiddleware::requireAuth()]);

        // API - Configuração
        $router->get('/api/tiktok/config', [$apiCtrl, 'getConfig'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['integracoes.tiktok.read'])]);
        $router->post('/api/tiktok/config', [$apiCtrl, 'updateConfig'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['integracoes.tiktok.update'])]);

        // API - Produtos
        $router->get('/api/tiktok/produtos', [$apiCtrl, 'listProdutos'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['integracoes.tiktok.read'])]);
        $router->post('/api/tiktok/produtos/sincronizar', [$apiCtrl, 'sincronizarProdutos'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['integracoes.tiktok.update'])]);
        $router->post('/api/tiktok/produtos/{id}/publicar', [$apiCtrl, 'publicarProduto'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['integracoes.tiktok.update'])]);

        // API - Pedidos
        $router->get('/api/tiktok/pedidos', [$apiCtrl, 'listPedidos'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['integracoes.tiktok.read'])]);
        $router->post('/api/tiktok/pedidos/sincronizar', [$apiCtrl, 'sincronizarPedidos'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['integracoes.tiktok.update'])]);
        $router->post('/api/tiktok/pedidos/{id}/importar', [$apiCtrl, 'importarPedido'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['integracoes.tiktok.update'])]);

        // API - Estoque
        $router->post('/api/tiktok/estoque/sincronizar', [$apiCtrl, 'sincronizarEstoque'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['integracoes.tiktok.update'])]);

        // Webhooks (público, validação interna)
        $router->post('/api/tiktok/webhook', [$apiCtrl, 'webhook'], []);
    }

    public function name(): string { return 'TikTok Seller'; }
    public function version(): string { return '1.0.0'; }
    public function dependencies(): array { return []; }
}

