<?php

use Illuminate\Support\Facades\Route;
use Modules\Auth\Http\Controllers\EmailVerificationController;
use Modules\Auth\Http\Controllers\PasswordResetController;
use Modules\Auth\Http\Controllers\PhoneVerificationController;
use Modules\Auth\Http\Controllers\SessionController;
use Laravel\Fortify\Http\Controllers\AuthenticatedSessionController;
use Laravel\Fortify\Http\Controllers\RegisteredUserController;

// Auth
Route::post('/phone-verification/send', [PhoneVerificationController::class, 'send'])
    ->middleware('throttle:phone-verification-send');
Route::post('/phone-verification/verify', [PhoneVerificationController::class, 'verify'])
    ->middleware('throttle:phone-verification-verify');

Route::post('/register', [RegisteredUserController::class, 'store']);
Route::post('/login', [AuthenticatedSessionController::class, 'store'])->middleware('block.api.tools');

Route::get('/email/verify/{id}/{hash}', [EmailVerificationController::class, 'verify'])
    ->middleware('throttle:email-verification')
    ->name('verification.verify');

Route::post('/forgot-password', [PasswordResetController::class, 'requestOtp'])
    ->middleware('throttle:password-reset-request');
Route::post('/reset-password', [PasswordResetController::class, 'reset'])
    ->middleware('throttle:password-reset');

Route::middleware(['auth:sanctum'])->prefix('v1')->group(function () {
    // get current user
    Route::get('/me', [SessionController::class, 'currentUser']);

    // switch active role
    Route::post('/active-role/switch', [SessionController::class, 'switchRole']);

    // logout
    Route::post('/logout', [AuthenticatedSessionController::class, 'destroy']);

    // optional email verification (post-login)
    Route::post('/email/verification-notification', [EmailVerificationController::class, 'send'])
        ->middleware('throttle:email-verification-resend');

    // optional phone verification (post-login)
    Route::post('/phone-verification/send', [PhoneVerificationController::class, 'sendForAuthenticatedUser'])
        ->middleware('throttle:phone-verification-send');
    Route::post('/phone-verification/verify', [PhoneVerificationController::class, 'verifyForAuthenticatedUser'])
        ->middleware('throttle:phone-verification-verify');
});