<?php

use Illuminate\Support\Facades\Route;
use Modules\SaasSwitcher\Http\Controllers\SaasController;
use Modules\SaasSwitcher\Http\Controllers\EntitlementsController;

/*
|--------------------------------------------------------------------------
| SaaS multi-project switcher (super-admin)
|--------------------------------------------------------------------------
| /saas              → choose a project (super-admin only, host DB)
| /saas/select/{key} → record the chosen project
| /saas/users        → dev/test user picker for that project
| /saas/login-as/{id}→ impersonate a project user (switches DB for the session)
| /saas/exit         → drop impersonation, back to the chooser
*/
Route::middleware(['web', 'auth'])->prefix('saas')->group(function () {
    Route::get('/', [SaasController::class, 'chooser'])->name('saas.chooser');
    Route::get('/select/{key}', [SaasController::class, 'select'])->name('saas.select');
    Route::get('/users', [SaasController::class, 'users'])->name('saas.users');
    Route::post('/login-as/{id}', [SaasController::class, 'loginAs'])
        ->where('id', '[0-9]+')->name('saas.login-as');
    Route::match(['get', 'post'], '/exit', [SaasController::class, 'exitProject'])->name('saas.exit');

    // Per-business module entitlements manager (super-admin) — module-owned UI.
    Route::get('/entitlements', [EntitlementsController::class, 'index'])->name('saas.entitlements');
    Route::post('/entitlements/{key}', [EntitlementsController::class, 'save'])->name('saas.entitlements.save');
});


