<?php

use App\Http\Controllers\Auth\AuthenticatedSessionController;
use App\Http\Controllers\Auth\ConfirmablePasswordController;
use App\Http\Controllers\Auth\EmailVerificationNotificationController;
use App\Http\Controllers\Auth\EmailVerificationPromptController;
use App\Http\Controllers\Auth\NewPasswordController;
use App\Http\Controllers\Auth\PasswordController;
use App\Http\Controllers\Auth\PasswordResetLinkController;
use App\Http\Controllers\Auth\RegisteredUserController;
use App\Http\Controllers\Auth\RegistrationVerificationController;
use App\Http\Controllers\Auth\SetupEmailController;
use App\Http\Controllers\Auth\SetupPasswordController;
use App\Http\Controllers\Auth\SetupUsernameController;
use App\Http\Controllers\Auth\OAuthController;
use App\Http\Controllers\Auth\VerifyEmailController;
use Illuminate\Support\Facades\Route;

// ── OAuth / Social Login ─────────────────────────────────────────────────────
Route::middleware('guest')->group(function (): void {
    Route::get('auth/{provider}/redirect', [OAuthController::class, 'redirectToProvider'])
        ->where('provider', 'google|discord')
        ->name('oauth.redirect');

    // Google and Discord use GET callbacks; Apple sends a POST on first login.
    Route::match(['get', 'post'], 'auth/{provider}/callback', [OAuthController::class, 'handleProviderCallback'])
        ->where('provider', 'google|discord')
        ->name('oauth.callback');
});

Route::middleware(['guest', 'normalize.username'])->group(function () {
    Route::get('register', [RegisteredUserController::class, 'create'])
        ->name('register');

    Route::get('register/notice', [RegisteredUserController::class, 'notice'])
        ->name('register.notice');

    Route::post('register', [RegisteredUserController::class, 'store'])
        ->middleware(['throttle:register-ip', 'throttle:register-ip-daily', 'forum.security.firewall:register', 'forum.bot.protection:register']);

    Route::post('register/resend-verification', [RegisteredUserController::class, 'resendVerification'])
        ->middleware('throttle:register')
        ->name('register.resend');

    Route::get('verify/{token}', RegistrationVerificationController::class)
        ->name('registration.verify');

    Route::get('login', [AuthenticatedSessionController::class, 'create'])
        ->name('login');

    Route::post('login', [AuthenticatedSessionController::class, 'store'])
        ->middleware(['forum.security.firewall:login', 'forum.bot.protection:login']);

    Route::get('forgot-password', [PasswordResetLinkController::class, 'create'])
        ->name('password.request');

    Route::post('forgot-password', [PasswordResetLinkController::class, 'store'])
        ->name('password.email');

    Route::get('reset-password/{token}', [NewPasswordController::class, 'create'])
        ->name('password.reset');

    Route::post('reset-password', [NewPasswordController::class, 'store'])
        ->name('password.store');
});

Route::middleware('auth')->group(function () {
    Route::get('setup/email', [SetupEmailController::class, 'create'])
        ->name('setup.email.create');

    Route::post('setup/email', [SetupEmailController::class, 'requestCode'])
        ->name('setup.email.store');

    Route::get('setup/password', [SetupPasswordController::class, 'create'])
        ->name('setup.password.create');

    Route::post('setup/password', [SetupPasswordController::class, 'store'])
        ->name('setup.password.store');

    Route::get('setup/username', [SetupUsernameController::class, 'create'])
        ->name('setup.username.create');

    Route::post('setup/username', [SetupUsernameController::class, 'store'])
        ->name('setup.username.store');

    Route::get('verify-email', EmailVerificationPromptController::class)
        ->name('verification.notice');

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

    Route::post('email/verification-notification', [EmailVerificationNotificationController::class, 'store'])
        ->middleware('throttle:6,1')
        ->name('verification.send');

    Route::get('confirm-password', [ConfirmablePasswordController::class, 'show'])
        ->name('password.confirm');

    Route::post('confirm-password', [ConfirmablePasswordController::class, 'store']);

    Route::put('password', [PasswordController::class, 'update'])->name('password.update');

        Route::get('logout', [AuthenticatedSessionController::class, 'destroy'])
        ->name('logout.get');

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