<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\{
    AuthController, 
    AdminDashboardController,
    SiswaMagangController,
    AttendanceController,
    HolidayController,
    AutomationController,
    SiswaController, 
    SekolahController, 
    UserController,
    RFIDController
};

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

Route::middleware(['auth'])->group(function () {
    Route::middleware(['admin'])->prefix('admin')->group(function () {
        Route::get('/dashboard', [AdminDashboardController::class, 'dashboard'])->name('admin.dashboard');

        Route::prefix('sekolah')->group(function () {
            Route::get('/', [SekolahController::class, 'index'])->name('admin.sekolah');
            Route::post('/create', [SekolahController::class, 'store'])->name('admin.sekolah.store');
            Route::get('/{id}/edit', [SekolahController::class, 'edit'])->name('admin.sekolah.edit');
            Route::put('/{id}/update', [SekolahController::class, 'update'])->name('admin.sekolah.update');
            Route::delete('/{id}/delete', [SekolahController::class, 'destroy'])->name('admin.sekolah.delete');
        });

        Route::prefix('users')->group(function () {
            Route::get('/', [UserController::class, 'index'])->name('admin.users');
            Route::post('/create', [UserController::class, 'store'])->name('admin.users.store');
            Route::get('/{id}/edit', [UserController::class, 'edit'])->name('admin.users.edit');
            Route::put('/{id}/update', [UserController::class, 'update'])->name('admin.users.update');
            Route::delete('/{id}/delete', [UserController::class, 'destroy'])->name('admin.users.delete');
        });

        Route::prefix('siswa-magang')->group(function () {
            Route::get('/', [SiswaMagangController::class, 'index'])->name('admin.siswa');
            Route::post('/create', [SiswaMagangController::class, 'store'])->name('admin.create.siswa');
            Route::get('/{id}/edit', [SiswaMagangController::class, 'edit'])->name('admin.edit.siswa');
            Route::put('/{id}/update', [SiswaMagangController::class, 'update'])->name('admin.update.siswa');
            Route::delete('/{id}/delete', [SiswaMagangController::class, 'destroy'])->name('admin.delete.siswa');
            Route::get('/{id}/detail', [SiswaMagangController::class, 'show'])->name('admin.detail.siswa');
            Route::get('/{id}/export-pdf', [SiswaMagangController::class, 'exportPdf'])->name('admin.export.pdf');
        });

        Route::get('/dudi', [App\Http\Controllers\DudiController::class, 'index'])->name('dudi');
        Route::post('/dudi', [App\Http\Controllers\DudiController::class, 'store'])->name('dudi.store');
        Route::put('/dudi/{id}', [App\Http\Controllers\DudiController::class, 'update'])->name('dudi.update');
        Route::delete('/dudi/{id}', [App\Http\Controllers\DudiController::class, 'destroy'])->name('dudi.destroy');
        
        // Pembimbing Magang
        Route::get('/pembimbing-magang', [App\Http\Controllers\PembimbingMagangController::class, 'index'])->name('pembimbing-magang');
        Route::post('/pembimbing-magang', [App\Http\Controllers\PembimbingMagangController::class, 'store'])->name('pembimbing-magang.store');
        Route::put('/pembimbing-magang/{id}', [App\Http\Controllers\PembimbingMagangController::class, 'update'])->name('pembimbing-magang.update');
        Route::delete('/pembimbing-magang/{id}', [App\Http\Controllers\PembimbingMagangController::class, 'destroy'])->name('pembimbing-magang.destroy');
        
        // Pembimbing Sekolah
        Route::get('/pembimbing-sekolah', [App\Http\Controllers\PembimbingSekolahController::class, 'index'])->name('pembimbing-sekolah');
        Route::post('/pembimbing-sekolah', [App\Http\Controllers\PembimbingSekolahController::class, 'store'])->name('pembimbing-sekolah.store');
        Route::put('/pembimbing-sekolah/{id}', [App\Http\Controllers\PembimbingSekolahController::class, 'update'])->name('pembimbing-sekolah.update');
        Route::delete('/pembimbing-sekolah/{id}', [App\Http\Controllers\PembimbingSekolahController::class, 'destroy'])->name('pembimbing-sekolah.destroy');
            
        Route::get('/rekap', [AttendanceController::class, 'index'])->name('admin.rekap');
        Route::get('/attendance/{id}/edit', [AttendanceController::class, 'edit'])->name('admin.edit.attendance');
        Route::put('/attendance/{id}/update', [AttendanceController::class, 'update'])->name('admin.update.attendance');
        Route::delete('/attendance/{id}/delete', [AttendanceController::class, 'destroy'])->name('admin.delete.attendance');
        
        Route::get('/libur', [HolidayController::class, 'index'])->name('admin.holidays');
        Route::post('/create-holiday', [HolidayController::class, 'store'])->name('admin.create.holiday');
        Route::put('/holiday/{id}/update', [HolidayController::class, 'update'])->name('admin.update.holiday');
        Route::delete('/holiday/{id}/delete', [HolidayController::class, 'destroy'])->name('admin.delete.holiday');
        
        // RFID Routes - Add these
        Route::get('/rfid/masuk', [RFIDController::class, 'rfidMasuk'])->name('admin.rfid.masuk');
        Route::get('/rfid/pulang', [RFIDController::class, 'rfidPulang'])->name('admin.rfid.pulang');
        Route::post('/rfid/process-masuk', [RFIDController::class, 'processMasuk'])->name('admin.rfid.process.masuk');
        Route::post('/rfid/process-pulang', [RFIDController::class, 'processPulang'])->name('admin.rfid.process.pulang');
        
        Route::post('/auto-alfa', [AutomationController::class, 'runAutoAlfa'])->name('admin.auto-alfa');
    });

    Route::middleware(['siswa'])->prefix('siswa')->group(function () {
        Route::get('/dashboard', [SiswaController::class, 'dashboard'])->name('siswa.dashboard');
        Route::get('/presensi-harian', [SiswaController::class, 'presensiHarian'])->name('siswa.presensi.harian');
        Route::get('/riwayat-kehadiran', [SiswaController::class, 'riwayatKehadiran'])->name('siswa.riwayat');
        Route::post('/presensi', [SiswaController::class, 'presensi'])->name('siswa.presensi');

        Route::get('/jurnal', [App\Http\Controllers\JurnalController::class, 'indexSiswa'])->name('jurnal');
        Route::get('/jurnal/create', [App\Http\Controllers\JurnalController::class, 'createSiswa'])->name('jurnal.create');
        Route::post('/jurnal', [App\Http\Controllers\JurnalController::class, 'storeSiswa'])->name('jurnal.store');
        Route::get('/jurnal/{id}/detail', [App\Http\Controllers\JurnalController::class, 'detailSiswa'])->name('jurnal.detail');
        Route::get('/jurnal/{id}/edit', [App\Http\Controllers\JurnalController::class, 'editSiswa'])->name('jurnal.edit');
        Route::put('/jurnal/{id}', [App\Http\Controllers\JurnalController::class, 'updateSiswa'])->name('jurnal.update');
        Route::delete('/jurnal/{id}', [App\Http\Controllers\JurnalController::class, 'destroySiswa'])->name('jurnal.destroy');
    });

    // Routes untuk Pembimbing Magang
    Route::middleware(['auth'])->prefix('pembimbing-magang')->name('pembimbing-magang.')->group(function () {
        Route::get('/dashboard', [App\Http\Controllers\PembimbingMagangDashboardController::class, 'dashboard'])->name('dashboard');
        Route::get('/siswa', [App\Http\Controllers\PembimbingMagangDashboardController::class, 'siswaList'])->name('siswa');
        
        // Jurnal
        Route::get('/jurnal', [App\Http\Controllers\JurnalController::class, 'indexPembimbingMagang'])->name('jurnal');
        Route::get('/jurnal/{id}', [App\Http\Controllers\JurnalController::class, 'showPembimbingMagang'])->name('jurnal.show');
        Route::post('/jurnal/{id}/verifikasi', [App\Http\Controllers\JurnalController::class, 'verifikasiJurnal'])->name('jurnal.verifikasi');
    });

    // Routes untuk Pembimbing Sekolah
    Route::middleware(['auth'])->prefix('pembimbing-sekolah')->name('pembimbing-sekolah.')->group(function () {
        Route::get('/dashboard', [App\Http\Controllers\PembimbingSekolahDashboardController::class, 'dashboard'])->name('dashboard');
        Route::get('/siswa', [App\Http\Controllers\PembimbingSekolahDashboardController::class, 'siswaList'])->name('siswa');
        Route::get('/jurnal', [App\Http\Controllers\JurnalController::class, 'indexPembimbingSekolah'])->name('jurnal');
        Route::get('/jurnal/{id}', [App\Http\Controllers\JurnalController::class, 'showPembimbingSekolah'])->name('jurnal.show');
    });
});