<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\DaftarController;
use App\Http\Controllers\ValidasiController; 
use App\Http\Controllers\PengajuanController;
use App\Http\Controllers\LaporanController;
use App\Http\Controllers\ProfilController;
use App\Http\Controllers\DashboardController;

/*
|--------------------------------------------------------------------------
| LANDING PAGE
|--------------------------------------------------------------------------
*/
Route::get('/', function () {
    return view('welcome');
})->name('welcome');

/*
|--------------------------------------------------------------------------
| AUTH - PROSES LOGIN & DAFTAR & LUPA PASSWORD
|--------------------------------------------------------------------------
*/
Route::get('/login', [AuthController::class, 'showLogin'])->name('login');
Route::post('/login', [AuthController::class, 'loginProcess'])->name('login.process');

Route::middleware('guest')->group(function () {
    Route::get('/daftar', [DaftarController::class, 'index'])->name('daftar');
    Route::post('/daftar', [DaftarController::class, 'store'])->name('daftar.store');
    
    // PERBAIKAN LUPA PASSWORD
    Route::get('/forgot-password', [AuthController::class, 'resetPassword'])->name('password.request');
    Route::post('/reset-password', [AuthController::class, 'updatePassword'])->name('password.update');
});

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

/*
|--------------------------------------------------------------------------
| AREA SETELAH LOGIN (MIDDLEWARE AUTH)
|--------------------------------------------------------------------------
*/
Route::middleware('auth')->group(function () {

    /* --- AREA KHUSUS ADMIN & SUPER ADMIN --- */
    Route::middleware('role:admin,super admin')->group(function () {
        Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

        // Manajemen Validasi KTP
        Route::prefix('validasi')->name('validasi.')->group(function () {
            Route::get('/', [ValidasiController::class, 'index'])->name('index');
            Route::post('/', [ValidasiController::class, 'store'])->name('store');
            Route::put('/update/{nik}', [ValidasiController::class, 'update'])->name('update');
            Route::delete('/destroy/{nik}', [ValidasiController::class, 'destroy'])->name('destroy');
        });

        Route::get('/laporan', [LaporanController::class, 'index'])->name('laporan');

        /* | MANAJEMEN USER (DIREVISI)
        | Prefix 'users' akan menghasilkan URL: /users/update/{id}
        | Name 'users.update' memudahkan pemanggilan di Blade: route('users.update', $id)
        */
        Route::prefix('users')->name('users.')->group(function () {
            Route::post('/store', [ProfilController::class, 'store'])->name('store');
            Route::put('/update/{id}', [ProfilController::class, 'updateOther'])->name('update');
            Route::delete('/{id}', [ProfilController::class, 'destroy'])->name('destroy');
        });
    });

    /* --- AREA DASHBOARD USER --- */
    Route::middleware('role:user')->group(function () {
        Route::get('/dashboard-user', [DashboardController::class, 'indexUser'])->name('dashboarduser');

        Route::prefix('user/validasi')->name('user.validasi.')->group(function () {
            Route::get('/', [ValidasiController::class, 'index'])->name('index');
            Route::post('/', [ValidasiController::class, 'store'])->name('store');
            Route::put('/{nik}', [ValidasiController::class, 'update'])->name('update');
            Route::delete('/{nik}', [ValidasiController::class, 'destroy'])->name('destroy');
        });

        Route::prefix('user/pengajuan')->name('user.pengajuan.')->group(function () {
            Route::get('/', [PengajuanController::class, 'indexUser'])->name('index');
            Route::post('/store', [PengajuanController::class, 'store'])->name('store');
            Route::put('/update/{id}', [PengajuanController::class, 'update'])->name('update');
            Route::delete('/delete/{id}', [PengajuanController::class, 'destroy'])->name('destroy');
        });
    });

    /* --- AREA BERSAMA (SHARED) --- */
    Route::get('/profil', [ProfilController::class, 'index'])->name('profil');
    Route::match(['post', 'put'], '/profil/update', [ProfilController::class, 'update'])->name('profil.update');

    // Manajemen Pengajuan Global
    Route::prefix('pengajuan')->name('pengajuan.')->group(function () {
        Route::get('', [PengajuanController::class, 'index'])->name('index');
        Route::post('/store', [PengajuanController::class, 'store'])->name('store');
        Route::put('/update/{id}', [PengajuanController::class, 'update'])->name('update');
        Route::delete('/{id}', [PengajuanController::class, 'destroy'])->name('destroy');
    });
});