<?php

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\PerhitunganController;
use App\Http\Controllers\CriteriaController;
use App\Http\Controllers\DataUserController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\SubCriteriaController;
use App\Http\Controllers\ExtracurricularController;

/*
|--------------------------------------------------------------------------
| 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('/', function () {
    return view('home', [
        "title" => "Home"
    ]);
});

Route::get('/home', function () {
    return view('home',[
        "title" => "Home"
    ]);
})->name('home');

Route::get('/data-ekstrakurikuler', function () {
    return view('ekstrakurikuler', [
        "title" => "Ekstrakurikuler"
    ]);
});

Route::get('/login', function () {
    if (Auth::check()) {
        return redirect('/dashboard');
    }
    return view('auth.login');
})->name('login');
Route::post('/login', [AuthController::class, 'login']);

Route::get('/register', function () {
    if (Auth::check()) {
        return redirect('/login');
    }
    return view('auth.register');
})->name('register');
Route::post('/register', [AuthController::class, 'register']);

Route::get('/forgot-password', function () {
    if (Auth::check()) {
        return redirect('/dashboard');
    }
    return view('auth.forgot-password');
})->name('password.request');

Route::post('/forgot-password', [AuthController::class, 'sendResetLinkEmail'])->name('password.email');

Route::get('/reset-password/{token}', [AuthController::class, 'showResetForm'])->name('password.reset');
Route::post('/reset-password', [AuthController::class, 'resetPassword'])->name('password.update');

Route::middleware(['auth', 'web'])->group(function () {
    // Routes yang dapat diakses oleh semua pengguna yang sudah login
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard.index');

    Route::get('/user-profile', function () {
        if (!Auth::check()) {
            return redirect('/login');
        }
        return view('user.user-profile');
    })->name('user-profile');

    Route::post('/user-profile', [UserController::class, 'update_profile']);
    Route::post('/update-profile-picture', [UserController::class, 'updateProfilePicture'])->name('update-profile-picture');
    Route::post('/update-password', [UserController::class, 'updatePassword'])->name('update-password');

    Route::post('/riwayat-hasil', [PerhitunganController::class, 'getRiwayatHasil'])->name('riwayat.hasil');
    Route::get('/riwayat-hasil', [PerhitunganController::class, 'getRiwayatHasil'])->name('riwayat.hasil');

    Route::get('/logout', [AuthController::class, 'logout'])->name('logout');
    Route::post('/logout', [AuthController::class, 'logout'])->name('logout');

    // Group middleware untuk admin
    Route::middleware('role:admin')->group(function () {
        // Routes yang hanya dapat diakses oleh admin
        Route::resource('ekstrakurikuler', ExtracurricularController::class);
        Route::resource('kriteria', CriteriaController::class);
        Route::resource('subkriteria', SubCriteriaController::class);
        Route::resource('user', DataUserController::class);
        Route::get('/export-user',[DataUserController::class, 'userexport'])->name('export-user');
        Route::post('/import-user',[DataUserController::class, 'userimport'])->name('import-user');
        Route::get('/download-user-template', [DataUserController::class, 'downloadUserTemplate'])->name('download-user-template');
        Route::post('/kriteria/calculate', [PerhitunganController::class, 'calculateBobotKriteria'])->name('kriteria.calculate');
        Route::post('/subkriteria/calculate', [PerhitunganController::class, 'calculateBobotSubKriteria'])->name('subkriteria.calculate');
    });

    // Group middleware untuk siswa
    Route::middleware('role:siswa')->group(function () {
        // Routes yang hanya dapat diakses oleh siswa
        Route::get('input-nilai', [PerhitunganController::class, 'penilaian'])->name(('penilaian.input-nilai'));
        Route::post('transformasi-nilai', [PerhitunganController::class, 'transformasiNilai'])->name('transformasiNilai');
        Route::post('nilai-utility', [PerhitunganController::class, 'nilaiUtility'])->name('nilaiUtility');
        Route::post('nilai-akhir', [PerhitunganController::class, 'nilaiAkhir'])->name('nilaiAkhir');
        Route::post('perangkingan', [PerhitunganController::class, 'perangkingan'])->name('perangkingan');
    });

});
