<?php

namespace Modules\CRMAvancado;

use Gestoreasy\Core\Module\ModuleInterface;
use Gestoreasy\Core\Routing\Router;
use Gestoreasy\Core\Http\AuthMiddleware;
use Gestoreasy\Core\Auth\Rbac;
use Modules\CRMAvancado\Controllers\LeadScoringController;
use Modules\CRMAvancado\Controllers\FunnelController;
use Modules\CRMAvancado\Controllers\EmailMarketingController;
use Modules\CRMAvancado\Controllers\AutomationController;
use Modules\CRMAvancado\Controllers\CRMAvancadoViewsController;

class Module implements ModuleInterface {
    public function register(Router $router): void {
        $scoringCtrl = new LeadScoringController();
        $funnelCtrl = new FunnelController();
        $emailCtrl = new EmailMarketingController();
        $automationCtrl = new AutomationController();
        $viewsCtrl = new CRMAvancadoViewsController();

        // Views
        $router->get('/crm-avancado', [$viewsCtrl, 'dashboard'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['crm.read'])]);
        $router->get('/crm-avancado/scoring', [$viewsCtrl, 'scoring'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['crm.read'])]);
        $router->get('/crm-avancado/funil', [$viewsCtrl, 'funil'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['crm.read'])]);
        $router->get('/crm-avancado/email-marketing', [$viewsCtrl, 'emailMarketing'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['crm.read'])]);

        // API - Lead Scoring
        $router->get('/api/crm/scoring/leads', [$scoringCtrl, 'listLeads'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['crm.read'])]);
        $router->get('/api/crm/scoring/lead/{id}', [$scoringCtrl, 'getLeadScore'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['crm.read'])]);
        $router->post('/api/crm/scoring/calcular', [$scoringCtrl, 'calcularScore'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['crm.update'])]);
        $router->post('/api/crm/scoring/config', [$scoringCtrl, 'updateConfig'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['crm.admin'])]);

        // API - Funil de Conversão
        $router->get('/api/crm/funil', [$funnelCtrl, 'getFunil'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['crm.read'])]);
        $router->get('/api/crm/funil/metricas', [$funnelCtrl, 'getMetricas'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['crm.read'])]);
        $router->get('/api/crm/funil/previsao', [$funnelCtrl, 'getPrevisao'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['crm.read'])]);

        // API - E-mail Marketing
        $router->get('/api/crm/campanhas', [$emailCtrl, 'listCampanhas'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['crm.read'])]);
        $router->post('/api/crm/campanhas', [$emailCtrl, 'createCampanha'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['crm.create'])]);
        $router->post('/api/crm/campanhas/{id}/enviar', [$emailCtrl, 'enviarCampanha'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['crm.create'])]);
        $router->get('/api/crm/campanhas/{id}/analytics', [$emailCtrl, 'getAnalytics'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['crm.read'])]);

        // Tracking (público, sem autenticação)
        $router->get('/api/crm/track/open/{token}', [$emailCtrl, 'trackOpen'], []);
        $router->get('/api/crm/track/click/{token}', [$emailCtrl, 'trackClick'], []);

        // API - Automações
        $router->get('/api/crm/automacoes', [$automationCtrl, 'list'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['crm.read'])]);
        $router->post('/api/crm/automacoes', [$automationCtrl, 'create'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['crm.create'])]);
        $router->put('/api/crm/automacoes/{id}', [$automationCtrl, 'update'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['crm.update'])]);
        $router->post('/api/crm/automacoes/{id}/ativar', [$automationCtrl, 'ativar'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['crm.update'])]);
    }
}

