<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\RegistrasiController;
use App\Http\Controllers\Auth\ResetPasswordController;
use App\Http\Controllers\Mahasiswa\BiodataController;
use App\Http\Controllers\Mahasiswa\DashboardController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('registrasi', [RegistrasiController::class, 'index'])->name('registrasi');
Route::post('registrasi', [RegistrasiController::class, 'register']);

Route::get('reset-password', [ResetPasswordController::class, 'index'])->name('reset-password');
Route::post('cek-email', [ResetPasswordController::class, 'cek'])->name('cek-email');
Route::post('reset-password/submit', [ResetPasswordController::class, 'reset'])->name('submit-password');

Route::get('login', [LoginController::class, 'index'])->name('login');
Route::post('login', [LoginController::class, 'login']);
Route::post('logout', [LoginController::class, 'logout'])->name('logout');
// Route::get('sso/{email}/{sessionid}', [LoginController::class, 'sso']);

Route::get('/', function () {
    if (auth()->check()) {
        if (auth()->user()->role == 'laman') {
            return redirect()->route('mahasiswa.dashboard');
        }
        elseif (auth()->user()->role == 'admin') {
            return redirect()->route('mahasiswa.dashboard');
        } else {
            return redirect()->route('login');
        }
    }

    return redirect()->route('login');
});

Route::middleware('auth')->group(function () {
    // Routes for Admin

    // Routes for Mahasiswa
    Route::prefix('pendaftar')->middleware('role:laman')->group(function () {
        Route::get('dashboard', [DashboardController::class, 'index'])->name('mahasiswa.dashboard');
        Route::resource('biodata', BiodataController::class);
    });
});

// Route::get('/logout', [LoginController::class, 'logout'])->name('admin.logout');