<?php
namespace Modules\GoogleIntegration;

use Gestoreasy\Core\Module\ModuleInterface;
use Gestoreasy\Core\Routing\Router;
use Gestoreasy\Core\Auth\AuthMiddleware;
use Gestoreasy\Core\Auth\Rbac;
use Gestoreasy\Core\Auth\PlanAccessMiddleware;
use Modules\GoogleIntegration\Controllers\GoogleCalendarController;
use Modules\GoogleIntegration\Controllers\GoogleViewsController;

class Module implements ModuleInterface {
    public function register(Router $router): void {
        $api = new GoogleCalendarController();
        $views = new GoogleViewsController();

        // Painel HTML
        $router->get('/addons/google-calendar', [$views, 'viewIndex'], [AuthMiddleware::requireAuthHtml(), PlanAccessMiddleware::requireModuleHtml('integrations.googlecalendar'), Rbac::requirePermissions(['integrations.googlecalendar.read'])]);

        // Configuração
        $router->get('/api/integrations/google-calendar/config', [$api, 'getConfig'], [AuthMiddleware::requireAuth(), PlanAccessMiddleware::requireModule('integrations.googlecalendar'), Rbac::requirePermissions(['integrations.googlecalendar.read'])]);
        $router->post('/api/integrations/google-calendar/config', [$api, 'saveConfig'], [AuthMiddleware::requireAuth(), PlanAccessMiddleware::requireModule('integrations.googlecalendar'), Rbac::requirePermissions(['integrations.googlecalendar.manage'])]);

        // Eventos pontuais
        $router->post('/api/integrations/google-calendar/events', [$api, 'createEvent'], [AuthMiddleware::requireAuth(), PlanAccessMiddleware::requireModule('integrations.googlecalendar'), Rbac::requirePermissions(['integrations.googlecalendar.manage'])]);

        // Sincronização: contas a pagar (tolerante)
        $router->post('/api/integrations/google-calendar/sync/payables', [$api, 'syncPayables'], [AuthMiddleware::requireAuth(), PlanAccessMiddleware::requireModule('integrations.googlecalendar'), Rbac::requirePermissions(['integrations.googlecalendar.manage'])]);
    }
    public function name(): string { return 'GoogleIntegration'; }
    public function version(): string { return '0.1.0'; }
    public function dependencies(): array { return ['Auth']; }
}