<?php

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\FormController;
use App\Http\Controllers\KriteriaController;
use App\Http\Controllers\SubkriteriaController;
use App\Http\Controllers\CripsController;
use App\Http\Controllers\PenilaianController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\RankingController;
use App\Http\Controllers\PengajuanController;
use App\Http\Controllers\AdminPengajuanController;
use App\Http\Controllers\SawController;
use App\Http\Controllers\StatusFormController;
use App\Http\Controllers\MahasiswaController;



/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/



// Authentication Routes
Route::get('login', [App\Http\Controllers\Auth\LoginController::class, 'showLoginForm'])->name('login');
Route::post('login', [App\Http\Controllers\Auth\LoginController::class, 'login']);
Route::post('logout', [App\Http\Controllers\Auth\LoginController::class, 'logout'])->name('logout');
Route::get('/', function () {
    return redirect()->route('login'); 
});


// Route untuk guest (belum login)
Route::middleware('guest')->group(function () {
    Route::get('/register/mahasiswa', [UserController::class, 'showMahasiswaRegistrationForm'])
         ->name('register.mahasiswa.form');
    
    Route::post('/register/mahasiswa', [UserController::class, 'registerMahasiswa'])
         ->name('register.mahasiswa');
});

Route::middleware(['auth'])->group(function () {
    // Redirect berdasarkan role
    Route::get('/redirect', function() {
        $user = Auth::user();
        
        if ($user->role === 'admin') {
            return redirect()->route('home');
        } 
        elseif ($user->role === 'karyawan') {
            return redirect()->route('dashboard.karyawan');
        }
        elseif ($user->role === 'mahasiswa') {
            return redirect()->route('dashboard.mahasiswa');
        }
        
        Auth::logout();
        return redirect('/login')->with('error', 'Role tidak valid');
    })->name('redirect');

    
    Route::middleware(['admin'])->group(function () {
        Route::get('/home', [HomeController::class, 'showAdminDashboard'])->name('home');
        
        Route::prefix('admin')->group(function() {
            Route::get('/users', [UserController::class, 'index'])->name('users.index');

        
            Route::delete('/users/{user}', [UserController::class, 'destroy'])
                ->name('users.destroy')
                ->middleware('can:delete,user');

            // Pengajuan
            Route::delete('admin/pengajuan/{id}', [AdminPengajuanController::class, 'destroy'])
                ->name('admin.pengajuan.destroy');
            Route::put('/admin/pengajuan/{id}/revalidate', [AdminPengajuanController::class, 'revalidate'])
                ->name('admin.pengajuan.revalidate');

            // Penilaian Routes
            Route::prefix('penilaian')->group(function() {
                Route::get('/', [PenilaianController::class, 'index'])->name('admin.penilaian.index');
                Route::post('/process-batch', [PenilaianController::class, 'processBatch'])->name('admin.penilaian.process-batch');
                Route::get('/result/{id}', [PenilaianController::class, 'result'])->name('admin.penilaian.result');
            });

            // Ranking Routes 
            Route::prefix('ranking')->group(function() {
                Route::get('/', [RankingController::class, 'index'])->name('admin.ranking.index');
                Route::get('/export', [RankingController::class, 'export'])
                    ->name('admin.ranking.export');
                Route::post('/recalculate', [RankingController::class, 'recalculateAll'])->name('admin.ranking.recalculate');
                Route::post('/upload', [AdminPengajuanController::class, 'uploadRanking'])->name('admin.ranking.upload');
                Route::delete('delete', [AdminPengajuanController::class, 'destroyRankingFile'])->name('ranking.delete');
                Route::post('/{id}/update-ukt', [RankingController::class, 'updateUkt'])
                 ->name('admin.ranking.update-ukt');
                Route::post('/set-budget', [RankingController::class, 'setBudget'])->name('admin.ranking.set-budget');
                Route::post('/set-active-budget', [RankingController::class, 'setActiveBudget'])->name('admin.ranking.set-active-budget');
            });
        });

        // Kuota
       Route::post('/forms/{form}/update-quota', [FormController::class, 'updateQuota'])
            ->middleware(['auth', 'admin'])
            ->name('form.updateQuota');
        
        // Resource Controllers
        Route::resources([
            'user' => UserController::class
        ]);

        // Ranking File
        Route::post('/ranking-report', [AdminPengajuanController::class, 'storeRankingReport'])
            ->name('admin.ranking-report.store');
    
        Route::delete('/ranking-report', [AdminPengajuanController::class, 'deleteRankingReport'])
            ->name('admin.ranking-report.delete');
        
        Route::get('/ranking-report/{id}', [AdminPengajuanController::class, 'viewRankingReport'])
            ->name('admin.ranking-report.view');
        
        // Kriteria 
        Route::resource('kriteria', KriteriaController::class);
        Route::get('kriteria/download/pdf', [KriteriaController::class, 'downloadPDF'])->name('kriteria.download.pdf');

        // Sub Kriteria 
        Route::resource('subkriteria', SubkriteriaController::class, [
         'parameters' => ['subkriteria' => 'id']
        ]);
        Route::get('/download/subkriteria-pdf/{kriteria_id?}', [SubkriteriaController::class, 'downloadPDF'])
            ->name('subkriteria.download.pdf');

        Route::prefix('kriteria/{kriteria}')->group(function() {
            Route::get('subkriteria', [SubkriteriaController::class, 'indexByKriteria'])
                ->name('kriteria.subkriteria.index');
        });

        // Form Settings
        Route::resource('form-settings', StatusFormController::class)->only(['index', 'update']);
    });

    // ================= Admin dan Karyawan ============== //
    Route::middleware('admin.karyawan')->group(function () {
        Route::get('/pengajuan/{id}/detail', [AdminPengajuanController::class, 'detail'])->name('admin.pengajuan.detail');

        Route::prefix('admin/pengajuan')->group(function () {
             Route::get('/', [AdminPengajuanController::class, 'index'])->name('admin.pengajuan');
             Route::get('/{id}', [AdminPengajuanController::class, 'detail'])->name('admin.pengajuan.detail');
             
        });

         Route::prefix('form')->group(function () {
            Route::get('/pendaftaran', [FormController::class, 'pendaftaran'])->name('form.pendaftaran');
        });

         Route::get('/pengajuan/{pengajuanId}/document/{docType}', [PengajuanController::class, 'viewDocument'])
            ->name('pengajuan.document.view');
    });


    // ================== KARYAWAN ROUTES ================== //
    Route::middleware(['karyawan'])->group(function () {
         Route::get('/karyawan/dashboard', function () {
            return view('karyawan.dashboard');
        })->name('dashboard.karyawan');

        Route::post('/pengajuan/{id}/verify', [AdminPengajuanController::class, 'bulkVerify'])
            ->name('karyawan.pengajuan.verify');
        Route::post('/pengajuan/{id}/verify-ukt', [AdminPengajuanController::class, 'verifyUKT'])
            ->name('admin.pengajuan.verify-ukt');
            
        Route::prefix('form')->group(function () {
            Route::get('/', [FormController::class, 'index'])->name('form.index'); 
            Route::get('/create', [FormController::class, 'create'])->name('form.create');
            Route::post('/store', [FormController::class, 'store'])->name('form.store');
            Route::delete('/{id}', [FormController::class, 'destroy'])->name('form.destroy');
            Route::post('/form/{id}/toggle-status', [FormController::class, 'toggleStatus'])->name('form.toggleStatus');
            Route::get('/{id}/edit', [FormController::class, 'edit'])->name('form.edit');
            Route::put('/{id}', [FormController::class, 'update'])->name('form.update');
        });
    
    });

    // ================== MAHASISWA ROUTES ================== //
    Route::middleware(['mahasiswa'])->group(function () {
        Route::get('/mahasiswa/dashboard', [MahasiswaController::class, 'dashboard'])
            ->name('dashboard.mahasiswa');        
        Route::get('/pengajuan', [PengajuanController::class, 'index'])->name('pengajuan.index');
        Route::get('/pengajuan/{jenis}/create', [PengajuanController::class, 'create'])
            ->whereIn('jenis', ['penurunan', 'pengangsuran'])
            ->name('pengajuan.create');
        Route::post('/pengajuan/{jenis}/store', [PengajuanController::class, 'store'])
            ->whereIn('jenis', ['penurunan', 'pengangsuran'])
            ->name('pengajuan.store');
        Route::put('/profile/update', [UserController::class, 'update'])->name('profile.update');

        Route::prefix('form')->group(function () {
            Route::get('/penurunan', [FormController::class, 'showReductionForm'])->name('form.penurunan');
            Route::get('/pengangsuran', [FormController::class, 'showInstallmentForm'])->name('form.pengangsuran');
         });

        Route::get('/ranking/{id}/download', [MahasiswaController::class, 'downloadRanking'])->name('mahasiswa.ranking.download');
    });

    // penambahan route kosong karena ada route yang tidak ada
    // sehingga ada error looping yang tidak diinginkan dan menyebabkan error ketika diakses
    // contoh error seperti "PHP Fatal error: Allowed memory size of 2147483648 bytes exhausted"
});