<?php
/**
 * Integrations Routes
 *
 * REST API routes for the new integration system.
 *
 * @package FleekDash\Routes\Groups
 * @since 2.3.0
 */

namespace FleekDash\Routes\Groups;

use FleekDash\Controllers\Integration\IntegrationsController;
use FleekDash\Libs\API\Route;
use FleekDash\Libs\API\RouteAuthMiddleware;

defined('ABSPATH') || exit;

class IntegrationsRoutes
{
    /**
     * Register routes
     *
     * @return void
     */
    public static function register(): void
    {
        // IMPORTANT: Order matters! More specific routes must come BEFORE generic ones

        // Get all routes (specific - before /integrations/{id})
        Route::get(
            'integrations/routes',
            [IntegrationsController::class, 'routes'],
            RouteAuthMiddleware::authenticated()
        );

        // Validate specific route (specific - before /integrations/{id})
        Route::get(
            'integrations/routes/validate',
            [IntegrationsController::class, 'validateRoute'],
            RouteAuthMiddleware::authenticated()
        );

        // Get MessageBus nonce (specific - before /integrations/{id})
        Route::get(
            'integrations/messagebus/nonce',
            [IntegrationsController::class, 'getMessageBusNonce'],
            RouteAuthMiddleware::authenticated()
        );

        // List all integrations
        Route::get(
            'integrations',
            [IntegrationsController::class, 'index'],
            RouteAuthMiddleware::authenticated()
        );

        // Get single integration (after specific routes)
        Route::get(
            'integrations/{integration_id}',
            [IntegrationsController::class, 'show'],
            RouteAuthMiddleware::authenticated()
        );

        // Update integration role access
        Route::post(
            'integrations/{integration_id}/permissions',
            [IntegrationsController::class, 'updatePermissions'],
            RouteAuthMiddleware::admin()
        );

        // Activate integration
        Route::post(
            'integrations/{integration_id}/activate',
            [IntegrationsController::class, 'activate'],
            RouteAuthMiddleware::admin()
        );

        // Deactivate integration
        Route::post(
            'integrations/{integration_id}/deactivate',
            [IntegrationsController::class, 'deactivate'],
            RouteAuthMiddleware::admin()
        );

        // Get integration settings
        Route::get(
            'integrations/{integration_id}/settings',
            [IntegrationsController::class, 'getSettings'],
            RouteAuthMiddleware::authenticated()
        );

        // Update integration settings
        Route::post(
            'integrations/{integration_id}/settings',
            [IntegrationsController::class, 'updateSettings'],
            RouteAuthMiddleware::admin()
        );
    }
}
