<?php

use Illuminate\Support\Facades\Route;

Route::redirect('/', '/portal');

Route::middleware('auth')->get('/portal', [\App\Http\Controllers\Web\PortalController::class, 'index'])->name('portal');


// Generic Login (Allow authenticated users to visit for redirect logic)
Route::get('/login', [\App\Http\Controllers\Web\LoginController::class, 'showLogin'])->name('login');
Route::post('/logout', [\App\Http\Controllers\Web\LoginController::class, 'logout'])->name('logout');

Route::middleware('guest')->group(function () {
    Route::post('/login', [\App\Http\Controllers\Web\LoginController::class, 'login']);

    // Forgot Password
    Route::get('/forgot-password', [\App\Http\Controllers\Web\ForgotPasswordController::class, 'showForm'])->name('password.forgot');
    Route::post('/forgot-password', [\App\Http\Controllers\Web\ForgotPasswordController::class, 'sendResetLink'])->name('password.forgot.submit');
    Route::get('/verify-otp', [\App\Http\Controllers\Web\ForgotPasswordController::class, 'showVerifyOtp'])->name('password.verify-otp');
    Route::post('/verify-otp', [\App\Http\Controllers\Web\ForgotPasswordController::class, 'verifyOtp'])->name('password.verify-otp.submit');
    Route::get('/reset-password', [\App\Http\Controllers\Web\ForgotPasswordController::class, 'showResetForm'])->name('password.reset');
    Route::post('/reset-password', [\App\Http\Controllers\Web\ForgotPasswordController::class, 'resetPassword'])->name('password.reset.submit');

    // Admin Login
    Route::get('/admin/login', [\App\Http\Controllers\Admin\AuthController::class, 'showLogin'])->name('admin.login');
    Route::post('/admin/login', [\App\Http\Controllers\Admin\AuthController::class, 'login'])->name('admin.login.submit');
});

Route::middleware('auth')->group(function () {
    Route::put('/change-password', [\App\Http\Controllers\Web\LoginController::class, 'changePassword'])->name('change-password');
});

Route::middleware(['auth', 'admin.role', 'mfa.verified'])->prefix('admin')->name('admin.')->group(function () {
    Route::post('/logout', [\App\Http\Controllers\Admin\AuthController::class, 'logout'])->name('logout');

    Route::get('/users', [\App\Http\Controllers\Admin\UserController::class, 'index'])->name('users.index');
    Route::get('/users/create', [\App\Http\Controllers\Admin\UserController::class, 'create'])->name('users.create');
    Route::get('/users/sync/preview', [\App\Http\Controllers\Admin\UserController::class, 'previewSync'])->name('users.sync.preview');
    Route::post('/users/sync', [\App\Http\Controllers\Admin\UserController::class, 'sync'])->name('users.sync');
    Route::post('/users', [\App\Http\Controllers\Admin\UserController::class, 'store'])->name('users.store');
    Route::get('/users/{user}/edit', [\App\Http\Controllers\Admin\UserController::class, 'edit'])->name('users.edit');
    Route::put('/users/{user}', [\App\Http\Controllers\Admin\UserController::class, 'update'])->name('users.update');

    Route::get('/audit', [\App\Http\Controllers\Admin\AuditController::class, 'index'])->name('audit.index');
    Route::get('/audit/export', [\App\Http\Controllers\Admin\AuditController::class, 'export'])->name('audit.export');

    Route::get('/settings', [\App\Http\Controllers\Admin\SettingController::class, 'index'])->name('settings.index');
    Route::get('/settings', [\App\Http\Controllers\Admin\SettingController::class, 'index'])->name('settings.index');
    Route::post('/settings', [\App\Http\Controllers\Admin\SettingController::class, 'update'])->name('settings.update');
});

Route::middleware('auth')->prefix('admin/mfa')->name('admin.mfa.')->group(function () {
    Route::get('/', [\App\Http\Controllers\Admin\MfaController::class, 'showChallenge'])->name('challenge');
    Route::post('/', [\App\Http\Controllers\Admin\MfaController::class, 'verify'])->name('verify');
});
