<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Admin\{
    DashboardController,
    AbsensiController,
    SiswaController,
    GuruController,
    UserController,
    LaporanController,
    HariLiburController,
    AbsenManualController, // <-- SUDAH DIMASUKKAN KE KELOMPOK ADMIN DI SINI
};
use App\Http\Controllers\Petugas\PetugasController;

/*
|--------------------------------------------------------------------------
| HALAMAN AWAL — Redirect sesuai peran
|--------------------------------------------------------------------------
*/

Route::get('/', function () {
    if (auth()->check()) {
        return match (auth()->user()->role) {
            'admin' => redirect()->route('admin.dashboard'),
            'guru'  => redirect()->route('guru.scan'),
            default => redirect()->route('petugas.dashboard'),
        };
    }
    return redirect()->route('login');
});

/*
|--------------------------------------------------------------------------
| AUTENTIKASI
|--------------------------------------------------------------------------
*/
Route::middleware('guest')->group(function () {
    Route::get('/login',  [LoginController::class, 'index'])->name('login');
    Route::post('/login', [LoginController::class, 'authenticate'])->name('login.process');
});

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

/*
|--------------------------------------------------------------------------
| ADMIN
|--------------------------------------------------------------------------
*/
Route::middleware(['auth', 'admin'])
    ->prefix('admin')
    ->name('admin.')
    ->group(function () {

        // ── Dashboard ──────────────────────────────────────────────────
        Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

        // ── Scan QR ────────────────────────────────────────────────────
        Route::get('/scan', [AbsensiController::class, 'scan'])->name('scan');

        // ── Siswa ──────────────────────────────────────────────────────
        Route::prefix('siswa')->name('siswa.')->group(function () {
            Route::get('cetak-semua',        [SiswaController::class, 'cetakSemua'])->name('cetak_semua');
            Route::get('{id}/cetak',        [SiswaController::class, 'cetakQr'])->name('cetak');
            Route::post('import',           [SiswaController::class, 'import'])->name('import');
            Route::post('naik-kelas',       [SiswaController::class, 'naikKelas'])->name('naik_kelas');
            Route::delete('kosongkan-kelas', [SiswaController::class, 'kosongkanKelas'])->name('kosongkan_kelas');
        });
        Route::resource('siswa', SiswaController::class);

        // ── Guru ───────────────────────────────────────────────────────
        Route::prefix('guru')->name('guru.')->group(function () {
            Route::get('cetak',             [GuruController::class, 'cetak'])->name('cetak');
            Route::get('cetak/pdf',         [GuruController::class, 'cetakPdf'])->name('print.pdf');
            Route::post('import',           [GuruController::class, 'import'])->name('import');
            Route::delete('kosongkan',      [GuruController::class, 'kosongkan'])->name('kosongkan');
        });
        Route::resource('guru', GuruController::class);

        // ── Pengguna ───────────────────────────────────────────────────
        Route::resource('user', UserController::class);
        Route::get('/user/check-username', [UserController::class, 'checkUsername'])->name('user.check-username');

        // ── Laporan QR Code (Ibadah) ───────────────────────────────────
        Route::prefix('laporan')->name('laporan.')->group(function () {
            Route::get('harian',   [LaporanController::class, 'absensiHarian'])->name('harian');
            Route::get('bulanan',  [LaporanController::class, 'rekapBulanan'])->name('bulanan');
            Route::get('cetak',    [LaporanController::class, 'cetak'])->name('cetak');
            // ── TAMBAHAN: tandai siswa/guru sudah dievaluasi untuk
            //    minggu tertentu (dipanggil dari modal di halaman
            //    rekap bulanan). Namanya jadi admin.laporan.evaluasi
            //    karena berada di dalam grup prefix('laporan')->name('laporan.').
            Route::post('evaluasi', [LaporanController::class, 'tandaiEvaluasi'])->name('evaluasi');
        });

        // ── Hari Libur ─────────────────────────────────────────────────
        Route::prefix('hari-libur')->name('hari-libur.')->group(function () {
            Route::post('/',        [HariLiburController::class, 'store'])->name('store');
            Route::put('{id}',      [HariLiburController::class, 'update'])->name('update');
            Route::delete('{id}',   [HariLiburController::class, 'destroy'])->name('destroy');
        });
    });

/*
|--------------------------------------------------------------------------
| TAMBAHAN: ABSEN MANUAL KELILING KELAS (KHUSUS ADMIN)
|--------------------------------------------------------------------------
| Sengaja diletakkan di luar group prefix 'admin.' bawaan agar nama route-nya 
| ringkas tanpa tambahan 'admin.absen-manual.index' sehingga tidak merusak 
| komponen sidebar dan controller yang sebelumnya Anda rancang.
*/
Route::middleware(['auth', 'admin'])
    ->prefix('admin/absen-manual')
    ->name('absen-manual.')
    ->group(function () {
        // 1. Tampilan Input Absen Keliling
        Route::get('/', [AbsenManualController::class, 'index'])->name('index');

        // 2. Aksi Simpan Data Bolos/Absen ke Database
        Route::post('/store', [AbsenManualController::class, 'store'])->name('store');

        // 3. Tampilan Laporan Ketidakhadiran Khusus Kesiswaan
        Route::get('/laporan', [AbsenManualController::class, 'laporan'])->name('laporan');
    });

/*
|--------------------------------------------------------------------------
| GURU — Hanya akses scan presensi siswa
|--------------------------------------------------------------------------
*/
Route::middleware(['auth', 'admin_or_guru'])
    ->prefix('guru')
    ->name('guru.')
    ->group(function () {
        Route::get('/scan', [AbsensiController::class, 'scan'])->name('scan');
    });

/*
|--------------------------------------------------------------------------
| PETUGAS
|--------------------------------------------------------------------------
*/
Route::middleware(['auth', 'petugas'])
    ->prefix('petugas')
    ->name('petugas.')
    ->group(function () {

        // ── Dashboard & Scan ───────────────────────────────────────────
        Route::get('/dashboard', [PetugasController::class, 'dashboard'])->name('dashboard');
        Route::get('/scan',      [PetugasController::class, 'scan'])->name('scan');

        // ── Laporan ────────────────────────────────────────────────────
        Route::prefix('laporan')->name('laporan.')->group(function () {
            Route::get('harian',  [LaporanController::class, 'absensiHarian'])->name('harian');
            Route::get('bulanan', [LaporanController::class, 'rekapBulanan'])->name('bulanan');
        });

        // ── Pengaturan Akun ────────────────────────────────────────────
        Route::get('/password',          [PetugasController::class, 'password'])->name('user.password');
        Route::post('/password/update',  [PetugasController::class, 'updatePassword'])->name('user.password.update');
    });

/*
|--------------------------------------------------------------------------
| GLOBAL — Proses absensi (admin, guru, petugas)
|--------------------------------------------------------------------------
*/
Route::middleware(['auth', 'admin_or_guru'])
    ->post('/absen/proses', [AbsensiController::class, 'prosesAbsen'])
    ->name('absen.proses');

Route::middleware(['auth', 'admin_or_guru'])
    ->post('/absen/manual', [AbsensiController::class, 'inputManual'])
    ->name('absen.manual');

Route::middleware(['auth', 'admin_or_guru'])
    ->get('/absen/belum-hadir', [AbsensiController::class, 'daftarBelumHadir'])
    ->name('absen.belumHadir');
