<?php

use Illuminate\Support\Facades\Route;
use Modules\Auth\Http\Controllers\AuthController;
use Modules\Auth\Http\Controllers\PasswordController;
use Modules\Auth\Http\Controllers\ProfileController;
use Modules\Auth\Http\Controllers\RoleController;
use Modules\Auth\Http\Controllers\UserController;

Route::prefix('auth')->group(function () {
    Route::post('login',   [AuthController::class, 'login']);
    Route::post('refresh', [AuthController::class, 'refresh']);

    Route::prefix('password')->group(function () {
        Route::post('/send-link', [PasswordController::class, 'sendLink']);
        Route::post('/password-reset', [PasswordController::class, 'setPassword']);
    });

    Route::middleware(['api.auth'])->group(function () {
        Route::post('/logout', [AuthController::class, 'logout']);
        Route::get('/profile', [ProfileController::class, 'show']);
        Route::get('/me', [ProfileController::class, 'myProfile']);
        Route::put('/profile/update', [ProfileController::class, 'updateProfile']);
        Route::post('/change-password', [AuthController::class, 'changePassword']);

        Route::prefix('roles')->group(function () {
            Route::get('list', [RoleController::class, 'index']);
            Route::get('{role}/permissions', [RoleController::class, 'rolePermissions']);
            Route::put('{role}/permissions/sync', [RoleController::class, 'update']);
        });

        Route::apiResource('users', UserController::class)->except('destroy');
        Route::post('users/update-roles/{id}', [UserController::class, 'updateRoles']);
        Route::patch('/users/{user}/status', [UserController::class, 'toggleStatus'])->name('users.toggle-status');
        Route::put('/users/{user}/reset-password', [UserController::class, 'resetPassword'])->name('users.reset-password');

        Route::prefix('lookups')->group(function () {
            Route::get('roles', [RoleController::class, 'rolesList']);
        });
    });
});
