<?php

use Maestro\Core\Http\Controllers\ActivityLogController;
use Maestro\Core\Http\Controllers\Auth\AuthenticatedSessionController;
use Maestro\Core\Http\Controllers\OrganizationController;
use Maestro\Core\Http\Controllers\PermissionListController;
use Maestro\Core\Http\Controllers\RoleSettingsController;
use Maestro\Core\Http\Controllers\UserManagementController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

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) {
            /** @var \Maestro\Core\Models\User $user */
            $user = $request->user();

            return response()->json([
                'id' => $user->id,
                'name' => $user->name,
                'email' => $user->email,
                'organization_id' => $user->organization_id,
                'permissions' => $user->getAllPermissions()->pluck('name')->values(),
                'roles' => $user->getRoleNames()->values(),
            ]);
        })->name('api.user');

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

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

        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']);
    });
});

Route::view('/{any?}', 'maestro-core::spa')->where('any', '.*');
