<?php

namespace Modules\Integracoes\Facebook;

use Gestoreasy\Core\Module\ModuleInterface;
use Gestoreasy\Core\Routing\Router;
use Gestoreasy\Core\Http\AuthMiddleware;
use Gestoreasy\Core\Auth\Rbac;
use Modules\Integracoes\Facebook\Controllers\FacebookController;
use Modules\Integracoes\Facebook\Controllers\FacebookViewsController;

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

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

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

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

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

        // API - Publicações
        $router->get('/api/facebook/publicacoes', [$apiCtrl, 'listPublicacoes'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['integracoes.facebook.read'])]);
        $router->post('/api/facebook/publicacoes', [$apiCtrl, 'criarPublicacao'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['integracoes.facebook.update'])]);

        // API - Anúncios
        $router->get('/api/facebook/anuncios', [$apiCtrl, 'listAnuncios'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['integracoes.facebook.read'])]);
        $router->post('/api/facebook/anuncios', [$apiCtrl, 'criarAnuncio'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['integracoes.facebook.update'])]);

        // API - Estatísticas
        $router->get('/api/facebook/estatisticas', [$apiCtrl, 'getEstatisticas'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['integracoes.facebook.read'])]);

        // Webhooks
        $router->post('/api/facebook/webhook', [$apiCtrl, 'webhook'], []);
    }

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

