<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\Admin\InstansiController;
use App\Http\Controllers\Admin\DashboardController as AdminDashboardController;
use App\Http\Controllers\Admin\PengaturanSistemController as AdminPengaturanController;
use App\Http\Controllers\Admin\LaporanController as AdminLaporanController;
use App\Http\Controllers\Instansi\KaryawanController;
use App\Http\Controllers\Instansi\JabatanController;
use App\Http\Controllers\Instansi\JamKerjaController;
use App\Http\Controllers\Instansi\AbsensiController as InstansiAbsensiController;
use App\Http\Controllers\Instansi\RekapController;
use App\Http\Controllers\Instansi\IzinController as InstansiIzinController;
use App\Http\Controllers\Instansi\CutiController as InstansiCutiController;
use App\Http\Controllers\Instansi\LemburController as InstansiLemburController;
use App\Http\Controllers\Instansi\PengaturanController;
use App\Http\Controllers\Karyawan\AbsensiController;
use App\Http\Controllers\Karyawan\IzinSakitController;
use App\Http\Controllers\Karyawan\CutiController;
use App\Http\Controllers\Karyawan\LemburController;
use App\Http\Controllers\Karyawan\HistoryController;

Route::get('/', fn() => view('welcome'));

Route::get('/dashboard', function () {
    $role = auth()->user()->role;
    if ($role === 'admin')    return app(AdminDashboardController::class)->index();
    if ($role === 'instansi') return view('instansi.dashboard');
    if ($role === 'karyawan') return view('karyawan.dashboard');
    return abort(403);
})->middleware('auth')->name('dashboard');

Route::prefix('admin')
    ->name('admin.')
    ->middleware(['auth', 'role:admin'])
    ->group(function () {
        Route::resource('instansi', InstansiController::class)->except(['show', 'edit']);
        Route::get('instansi/{instansi}', [InstansiController::class, 'show'])->name('instansi.show');
        Route::get('laporan',              [AdminLaporanController::class, 'index'])->name('laporan.index');
        Route::get('laporan/export-pdf',   [AdminLaporanController::class, 'exportPdf'])->name('laporan.export-pdf');
        Route::get('laporan/export-excel', [AdminLaporanController::class, 'exportExcel'])->name('laporan.export-excel');
        Route::get('pengaturan',              [AdminPengaturanController::class, 'index'])->name('pengaturan.index');
        Route::put('pengaturan/aplikasi',     [AdminPengaturanController::class, 'updateAplikasi'])->name('pengaturan.aplikasi');
        Route::put('pengaturan/jam-kerja',    [AdminPengaturanController::class, 'updateJamKerja'])->name('pengaturan.jam-kerja');
        Route::put('pengaturan/profil',       [AdminPengaturanController::class, 'updateProfil'])->name('pengaturan.profil');
        Route::put('pengaturan/password',     [AdminPengaturanController::class, 'updatePassword'])->name('pengaturan.password');
        Route::get('pengaturan/clear-cache',  [AdminPengaturanController::class, 'clearCache'])->name('pengaturan.clear-cache');
    });

Route::prefix('instansi')
    ->name('instansi.')
    ->middleware(['auth', 'role:instansi'])
    ->group(function () {
        Route::get('karyawan/{id}/edit-data', [KaryawanController::class, 'editData'])->name('karyawan.edit-data');
        Route::resource('karyawan', KaryawanController::class);
        Route::resource('jabatan', JabatanController::class);
        Route::resource('jam-kerja', JamKerjaController::class);
        Route::get('absensi',  [InstansiAbsensiController::class, 'index'])->name('absensi.index');
        Route::get('rekap',    [RekapController::class, 'index'])->name('rekap.index');
        Route::get('izin',     [InstansiIzinController::class, 'index'])->name('izin.index');
        Route::post('izin/{id}/approve', [InstansiIzinController::class, 'approve'])->name('izin.approve');
        Route::post('izin/{id}/reject',  [InstansiIzinController::class, 'reject'])->name('izin.reject');
        Route::get('cuti',               [InstansiCutiController::class, 'index'])->name('cuti.index');
        Route::post('cuti/{id}/approve', [InstansiCutiController::class, 'approve'])->name('cuti.approve');
        Route::post('cuti/{id}/reject',  [InstansiCutiController::class, 'reject'])->name('cuti.reject');
        Route::post('cuti/jatah/{karyawanId}', [InstansiCutiController::class, 'setJatah'])->name('cuti.set-jatah');
        Route::get('lembur',               [InstansiLemburController::class, 'index'])->name('lembur.index');
        Route::post('lembur/{id}/approve', [InstansiLemburController::class, 'approve'])->name('lembur.approve');
        Route::post('lembur/{id}/reject',  [InstansiLemburController::class, 'reject'])->name('lembur.reject');
        Route::get('pengaturan',           [PengaturanController::class, 'index'])->name('pengaturan.index');
        Route::put('pengaturan/instansi',  [PengaturanController::class, 'updateInstansi'])->name('pengaturan.instansi');
        Route::put('pengaturan/akun',      [PengaturanController::class, 'updateAkun'])->name('pengaturan.akun');
    });

Route::prefix('karyawan')
    ->name('karyawan.')
    ->middleware(['auth', 'role:karyawan'])
    ->group(function () {

        Route::post('absen/masuk',    [AbsensiController::class, 'absenMasuk'])->name('absen.masuk');
        Route::post('absen/pulang',   [AbsensiController::class, 'absenPulang'])->name('absen.pulang');

        // ✅ ROUTE HISTORY (TAMBAHAN)
        Route::get('history', [HistoryController::class, 'index'])->name('history.index');

        Route::get('absen',           [AbsensiController::class, 'index'])->name('absensis.index');
        Route::get('absen/create',    [AbsensiController::class, 'create'])->name('absensis.create');
        Route::get('absen/{id}',      [AbsensiController::class, 'show'])->name('absensis.show');
        Route::get('absen/{id}/edit', [AbsensiController::class, 'edit'])->name('absensis.edit');
        Route::delete('absen/{id}',   [AbsensiController::class, 'destroy'])->name('absensis.destroy');

        Route::get('izin-sakit',        [IzinSakitController::class, 'index'])->name('izin-sakit.index');
        Route::get('izin-sakit/create', [IzinSakitController::class, 'create'])->name('izin-sakit.create');
        Route::post('izin-sakit',       [IzinSakitController::class, 'store'])->name('izin-sakit.store');
        Route::get('izin-sakit/{id}',   [IzinSakitController::class, 'show'])->name('izin-sakit.show');

        Route::get('cuti',              [CutiController::class, 'index'])->name('cuti.index');
        Route::get('cuti/create',       [CutiController::class, 'create'])->name('cuti.create');
        Route::post('cuti',             [CutiController::class, 'store'])->name('cuti.store');
        Route::get('cuti/{id}',         [CutiController::class, 'show'])->name('cuti.show');

        Route::get('lembur',            [LemburController::class, 'index'])->name('lembur.index');
        Route::get('lembur/create',     [LemburController::class, 'create'])->name('lembur.create');
        Route::post('lembur',           [LemburController::class, 'store'])->name('lembur.store');
        Route::get('lembur/{id}',       [LemburController::class, 'show'])->name('lembur.show');
    });

Route::middleware('auth')->group(function () {
    Route::get('/profile',    [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile',  [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
});

require __DIR__ . '/auth.php';
