<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\AdminController;
use App\Http\Controllers\ScanController;
use App\Http\Controllers\InputController;
use App\Http\Controllers\AbsensiMapelController;

// =========================================
// PORTAL (Halaman Utama = Dashboard/Menu)
// =========================================
Route::get('/', function () {
    return view('portal');
})->name('portal');

// =========================================
// LOGIN
// =========================================
Route::get('/login', [AuthController::class, 'login'])->name('login');
Route::post('/login', [AuthController::class, 'loginPost'])->name('login.post');
Route::post('/logout', [AuthController::class, 'logout'])->name('logout');

// =========================================
// DASHBOARDS (Setelah Login)
// =========================================
Route::middleware('auth')->group(function () {
    Route::get('/admin/dashboard', [DashboardController::class, 'admin'])->name('admin.dashboard');
    Route::get('/guru/dashboard', [DashboardController::class, 'guru'])->name('guru.dashboard');
    Route::get('/siswa/dashboard', [DashboardController::class, 'siswa'])->name('siswa.dashboard');

    // ====== ADMIN CRUD ======
    // Master Data - Admin
    Route::get('/admin/data-admin', [AdminController::class, 'admin'])->name('admin.data.admin');
    Route::post('/admin/data-admin', [AdminController::class, 'storeAdmin'])->name('admin.data.admin.store');
    Route::put('/admin/data-admin/{id}', [AdminController::class, 'updateAdmin'])->name('admin.data.admin.update');
    Route::delete('/admin/data-admin/{id}', [AdminController::class, 'destroyAdmin'])->name('admin.data.admin.destroy');

    // Master Data - Guru
    Route::get('/admin/data-guru', [AdminController::class, 'guru'])->name('admin.data.guru');
    Route::post('/admin/data-guru', [AdminController::class, 'storeGuru'])->name('admin.data.guru.store');
    Route::put('/admin/data-guru/{id}', [AdminController::class, 'updateGuru'])->name('admin.data.guru.update');
    Route::delete('/admin/data-guru/{id}', [AdminController::class, 'destroyGuru'])->name('admin.data.guru.destroy');

    // Master Data - Siswa
    Route::get('/admin/data-siswa', [AdminController::class, 'siswa'])->name('admin.data.siswa');
    Route::post('/admin/data-siswa', [AdminController::class, 'storeSiswa'])->name('admin.data.siswa.store');
    Route::put('/admin/data-siswa/{id}', [AdminController::class, 'updateSiswa'])->name('admin.data.siswa.update');
    Route::delete('/admin/data-siswa/{id}', [AdminController::class, 'destroySiswa'])->name('admin.data.siswa.destroy');

    // Master Data - Kelas
    Route::get('/admin/data-kelas', [AdminController::class, 'kelas'])->name('admin.data.kelas');
    Route::post('/admin/data-kelas', [AdminController::class, 'storeKelas'])->name('admin.data.kelas.store');
    Route::put('/admin/data-kelas/{id}', [AdminController::class, 'updateKelas'])->name('admin.data.kelas.update');
    Route::delete('/admin/data-kelas/{id}', [AdminController::class, 'destroyKelas'])->name('admin.data.kelas.destroy');

    // Master Data - Wali Kelas
    Route::get('/admin/data-wali-kelas', [AdminController::class, 'waliKelas'])->name('admin.data.wali_kelas');
    Route::post('/admin/data-wali-kelas', [AdminController::class, 'storeWaliKelas'])->name('admin.data.wali_kelas.store');
    Route::put('/admin/data-wali-kelas/{id}', [AdminController::class, 'updateWaliKelas'])->name('admin.data.wali_kelas.update');
    Route::delete('/admin/data-wali-kelas/{id}', [AdminController::class, 'destroyWaliKelas'])->name('admin.data.wali_kelas.destroy');

    // Master Data - Pelajaran
    Route::get('/admin/data-pelajaran', [AdminController::class, 'pelajaran'])->name('admin.data.pelajaran');
    Route::post('/admin/data-pelajaran', [AdminController::class, 'storePelajaran'])->name('admin.data.pelajaran.store');
    Route::put('/admin/data-pelajaran/{id}', [AdminController::class, 'updatePelajaran'])->name('admin.data.pelajaran.update');
    Route::delete('/admin/data-pelajaran/{id}', [AdminController::class, 'destroyPelajaran'])->name('admin.data.pelajaran.destroy');

    // Master Data - Jam Ngajar
    Route::get('/admin/data-jam-ngajar', [AdminController::class, 'jamNgajar'])->name('admin.data.jam_ngajar');
    Route::post('/admin/data-jam-ngajar', [AdminController::class, 'storeJamNgajar'])->name('admin.data.jam_ngajar.store');
    Route::put('/admin/data-jam-ngajar/{id}', [AdminController::class, 'updateJamNgajar'])->name('admin.data.jam_ngajar.update');
    Route::delete('/admin/data-jam-ngajar/{id}', [AdminController::class, 'destroyJamNgajar'])->name('admin.data.jam_ngajar.destroy');

    // Data Siswa - Prestasi
    Route::get('/admin/prestasi', [AdminController::class, 'prestasi'])->name('admin.prestasi');
    Route::post('/admin/prestasi', [AdminController::class, 'storePrestasi'])->name('admin.prestasi.store');
    Route::put('/admin/prestasi/{id}', [AdminController::class, 'updatePrestasi'])->name('admin.prestasi.update');
    Route::delete('/admin/prestasi/{id}', [AdminController::class, 'destroyPrestasi'])->name('admin.prestasi.destroy');

    // Data Siswa - Pelanggaran
    Route::get('/admin/pelanggaran', [AdminController::class, 'pelanggaran'])->name('admin.pelanggaran');
    Route::post('/admin/pelanggaran', [AdminController::class, 'storePelanggaran'])->name('admin.pelanggaran.store');
    Route::put('/admin/pelanggaran/{id}', [AdminController::class, 'updatePelanggaran'])->name('admin.pelanggaran.update');
    Route::delete('/admin/pelanggaran/{id}', [AdminController::class, 'destroyPelanggaran'])->name('admin.pelanggaran.destroy');

    // Reward
    Route::get('/admin/reward-absen', [AdminController::class, 'rewardAbsen'])->name('admin.reward.absen');
    Route::post('/admin/reward-absen', [AdminController::class, 'storeRewardAbsen'])->name('admin.reward.absen.store');
    Route::put('/admin/reward-absen/{id}', [AdminController::class, 'updateRewardAbsen'])->name('admin.reward.absen.update');
    Route::delete('/admin/reward-absen/{id}', [AdminController::class, 'destroyRewardAbsen'])->name('admin.reward.absen.destroy');

    Route::get('/admin/reward-prestasi', [AdminController::class, 'rewardPrestasi'])->name('admin.reward.prestasi');
    Route::post('/admin/reward-prestasi', [AdminController::class, 'storeRewardPrestasi'])->name('admin.reward.prestasi.store');
    Route::put('/admin/reward-prestasi/{id}', [AdminController::class, 'updateRewardPrestasi'])->name('admin.reward.prestasi.update');
    Route::delete('/admin/reward-prestasi/{id}', [AdminController::class, 'destroyRewardPrestasi'])->name('admin.reward.prestasi.destroy');

    // Absensi (read-only views)
    Route::get('/admin/kehadiran-guru', [AdminController::class, 'kehadiranGuru'])->name('admin.kehadiran_guru');
    Route::get('/admin/absen-guru', [AdminController::class, 'absenGuru'])->name('admin.absen_guru');
    Route::get('/admin/absen-jam-ngajar', [AdminController::class, 'absenJamNgajar'])->name('admin.absen_jam_ngajar');
    Route::get('/admin/absen-siswa', [AdminController::class, 'absenSiswa'])->name('admin.absen_siswa');

    // Export Global (Laporan)
    Route::get('/admin/laporan/export/{type}', [AdminController::class, 'exportLaporan'])->name('admin.laporan.export');

    // Laporan (read-only views)
    Route::get('/admin/laporan-absen-siswa', [AdminController::class, 'laporanAbsenSiswa'])->name('admin.laporan.absen_siswa');
    Route::get('/admin/laporan-absen-guru', [AdminController::class, 'laporanAbsenGuru'])->name('admin.laporan.absen_guru');
    Route::get('/admin/laporan-ngajar-guru', [AdminController::class, 'laporanNgajarGuru'])->name('admin.laporan.ngajar_guru');
    Route::get('/admin/laporan-pelanggaran', [AdminController::class, 'laporanPelanggaran'])->name('admin.laporan.pelanggaran');
    Route::get('/admin/laporan-prestasi', [AdminController::class, 'laporanPrestasi'])->name('admin.laporan.prestasi');
    Route::get('/admin/grafik-pelanggaran', [AdminController::class, 'grafikPelanggaran'])->name('admin.grafik.pelanggaran');
    Route::get('/admin/grafik-prestasi', [AdminController::class, 'grafikPrestasi'])->name('admin.grafik.prestasi');
    Route::get('/admin/laporan-absen-mapel', [AdminController::class, 'laporanAbsenMapel'])->name('admin.laporan.absen_mapel');

    // Profile & Settings
    Route::get('/admin/edit-profile', [AdminController::class, 'editProfile'])->name('admin.edit_profile');
    Route::post('/admin/edit-profile', [AdminController::class, 'updateProfile'])->name('admin.edit_profile.update');
    Route::get('/admin/settings', [AdminController::class, 'settings'])->name('admin.settings');
});

// =========================================
// SCAN QR Kehadiran (Public)
// =========================================
Route::get('/scan', [ScanController::class, 'index'])->name('scan.index');
Route::post('/scan/proses', [ScanController::class, 'prosesAbsen'])->name('scan.proses');
Route::get('/scan/generate-qr', [ScanController::class, 'generateQr'])->name('scan.generate_qr');
Route::post('/scan/download-qr', [ScanController::class, 'downloadQr'])->name('scan.download_qr');

// =========================================
// SCAN QR PULANG (Public)
// =========================================
Route::get('/scan-pulang', [ScanController::class, 'scanPulang'])->name('scan.pulang');
Route::post('/scan-pulang/proses', [ScanController::class, 'prosesPulang'])->name('scan.pulang.proses');

// =========================================
// ABSENSI MATA PELAJARAN (Public - sesuai sistem lama)
// =========================================
Route::get('/absensi-mapel', [AbsensiMapelController::class, 'index'])->name('absensi.mapel');
Route::post('/absensi-mapel/proses', [AbsensiMapelController::class, 'proses'])->name('absensi.mapel.proses');

// =========================================
// INPUT PRESTASI & PELANGGARAN (Public - sesuai sistem lama)
// =========================================
Route::get('/input-siswa-berprestasi', [InputController::class, 'prestasi'])->name('input.prestasi');
Route::post('/input-siswa-berprestasi', [InputController::class, 'simpanPrestasi'])->name('input.prestasi.simpan');
Route::get('/input-siswa-pelanggaran', [InputController::class, 'pelanggaran'])->name('input.pelanggaran');
Route::post('/input-siswa-pelanggaran', [InputController::class, 'simpanPelanggaran'])->name('input.pelanggaran.simpan');
