<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Auth\AdminAuthController;
use App\Http\Controllers\Auth\PetugasAuthController;
use App\Http\Controllers\Auth\AlumniAuthController;
use App\Http\Controllers\Dashboard\AdminDashboardController;
use App\Http\Controllers\Dashboard\PetugasDashboardController;
use Illuminate\Support\Facades\Auth;

// Main landing page
Route::get('/', function () {
    $pengumumanList = \App\Models\Pengumuman::orderBy('tanggal', 'desc')->take(3)->get();
    $galeriList = \App\Models\Galeri::orderBy('tanggal_kegiatan', 'desc')->get();
    $guruList = \App\Models\Guru::all();
    return view('welcome', compact('pengumumanList', 'galeriList', 'guruList'));
});

// Disable standard /login route to prevent guessing
Route::any('/login', function () {
    abort(404);
});

// --- ADMIN ROUTES ---
Route::middleware('guest.admin')->group(function () {
    Route::get('/admin', [AdminAuthController::class, 'showLoginForm'])->name('admin.login');
    Route::post('/admin/login', [AdminAuthController::class, 'login'])->name('admin.login.submit');
});

Route::middleware('auth.admin')->group(function () {
    Route::post('/admin/logout', [AdminAuthController::class, 'logout'])->name('admin.logout');
    Route::get('/admin/dashboard', [AdminDashboardController::class, 'index'])->name('admin.dashboard');
    Route::post('/admin/dashboard/clear-logs', [AdminDashboardController::class, 'clearLogs'])->name('admin.dashboard.clear-logs');

    // Admin CRUD (Admin can add new Admin)
    Route::post('/admin/register', [AdminAuthController::class, 'register'])->name('admin.register.submit');
    Route::put('/admin/dashboard/admin/{id}', [AdminDashboardController::class, 'updateAdmin'])->name('admin.dashboard.admin.update');
    Route::delete('/admin/dashboard/admin/{id}', [AdminDashboardController::class, 'destroyAdmin'])->name('admin.dashboard.admin.destroy');

    // Petugas CRUD
    Route::post('/admin/dashboard/petugas', [AdminDashboardController::class, 'storePetugas'])->name('admin.dashboard.petugas.store');
    Route::put('/admin/dashboard/petugas/{id}', [AdminDashboardController::class, 'updatePetugas'])->name('admin.dashboard.petugas.update');
    Route::delete('/admin/dashboard/petugas/{id}', [AdminDashboardController::class, 'destroyPetugas'])->name('admin.dashboard.petugas.destroy');

    // Alumni CRUD (only Admin can add/register alumni now)
    Route::post('/admin/dashboard/alumni', [AdminDashboardController::class, 'storeAlumni'])->name('admin.dashboard.alumni.store');
    Route::put('/admin/dashboard/alumni/{id}', [AdminDashboardController::class, 'updateAlumni'])->name('admin.dashboard.alumni.update');
    Route::delete('/admin/dashboard/alumni/{id}', [AdminDashboardController::class, 'destroyAlumni'])->name('admin.dashboard.alumni.destroy');

    // Siswa CRUD (only Admin can add siswa now)
    Route::post('/admin/dashboard/siswa', [AdminDashboardController::class, 'storeSiswa'])->name('admin.dashboard.siswa.store');
    Route::put('/admin/dashboard/siswa/{nis}', [AdminDashboardController::class, 'updateSiswa'])->name('admin.dashboard.siswa.update');
    Route::delete('/admin/dashboard/siswa/{nis}', [AdminDashboardController::class, 'destroySiswa'])->name('admin.dashboard.siswa.destroy');

    // Guru CRUD
    Route::post('/admin/dashboard/guru', [AdminDashboardController::class, 'storeGuru'])->name('admin.dashboard.guru.store');
    Route::put('/admin/dashboard/guru/{nip}', [AdminDashboardController::class, 'updateGuru'])->name('admin.dashboard.guru.update');
    Route::delete('/admin/dashboard/guru/{nip}', [AdminDashboardController::class, 'destroyGuru'])->name('admin.dashboard.guru.destroy');

    // Profile Edit
    Route::put('/admin/profile', [AdminDashboardController::class, 'updateProfile'])->name('admin.profile.update');
});

// --- PETUGAS ROUTES ---
Route::middleware('guest.petugas')->group(function () {
    Route::get('/petugas', [PetugasAuthController::class, 'showLoginForm'])->name('petugas.login');
    Route::post('/petugas/login', [PetugasAuthController::class, 'login'])->name('petugas.login.submit');
});

Route::middleware('auth.petugas')->group(function () {
    Route::post('/petugas/logout', [PetugasAuthController::class, 'logout'])->name('petugas.logout');
    Route::get('/petugas/dashboard', [PetugasDashboardController::class, 'index'])->name('petugas.dashboard');

    // Petugas can still update/delete Siswa & Alumni, but CANNOT store Siswa (only Admin does that)
    Route::put('/petugas/dashboard/siswa/{nis}', [PetugasDashboardController::class, 'updateSiswa'])->name('petugas.dashboard.siswa.update');
    Route::delete('/petugas/dashboard/siswa/{nis}', [PetugasDashboardController::class, 'destroySiswa'])->name('petugas.dashboard.siswa.destroy');
    Route::put('/petugas/dashboard/alumni/{id}', [PetugasDashboardController::class, 'updateAlumni'])->name('petugas.dashboard.alumni.update');
    Route::delete('/petugas/dashboard/alumni/{id}', [PetugasDashboardController::class, 'destroyAlumni'])->name('petugas.dashboard.alumni.destroy');

    // Pengumuman CRUD
    Route::post('/petugas/dashboard/pengumuman', [PetugasDashboardController::class, 'storePengumuman'])->name('petugas.dashboard.pengumuman.store');
    Route::put('/petugas/dashboard/pengumuman/{id}', [PetugasDashboardController::class, 'updatePengumuman'])->name('petugas.dashboard.pengumuman.update');
    Route::delete('/petugas/dashboard/pengumuman/{id}', [PetugasDashboardController::class, 'destroyPengumuman'])->name('petugas.dashboard.pengumuman.destroy');

    // Galeri CRUD
    Route::post('/petugas/dashboard/galeri', [PetugasDashboardController::class, 'storeGaleri'])->name('petugas.dashboard.galeri.store');
    Route::put('/petugas/dashboard/galeri/{id}', [PetugasDashboardController::class, 'updateGaleri'])->name('petugas.dashboard.galeri.update');
    Route::delete('/petugas/dashboard/galeri/{id}', [PetugasDashboardController::class, 'destroyGaleri'])->name('petugas.dashboard.galeri.destroy');

    // Guru CRUD
    Route::post('/petugas/dashboard/guru', [PetugasDashboardController::class, 'storeGuru'])->name('petugas.dashboard.guru.store');
    Route::put('/petugas/dashboard/guru/{nip}', [PetugasDashboardController::class, 'updateGuru'])->name('petugas.dashboard.guru.update');
    Route::delete('/petugas/dashboard/guru/{nip}', [PetugasDashboardController::class, 'destroyGuru'])->name('petugas.dashboard.guru.destroy');
    Route::post('/petugas/dashboard/guru/bulk-delete', [PetugasDashboardController::class, 'bulkDestroyGuru'])->name('petugas.dashboard.guru.bulk.destroy');
    Route::get('/petugas/dashboard/guru/bulk-delete', function () {
        return redirect()->route('petugas.dashboard');
    });
    // Guru import/export (JSON)
    Route::get('/petugas/dashboard/guru/export', [PetugasDashboardController::class, 'exportGuru'])->name('petugas.dashboard.guru.export');
    Route::post('/petugas/dashboard/guru/import', [PetugasDashboardController::class, 'importGuru'])->name('petugas.dashboard.guru.import');

    // Profil Jurusan (Konten) Update
    Route::put('/petugas/dashboard/konten', [PetugasDashboardController::class, 'updateKonten'])->name('petugas.dashboard.konten.update');

    // Profile Edit
    Route::put('/petugas/profile', [PetugasDashboardController::class, 'updateProfile'])->name('petugas.profile.update');
});

// --- ALUMNI ROUTES ---
Route::middleware('guest.alumni')->group(function () {
    Route::get('/alumni', [AlumniAuthController::class, 'showLoginForm'])->name('alumni.login');
    Route::post('/alumni/login', [AlumniAuthController::class, 'login'])->name('alumni.login.submit');
});

Route::middleware('auth.alumni')->group(function () {
    Route::post('/alumni/logout', [AlumniAuthController::class, 'logout'])->name('alumni.logout');
    Route::get('/alumni/dashboard', function () {
        return view('dashboard.placeholder', [
            'role' => 'Alumni',
            'name' => Auth::guard('alumni')->user()->nama,
            'logoutRoute' => 'alumni.logout'
        ]);
    })->name('alumni.dashboard');

    // Profile Edit
    Route::put('/alumni/profile', [AlumniAuthController::class, 'updateProfile'])->name('alumni.profile.update');
});


