<?php

namespace Modules\Workflows;

use Gestoreasy\Core\Module\ModuleInterface;
use Gestoreasy\Core\Routing\Router;
use Gestoreasy\Core\Http\AuthMiddleware;
use Gestoreasy\Core\Auth\Rbac;
use Modules\Workflows\Controllers\WorkflowsController;
use Modules\Workflows\Controllers\WorkflowsViewsController;

class Module implements ModuleInterface {
    public function register(Router $router): void {
        $workflowsCtrl = new WorkflowsController();
        $workflowsViewsCtrl = new WorkflowsViewsController();

        // Views
        $router->get('/workflows', [$workflowsViewsCtrl, 'index'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['workflows.read'])]);
        $router->get('/workflows/create', [$workflowsViewsCtrl, 'create'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['workflows.create'])]);
        $router->get('/workflows/{id}', [$workflowsViewsCtrl, 'show'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['workflows.read'])]);
        $router->get('/workflows/{id}/edit', [$workflowsViewsCtrl, 'edit'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['workflows.update'])]);

        // API - CRUD
        $router->get('/api/workflows', [$workflowsCtrl, 'list'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['workflows.read'])]);
        $router->get('/api/workflows/{id}', [$workflowsCtrl, 'get'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['workflows.read'])]);
        $router->post('/api/workflows', [$workflowsCtrl, 'create'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['workflows.create'])]);
        $router->put('/api/workflows/{id}', [$workflowsCtrl, 'update'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['workflows.update'])]);
        $router->delete('/api/workflows/{id}', [$workflowsCtrl, 'delete'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['workflows.delete'])]);

        // API - Execução
        $router->post('/api/workflows/{id}/execute', [$workflowsCtrl, 'execute'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['workflows.execute'])]);
        $router->post('/api/workflows/{id}/activate', [$workflowsCtrl, 'activate'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['workflows.update'])]);
        $router->post('/api/workflows/{id}/deactivate', [$workflowsCtrl, 'deactivate'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['workflows.update'])]);

        // API - Logs e Histórico
        $router->get('/api/workflows/{id}/logs', [$workflowsCtrl, 'getLogs'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['workflows.read'])]);
        $router->get('/api/workflows/{id}/stats', [$workflowsCtrl, 'getStats'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['workflows.read'])]);

        // API - Aprovações
        $router->get('/api/workflows/approvals', [$workflowsCtrl, 'listApprovals'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['workflows.read'])]);
        $router->get('/api/workflows/approvals/{id}', [$workflowsCtrl, 'getApproval'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['workflows.read'])]);
        $router->post('/api/workflows/approvals/{id}/approve', [$workflowsCtrl, 'approveApproval'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['workflows.execute'])]);
        $router->post('/api/workflows/approvals/{id}/reject', [$workflowsCtrl, 'rejectApproval'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['workflows.execute'])]);

        // API - Notificações
        $router->get('/api/notifications', [$workflowsCtrl, 'listNotifications'], [AuthMiddleware::requireAuth()]);
        $router->get('/api/notifications/unread-count', [$workflowsCtrl, 'getUnreadCount'], [AuthMiddleware::requireAuth()]);
        $router->post('/api/notifications/{id}/read', [$workflowsCtrl, 'markNotificationAsRead'], [AuthMiddleware::requireAuth()]);
        $router->post('/api/notifications/read-all', [$workflowsCtrl, 'markAllNotificationsAsRead'], [AuthMiddleware::requireAuth()]);
    }
}

