<?php

use App\Http\Controllers\ProfileController;
use App\Http\Controllers\SantriController;
use App\Http\Controllers\GuruController;
use App\Http\Controllers\KelasController;
use App\Http\Controllers\SemesterController;
use App\Http\Controllers\MataPelajaranController;
use App\Http\Controllers\JadwalController;
use App\Http\Controllers\AbsensiController;
use App\Http\Controllers\NilaiSantriController;
use App\Http\Controllers\PrestasiController;
use App\Http\Controllers\CatatanKesehatanController;
use App\Http\Controllers\BeritaController;
use App\Http\Controllers\PembayaranController;
use App\Http\Controllers\AlumniController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\PelanggaranController;
use Illuminate\Support\Facades\Route;


Route::get('/', function () {
    if (auth()->check()) {
        return redirect()->route('dashboard');
    }
    return redirect()->route('login');
});

Route::get('/dashboard', function () {
    $user = auth()->user();
    if ($user->isAdmin()) {
        return redirect()->route('admin.dashboard');
    } elseif ($user->isGuru()) {
        return redirect()->route('guru.dashboard');
    }
    return '/';
})->middleware(['auth', 'verified'])->name('dashboard');

Route::get('/admin/dashboard', function () {
    return view('admin.dashboard');
})->middleware(['auth', 'verified'])->name('admin.dashboard');

Route::get('/guru/dashboard', function () {
    return view('guru.dashboard');
})->middleware(['auth', 'verified'])->name('guru.dashboard');

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');
    Route::resource('santris', SantriController::class);
    Route::resource('gurus', GuruController::class);
    Route::resource('kelas', KelasController::class);
    Route::resource('semesters', SemesterController::class);
    Route::patch('semesters/{semester}/activate', [SemesterController::class, 'activate'])->name('semesters.activate');
    Route::resource('mata_pelajarans', MataPelajaranController::class);
    Route::resource('jadwals', JadwalController::class);
    Route::resource('absensis', AbsensiController::class)->except(['show']);
    Route::get('absensis/santri/{santri_id}', [AbsensiController::class, 'show'])->name('absensis.show');
    Route::resource('nilai_santris', NilaiSantriController::class);
    Route::resource('prestasis', PrestasiController::class);
    Route::resource('pelanggarans', PelanggaranController::class);
    Route::resource('catatan_kesehatans', CatatanKesehatanController::class);
    Route::resource('beritas', BeritaController::class);
    Route::resource('pembayarans', PembayaranController::class);
    Route::resource('alumnis', AlumniController::class);
    Route::resource('users', UserController::class);
});

Route::get('santris/import', [SantriController::class, 'import'])->name('santris.import'); // GET untuk menampilkan form
Route::post('santris/import', [SantriController::class, 'importStore'])->name('santris.import.store');

Route::middleware(['auth', 'verified'])->group(function () {
    Route::get('/guru/profile', [GuruController::class, 'profile'])->name('guru.profile');
    Route::get('/guru/profile/edit', [GuruController::class, 'editProfile'])->name('guru.profile.edit');
    Route::post('/guru/profile/update', [GuruController::class, 'updateProfile'])->name('guru.profile.update');
});

Route::middleware(['auth', 'verified'])->prefix('guru')->name('guru.')->group(function () {
    Route::resource('santris', \App\Http\Controllers\SantriController::class)->only(['index']);
    Route::resource('jadwals', \App\Http\Controllers\JadwalController::class)->only(['index']);
    Route::resource('nilai_santris', \App\Http\Controllers\NilaiSantriController::class)->except(['show']);
    Route::resource('prestasis', \App\Http\Controllers\PrestasiController::class)->except(['show']);
    Route::resource('pelanggarans', \App\Http\Controllers\PelanggaranController::class)->except(['show']);
    Route::resource('catatan_kesehatans', \App\Http\Controllers\CatatanKesehatanController::class)->except(['show']);
    Route::resource('absensis', \App\Http\Controllers\AbsensiController::class)->except(['show']);
});

require __DIR__ . '/auth.php';
