<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Maestro\Core\Http\Controllers\ActivityLogController;
use Maestro\Core\Http\Controllers\Auth\AuthenticatedSessionController;
use Maestro\Core\Http\Controllers\NavigationController;
use Maestro\Core\Http\Controllers\OrganizationController;
use Maestro\Core\Http\Controllers\PermissionListController;
use Maestro\Core\Http\Controllers\RoleSettingsController;
use Maestro\Core\Http\Controllers\Superadmin\MaestroModuleController;
use Maestro\Core\Http\Controllers\Superadmin\SuperadminNavigationController;
use Maestro\Core\Http\Controllers\UserManagementController;
use Maestro\Core\Http\Middleware\EnsurePlatformSuperadmin;
use Maestro\Core\Support\AuthenticatedUserApiPayload;

Route::middleware('web')->group(function () {
    Route::post('/login', [AuthenticatedSessionController::class, 'store'])
        ->middleware('guest')
        ->name('login.store');

    Route::post('/logout', [AuthenticatedSessionController::class, 'destroy'])
        ->middleware('auth')
        ->name('logout');

    Route::middleware('auth')->prefix('api')->group(function () {
        Route::get('/user', function (Request $request) {
            return response()->json(
                AuthenticatedUserApiPayload::toArray($request->user())
            );
        })->name('api.user');

        Route::get('/organization', [OrganizationController::class, 'show']);
        Route::patch('/organization', [OrganizationController::class, 'update']);

        Route::get('/permissions', [PermissionListController::class, 'index']);

        Route::get('/navigation', [NavigationController::class, 'index']);

        Route::middleware([EnsurePlatformSuperadmin::class])->prefix('superadmin')->group(function () {
            Route::get('/modules', [MaestroModuleController::class, 'index']);
            Route::patch('/modules/{slug}', [MaestroModuleController::class, 'update']);
            Route::put('/modules/reorder', [MaestroModuleController::class, 'reorder']);
            Route::get('/navigation-tree', [SuperadminNavigationController::class, 'show']);
            Route::put('/navigation-tree', [SuperadminNavigationController::class, 'update']);
        });

        Route::get('/users', [UserManagementController::class, 'index']);
        Route::post('/users', [UserManagementController::class, 'store']);
        Route::patch('/users/{user}', [UserManagementController::class, 'update']);
        Route::delete('/users/{user}', [UserManagementController::class, 'destroy']);

        Route::get('/roles', [RoleSettingsController::class, 'index']);
        Route::post('/roles', [RoleSettingsController::class, 'store']);
        Route::put('/roles/{role}', [RoleSettingsController::class, 'update']);
        Route::delete('/roles/{role}', [RoleSettingsController::class, 'destroy']);

        Route::get('/settings/activity-log', [ActivityLogController::class, 'index']);
    });
});

/*
|--------------------------------------------------------------------------
| SPA fallback
|--------------------------------------------------------------------------
|
| Host applications must register a single catch-all route (see stubs/routes/web.php)
| that returns the SPA Blade view. Duplicate global fallbacks cause confusing route
| precedence; this package does not register one.
|
*/
