<?php

use App\Http\Controllers\AuthController;
use App\Http\Controllers\HuntsController;
use App\Http\Controllers\HuntStepsController;
use App\Http\Controllers\UsersController;
use App\Http\Middleware\EnsureUserIsAdmin;
use App\Http\Middleware\EnsureUserIsOrga;
use Illuminate\Support\Facades\Route;

Route::prefix('auth')->group(function (): void {
    Route::post('register', [AuthController::class, 'register']);
    Route::post('login', [AuthController::class, 'login']);
    Route::get('email/verify/{id}/{hash}', [AuthController::class, 'verifyEmail'])
        ->middleware(['signed', 'throttle:6,1'])
        ->name('verification.verify');
    // Route::get('{provider}/redirect', [ProviderRedirectController::class, 'redirect']);
    // Route::get('{provider}/callback', [ProviderCallbackController::class, 'callback']);
});

// List hunts

// Ensure is Admin
Route::middleware(['auth:sanctum', 'verified', EnsureUserIsAdmin::class])->group(function () {
    Route::post('users/{user}/delete', [UsersController::class, 'softDeleteById'])->withTrashed();
});

// Ensure is Orga
Route::middleware(['auth:sanctum', 'verified', EnsureUserIsOrga::class])->group(function () {
    Route::post('hunts', [HuntsController::class, 'store']);
    Route::post('hunts/{hunt}/steps', [HuntStepsController::class, 'store']);
    Route::match(['put', 'patch'], 'hunts/{hunt}', [HuntsController::class, 'update']);
    Route::delete('hunts/{hunt}', [HuntsController::class, 'destroy']);
});

// Auth middleware
Route::middleware('auth:sanctum')->group(function (): void {
    Route::post('auth/email/verification-notification', [AuthController::class, 'resendEmailVerificationNotification'])
        ->middleware('throttle:6,1')
        ->name('verification.send');
    Route::get('auth/me', [AuthController::class, 'me']);
    Route::post('auth/logout', [AuthController::class, 'logout']);
});

Route::middleware(['auth:sanctum', 'verified'])->group(function (): void {
    Route::get('hunts', [HuntsController::class, 'index']);
    Route::get('hunts/{hunt}', [HuntsController::class, 'show']);
    // Auth
    Route::post('auth/delete', [AuthController::class, 'softDelete']);
    // User
    Route::get('users/leaderboard', [UsersController::class, 'leaderboard']);
    Route::put('users/update/username', [UsersController::class, 'updateUsername']);
    Route::put('users/update/password', [UsersController::class, 'updatePassword']);
    Route::get('users/{user}', [UsersController::class, 'show']);
    Route::post('users/{user}/avatar', [UsersController::class, 'updateAvatar']);
    Route::post('hunts/{hunt}/participate', [HuntsController::class, 'participate']);
    Route::post('hunts/{hunt}/start', [HuntsController::class, 'start']);
    Route::get('hunts/{hunt}/current-step', [HuntsController::class, 'currentStep']);
    Route::delete('hunts/{hunt}/participate', [HuntsController::class, 'unparticipate']);
    Route::post('hunts/{hunt}/steps/{step}/validate', [HuntStepsController::class, 'validateStep']);
});
