<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\MahasiswaController;
use App\Http\Controllers\DosenController;
use App\Http\Controllers\PengajuanP1Controller;
use App\Http\Controllers\P1Controller;
use App\Http\Controllers\P2Controller;
use App\Http\Controllers\P3Controller;
use App\Http\Controllers\P4Controller;
use App\Http\Controllers\P5Controller;
use App\Http\Controllers\LaporanController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\KriteriaPenilaianController;
use App\Http\Controllers\PenilaianController;
use App\Http\Controllers\RolePermissionController;

Route::get('/', function () {
    return redirect()->route('dashboard');
});

Route::middleware(['auth'])->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

    // Master Data
    Route::get('mahasiswa/export', [MahasiswaController::class, 'export'])->name('mahasiswa.export');
    Route::post('mahasiswa/import', [MahasiswaController::class, 'import'])->name('mahasiswa.import');
    Route::get('mahasiswa/template', [MahasiswaController::class, 'downloadTemplate'])->name('mahasiswa.template');
    Route::get('mahasiswa/detail/{nim}', [MahasiswaController::class, 'getDetail'])->name('mahasiswa.detail');
    Route::resource('mahasiswa', MahasiswaController::class);

    Route::get('dosen/export', [DosenController::class, 'export'])->name('dosen.export');
    Route::post('dosen/import', [DosenController::class, 'import'])->name('dosen.import');
    Route::get('dosen/template', [DosenController::class, 'downloadTemplate'])->name('dosen.template');
    Route::resource('dosen', DosenController::class);

    // Pengajuan P1
    Route::prefix('p1/pengajuan')->name('p1.pengajuan.')->group(function () {
        Route::get('/', [PengajuanP1Controller::class, 'index'])->name('index');
        Route::get('/create', [PengajuanP1Controller::class, 'create'])->name('create');
        Route::post('/store', [PengajuanP1Controller::class, 'store'])->name('store');
        Route::post('/{pengajuan}/approve', [PengajuanP1Controller::class, 'approve'])->name('approve');
        Route::post('/{pengajuan}/reject', [PengajuanP1Controller::class, 'reject'])->name('reject');
    });

    // Izin P1
    Route::prefix('izin-p1')->name('izin_p1.')->group(function () {
        Route::get('/', [\App\Http\Controllers\IzinP1Controller::class, 'index'])->name('index');
        Route::get('/create', [\App\Http\Controllers\IzinP1Controller::class, 'create'])->name('create');
        Route::post('/store', [\App\Http\Controllers\IzinP1Controller::class, 'store'])->name('store');
        Route::get('/{id}/edit', [\App\Http\Controllers\IzinP1Controller::class, 'edit'])->name('edit');
        Route::put('/{id}', [\App\Http\Controllers\IzinP1Controller::class, 'update'])->name('update');
        Route::delete('/{id}', [\App\Http\Controllers\IzinP1Controller::class, 'destroy'])->name('destroy');
        Route::get('/pdf/{id}', [\App\Http\Controllers\IzinP1Controller::class, 'cetakPdf'])->name('pdf');
    });

    // Surat P1
    Route::prefix('p1/persetujuan')->name('p1.persetujuan.')->group(function () {
        Route::get('/', [P1Controller::class, 'index'])->name('index');
        Route::get('/create', [P1Controller::class, 'create'])->name('create');
        Route::post('/store', [P1Controller::class, 'store'])->name('store');
        Route::get('/{p1_persetujuan_penelitian}/edit', [P1Controller::class, 'edit'])->name('edit')->where('p1_persetujuan_penelitian', '.*');
        Route::put('/{p1_persetujuan_penelitian}', [P1Controller::class, 'update'])->name('update')->where('p1_persetujuan_penelitian', '.*');
        Route::delete('/{p1_persetujuan_penelitian}', [P1Controller::class, 'destroy'])->name('destroy')->where('p1_persetujuan_penelitian', '.*');
        Route::get('/pdf/{p1}', [P1Controller::class, 'cetak_pdf'])->name('pdf')->where('p1', '.*');
    });

    // Surat P2
    Route::prefix('p2/sk-pembimbing')->name('p2.sk_pembimbing.')->group(function () {
        Route::get('/', [P2Controller::class, 'index'])->name('index');
        Route::get('/create', [P2Controller::class, 'create'])->name('create');
        Route::post('/store', [P2Controller::class, 'store'])->name('store');
        Route::get('/{p2_sk_pembimbing}/edit', [P2Controller::class, 'edit'])->name('edit')->where('p2_sk_pembimbing', '.*');
        Route::put('/{p2_sk_pembimbing}', [P2Controller::class, 'update'])->name('update')->where('p2_sk_pembimbing', '.*');
        Route::delete('/{p2_sk_pembimbing}', [P2Controller::class, 'destroy'])->name('destroy')->where('p2_sk_pembimbing', '.*');
        Route::get('/pdf/{nosrt}', [P2Controller::class, 'cetak_pdf'])->name('pdf')->where('nosrt', '.*');
    });

    // Surat P3
    Route::prefix('p3/surat-penelitian')->name('p3.surat_penelitian.')->group(function () {
        Route::get('/', [P3Controller::class, 'index'])->name('index');
        Route::get('/create', [P3Controller::class, 'create'])->name('create');
        Route::post('/store', [P3Controller::class, 'store'])->name('store');
        Route::get('/{p3_surat_penelitian}/edit', [P3Controller::class, 'edit'])->name('edit')->where('p3_surat_penelitian', '.*');
        Route::put('/{p3_surat_penelitian}', [P3Controller::class, 'update'])->name('update')->where('p3_surat_penelitian', '.*');
        Route::delete('/{p3_surat_penelitian}', [P3Controller::class, 'destroy'])->name('destroy')->where('p3_surat_penelitian', '.*');
        Route::get('/pdf/{nosrt}', [P3Controller::class, 'cetak_pdf'])->name('pdf')->where('nosrt', '.*');
    });

    // Surat P4
    Route::prefix('p4/tim-penguji')->name('p4.tim_penguji.')->group(function () {
        Route::get('/', [P4Controller::class, 'index'])->name('index');
        Route::get('/create', [P4Controller::class, 'create'])->name('create');
        Route::post('/store', [P4Controller::class, 'store'])->name('store');
        Route::get('/{p4_tim_penguji}/edit', [P4Controller::class, 'edit'])->name('edit');
        Route::put('/{p4_tim_penguji}', [P4Controller::class, 'update'])->name('update');
        Route::delete('/{p4_tim_penguji}', [P4Controller::class, 'destroy'])->name('destroy');
        Route::get('/pdf/{id}', [P4Controller::class, 'cetak_pdf'])->name('pdf');
    });

    // Surat P5
    Route::prefix('p5/undangan-ujian')->name('p5.undangan_ujian.')->group(function () {
        Route::get('/', [P5Controller::class, 'index'])->name('index');
        Route::get('/create', [P5Controller::class, 'create'])->name('create');
        Route::post('/store', [P5Controller::class, 'store'])->name('store');
        Route::get('/{p5_undangan_ujian}/edit', [P5Controller::class, 'edit'])->name('edit')->where('p5_undangan_ujian', '.*');
        Route::put('/{p5_undangan_ujian}', [P5Controller::class, 'update'])->name('update')->where('p5_undangan_ujian', '.*');
        Route::delete('/{p5_undangan_ujian}', [P5Controller::class, 'destroy'])->name('destroy')->where('p5_undangan_ujian', '.*');
        Route::get('/pdf/{nosrt}', [P5Controller::class, 'cetak_pdf'])->name('pdf')->where('nosrt', '.*');
    });



    // Kriteria Penilaian
    Route::resource('kriteria', KriteriaPenilaianController::class)->except(['show']);

    // Penilaian
    Route::prefix('penilaian')->name('penilaian.')->group(function () {
        Route::get('/', [PenilaianController::class, 'index'])->name('index');
        Route::get('/{nim}', [PenilaianController::class, 'detail'])->name('detail');
        Route::get('/{nim}/input/{penguji_ke}', [PenilaianController::class, 'input'])->name('input');
        Route::post('/{nim}/input/{penguji_ke}', [PenilaianController::class, 'store'])->name('store');
        Route::get('/{nim}/cetak/{penguji_ke}', [PenilaianController::class, 'cetak_form'])->name('cetak_form');
        Route::get('/{nim}/rekap/cetak', [PenilaianController::class, 'cetak_rekap'])->name('cetak_rekap');
    });

    // Laporan
    Route::prefix('laporan')->name('laporan.')->group(function () {
        Route::get('/p1', [LaporanController::class, 'p1'])->name('p1');
        Route::get('/p2', [LaporanController::class, 'p2'])->name('p2');
        Route::get('/p3', [LaporanController::class, 'p3'])->name('p3');
        Route::get('/p4', [LaporanController::class, 'p4'])->name('p4');
        Route::get('/p5', [LaporanController::class, 'p5'])->name('p5');
    });

    // User Management
    Route::resource('pengguna', UserController::class);

    // Permissions
    Route::get('permissions', [RolePermissionController::class, 'index'])->name('permissions.index');
    Route::post('permissions', [RolePermissionController::class, 'update'])->name('permissions.update');
});

require __DIR__.'/auth.php';
