<?php

use Illuminate\Support\Facades\Route;

// Ajax endpoint to get mata pelajaran by kelas for export modal
Route::get('admin/penilaian/mapel-by-kelas/{kelas}', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'mapelByKelas']);
use App\Http\Controllers\AdminController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\UserController;

Route::get('/', function () {
    return view('login');
});

// Web login (session-based) for admin area
Route::get('/login', function () {
    return view('login');
})->name('login');

Route::post('/login', [AuthController::class, 'loginWeb'])->name('login.attempt');
Route::post('/logout', [AuthController::class, 'logoutWeb'])->middleware('auth')->name('logout');

Route::middleware(['auth', \App\Http\Middleware\IsAdmin::class])->get('/admin', [AdminController::class, 'index']);

use App\Models\User;

Route::middleware(['auth', \App\Http\Middleware\IsAdmin::class])->get('/admin/users', function () {
    $users = User::all();
    return view('users.index', compact('users'));
});
 
Route::middleware(['auth', \App\Http\Middleware\IsAdmin::class])->group(function () {
    Route::resource('admin/users', UserController::class);
});

// Guru dashboard route
Route::get('/guru/dashboard', function () {
    return view('guru.dashboard');
})->middleware(['auth', \App\Http\Middleware\IsGuru::class]);

Route::get('/guru/users', [UserController::class, 'index'])->middleware(['auth', \App\Http\Middleware\IsGuru::class]);

// Data Umum (admin only)
Route::middleware(['auth', \App\Http\Middleware\IsAdmin::class])->prefix('admin/data-umum')->group(function () {
    Route::get('periode', [\App\Http\Controllers\Admin\DataUmum\DataUmumController::class, 'periode']);
    Route::post('periode', [\App\Http\Controllers\Admin\DataUmum\PeriodeController::class, 'store']);
    Route::put('periode/{id}', [\App\Http\Controllers\Admin\DataUmum\PeriodeController::class, 'update']);
    Route::delete('periode/{id}', [\App\Http\Controllers\Admin\DataUmum\PeriodeController::class, 'destroy']);
    Route::get('sekolah', [\App\Http\Controllers\Admin\DataUmum\DataUmumController::class, 'sekolah']);
    Route::post('sekolah', [\App\Http\Controllers\Admin\DataUmum\SekolahController::class, 'store']);
    Route::put('sekolah/{id}', [\App\Http\Controllers\Admin\DataUmum\SekolahController::class, 'update']);
    Route::delete('sekolah/{id}', [\App\Http\Controllers\Admin\DataUmum\SekolahController::class, 'destroy']);
    Route::post('kelas', [\App\Http\Controllers\Admin\DataUmum\KelasController::class, 'store']);
    Route::put('kelas/{id}', [\App\Http\Controllers\Admin\DataUmum\KelasController::class, 'update']);
    Route::delete('kelas/{id}', [\App\Http\Controllers\Admin\DataUmum\KelasController::class, 'destroy']);
    Route::get('kelas', [\App\Http\Controllers\Admin\DataUmum\DataUmumController::class, 'kelas']);
    Route::get('guru', [\App\Http\Controllers\Admin\DataUmum\DataUmumController::class, 'guru']);
    Route::put('guru/{id}', [\App\Http\Controllers\Admin\DataUmum\GuruController::class, 'update']);
    Route::get('peserta-didik', [\App\Http\Controllers\Admin\DataUmum\DataUmumController::class, 'pesertaDidik']);
    Route::post('peserta-didik', [\App\Http\Controllers\Admin\DataUmum\DataUmumController::class, 'storePesertaDidik']);
    Route::get('peserta-didik/template', [\App\Http\Controllers\Admin\DataUmum\DataUmumController::class, 'templatePesertaDidik']);
    Route::get('peserta-didik/export', [\App\Http\Controllers\Admin\DataUmum\DataUmumController::class, 'exportPesertaDidik']);
    Route::post('peserta-didik/import', [\App\Http\Controllers\Admin\DataUmum\DataUmumController::class, 'importPesertaDidik']);
    Route::put('peserta-didik/{id}', [\App\Http\Controllers\Admin\DataUmum\DataUmumController::class, 'updatePesertaDidik']);
    Route::delete('peserta-didik/{id}', [\App\Http\Controllers\Admin\DataUmum\DataUmumController::class, 'destroyPesertaDidik']);
    Route::post('kelas/add-peserta', [\App\Http\Controllers\Admin\DataUmum\DataUmumController::class, 'addPesertaToKelas']);
    Route::get('mata-pelajaran', [\App\Http\Controllers\Admin\DataUmum\DataUmumController::class, 'mataPelajaran']);
    Route::get('mata-pelajaran/{id}/json', [\App\Http\Controllers\Admin\DataUmum\DataUmumController::class, 'mataPelajaranJson']);
    Route::get('mata-pelajaran/by-kelas/{kelas}/json', [\App\Http\Controllers\Admin\DataUmum\DataUmumController::class, 'mataPelajaranByKelasJson']);
    Route::post('mata-pelajaran', [\App\Http\Controllers\Admin\DataUmum\DataUmumController::class, 'storeMataPelajaran'])->name('admin.mata-pelajaran.store');
    Route::get('mata-pelajaran/{id}/edit', [\App\Http\Controllers\Admin\DataUmum\DataUmumController::class, 'editMataPelajaran'])->name('admin.mata-pelajaran.edit');
    Route::put('mata-pelajaran/{id}', [\App\Http\Controllers\Admin\DataUmum\DataUmumController::class, 'updateMataPelajaran'])->name('admin.mata-pelajaran.update');
    Route::delete('mata-pelajaran/{id}', [\App\Http\Controllers\Admin\DataUmum\DataUmumController::class, 'destroyMataPelajaran'])->name('admin.mata-pelajaran.destroy');
    // reorder mata pelajaran (AJAX) - accepts JSON { order: [id1, id2, ...] }
    Route::post('mata-pelajaran/reorder', [\App\Http\Controllers\Admin\MataPelajaranController::class, 'reorder'])->name('admin.mata-pelajaran.reorder');
    Route::get('demensi-kokurikuler', [\App\Http\Controllers\Admin\DataUmum\DataUmumController::class, 'demensiKokurikuler']);
    Route::post('demensi-kokurikuler', [\App\Http\Controllers\Admin\DataUmum\DataUmumController::class, 'storeDemensiKokurikuler'])->name('admin.data-umum.demensi-kokurikuler.store');
    Route::get('demensi-kokurikuler/{id}/edit', [\App\Http\Controllers\Admin\DataUmum\DataUmumController::class, 'editDemensiKokurikuler'])->name('admin.data-umum.demensi-kokurikuler.edit');
    Route::put('demensi-kokurikuler/{id}', [\App\Http\Controllers\Admin\DataUmum\DataUmumController::class, 'updateDemensiKokurikuler'])->name('admin.data-umum.demensi-kokurikuler.update');
    Route::delete('demensi-kokurikuler/{id}', [\App\Http\Controllers\Admin\DataUmum\DataUmumController::class, 'destroyDemensiKokurikuler'])->name('admin.data-umum.demensi-kokurikuler.destroy');
    Route::resource('ekstrakulikuler', \App\Http\Controllers\Admin\DataUmum\EkstrakulikulerController::class)->except(['show']);
});

// Guru access to peserta-didik (read-only, filtered to their classes)
Route::middleware(['auth', \App\Http\Middleware\IsGuru::class])->get('guru/data-umum/peserta-didik', [\App\Http\Controllers\Admin\DataUmum\DataUmumController::class, 'pesertaDidik'])->name('guru.peserta-didik');

// Allow guru role to view mata-pelajaran (read-only/create modal will auto-select kelas mereka)
Route::middleware(['auth', \App\Http\Middleware\IsGuru::class])->get('guru/data-umum/mata-pelajaran', [\App\Http\Controllers\Admin\DataUmum\DataUmumController::class, 'mataPelajaran'])->name('guru.mata-pelajaran');
// JSON endpoint for guru to fetch mata pelajaran details
Route::middleware(['auth', \App\Http\Middleware\IsGuru::class])->get('guru/data-umum/mata-pelajaran/{id}/json', [\App\Http\Controllers\Admin\DataUmum\DataUmumController::class, 'mataPelajaranJson']);
Route::middleware(['auth', \App\Http\Middleware\IsGuru::class])->get('guru/data-umum/mata-pelajaran/by-kelas/{kelas}/json', [\App\Http\Controllers\Admin\DataUmum\DataUmumController::class, 'mataPelajaranByKelasJson']);

// Guru access to penilaian (kokurikuler, intrakulikuler, ekstrakulikuler) - read-only filtered to their kelas
Route::middleware(['auth', \App\Http\Middleware\IsGuru::class])->prefix('guru/penilaian')->group(function(){
    Route::get('kokurikuler', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'kokurikuler'])->name('guru.penilaian.kokurikuler');
    Route::get('kokurikuler/hasil', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'hasil'])->name('guru.penilaian.kokurikuler.hasil');
    Route::get('intrakulikuler', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'intrakulikuler'])->name('guru.penilaian.intrakulikuler');
    Route::get('intrakulikuler/template', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'downloadIntrakulikulerTemplate'])->name('guru.penilaian.intrakulikuler.template');
    Route::get('intrakulikuler/template-xlsx', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'downloadIntrakulikulerTemplateXlsx'])->name('guru.penilaian.intrakulikuler.template.xlsx');
    Route::get('intrakulikuler/export', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'exportIntrakulikuler'])->name('guru.penilaian.intrakulikuler.export');
    Route::get('intrakulikuler/export-xlsx', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'exportIntrakulikulerXlsx'])->name('guru.penilaian.intrakulikuler.export.xlsx');
    Route::post('intrakulikuler/import', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'importIntrakulikuler'])->name('guru.penilaian.intrakulikuler.import');
    Route::post('intrakulikuler/import-xlsx', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'importIntrakulikulerXlsx'])->name('guru.penilaian.intrakulikuler.import.xlsx');
    
    Route::get('intrakulikuler/{id}/edit', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'editIntrakulikuler'])->name('guru.penilaian.intrakulikuler.edit');
    Route::get('ekstrakulikuler', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'ekstrakulikuler'])->name('guru.penilaian.ekstrakulikuler');
    // AJAX endpoints reused by admin views — expose them for guru prefix as well
    Route::get('peserta-kelas/{kelas}', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'pesertaByKelas']);
    Route::get('demensi/{demensi}/sub-demensi', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'subDemensiByDemensi']);
    Route::get('demensi/{demensi}/deskripsi', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'deskripsiByDemensi']);
    // edit/update/destroy routes for kokurikuler (guru)
    Route::get('kokurikuler/{id}/edit', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'editKokurikuler'])->name('guru.penilaian.kokurikuler.edit');
    Route::put('kokurikuler/{id}', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'updateKokurikuler'])->name('guru.penilaian.kokurikuler.update');
    Route::delete('kokurikuler/{id}', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'destroyKokurikuler'])->name('guru.penilaian.kokurikuler.destroy');
    // write actions for guru (allow full access within their classes)
    Route::post('kokurikuler', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'store'])->name('guru.penilaian.kokurikuler.store');
    Route::post('intrakulikuler', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'storeIntrakulikuler'])->name('guru.penilaian.intrakulikuler.store');
    Route::put('intrakulikuler/{id}', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'updateIntrakulikuler'])->name('guru.penilaian.intrakulikuler.update');
    Route::delete('intrakulikuler/{id}', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'destroyIntrakulikuler'])->name('guru.penilaian.intrakulikuler.destroy');
    Route::post('ekstrakulikuler', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'storeEkstrakulikuler'])->name('guru.penilaian.ekstrakulikuler.store');
    Route::put('ekstrakulikuler/{id}', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'updateEkstrakulikuler'])->name('guru.penilaian.ekstrakulikuler.update');
    Route::delete('ekstrakulikuler/{id}', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'destroyEkstrakulikuler'])->name('guru.penilaian.ekstrakulikuler.destroy');
});

// Penilaian (admin)
Route::middleware(['auth', \App\Http\Middleware\IsAdmin::class])->prefix('admin/penilaian')->group(function () {
    Route::get('kokurikuler', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'kokurikuler']);
    Route::get('kokurikuler/{id}/edit', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'editKokurikuler'])->name('admin.penilaian.kokurikuler.edit');
    Route::put('kokurikuler/{id}', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'updateKokurikuler'])->name('admin.penilaian.kokurikuler.update');
    Route::delete('kokurikuler/{id}', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'destroyKokurikuler'])->name('admin.penilaian.kokurikuler.destroy');
    Route::get('kokurikuler/hasil', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'hasil']);
    Route::post('kokurikuler', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'store'])->name('admin.penilaian.kokurikuler.store');
    Route::get('peserta-kelas/{kelas}', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'pesertaByKelas']);
    Route::get('demensi/{demensi}/sub-demensi', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'subDemensiByDemensi']);
    Route::get('demensi/{demensi}/deskripsi', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'deskripsiByDemensi']);
    Route::get('intrakulikuler', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'intrakulikuler'])->name('admin.penilaian.intrakulikuler');
    Route::get('intrakulikuler/template', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'downloadIntrakulikulerTemplate'])->name('admin.penilaian.intrakulikuler.template');
    Route::get('intrakulikuler/template-xlsx', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'downloadIntrakulikulerTemplateXlsx'])->name('admin.penilaian.intrakulikuler.template.xlsx');
    Route::get('intrakulikuler/export', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'exportIntrakulikuler'])->name('admin.penilaian.intrakulikuler.export');
    Route::get('intrakulikuler/export-xlsx', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'exportIntrakulikulerXlsx'])->name('admin.penilaian.intrakulikuler.export.xlsx');
    Route::post('intrakulikuler/import', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'importIntrakulikuler'])->name('admin.penilaian.intrakulikuler.import');
    Route::post('intrakulikuler/import-xlsx', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'importIntrakulikulerXlsx'])->name('admin.penilaian.intrakulikuler.import.xlsx');
    
    Route::post('intrakulikuler', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'storeIntrakulikuler'])->name('admin.penilaian.intrakulikuler.store');
    Route::get('intrakulikuler/{id}/edit', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'editIntrakulikuler'])->name('admin.penilaian.intrakulikuler.edit');
    Route::put('intrakulikuler/{id}', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'updateIntrakulikuler'])->name('admin.penilaian.intrakulikuler.update');
    Route::delete('intrakulikuler/{id}', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'destroyIntrakulikuler'])->name('admin.penilaian.intrakulikuler.destroy');
    Route::get('ekstrakulikuler', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'ekstrakulikuler']);
    Route::post('ekstrakulikuler', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'storeEkstrakulikuler']);
    Route::put('ekstrakulikuler/{id}', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'updateEkstrakulikuler']);
    Route::delete('ekstrakulikuler/{id}', [\App\Http\Controllers\Admin\Penilaian\PenilaianController::class, 'destroyEkstrakulikuler']);
});

// Pendukung (admin)
Route::middleware(['auth', \App\Http\Middleware\IsAdmin::class])->prefix('admin/pendukung')->group(function () {
    Route::get('walimurid', [\App\Http\Controllers\Admin\PendukungController::class, 'walimurid']);
    // Basic CRUD routes for walimurid (stubs in controller) to avoid 404 on edit/delete
    Route::get('walimurid/create', [\App\Http\Controllers\Admin\PendukungController::class, 'create']);
    Route::post('walimurid', [\App\Http\Controllers\Admin\PendukungController::class, 'store']);
    Route::get('walimurid/{id}/edit', [\App\Http\Controllers\Admin\PendukungController::class, 'edit']);
    Route::put('walimurid/{id}', [\App\Http\Controllers\Admin\PendukungController::class, 'update']);
    Route::delete('walimurid/{id}', [\App\Http\Controllers\Admin\PendukungController::class, 'destroy']);

    Route::get('kehadiran', [\App\Http\Controllers\Admin\PendukungController::class, 'kehadiran']);
    Route::post('kehadiran/update', [\App\Http\Controllers\Admin\PendukungController::class, 'updateKehadiran']);
});

// Dokumen (admin)
Route::middleware(['auth', \App\Http\Middleware\IsAdmin::class])->prefix('admin/dokumen')->group(function () {
    Route::get('rapot', [\App\Http\Controllers\Admin\DokumenController::class, 'rapot']);
    Route::get('rapot/data', [\App\Http\Controllers\Admin\DokumenController::class, 'rapotData']);
    Route::get('rapot/cetak', [\App\Http\Controllers\Admin\DokumenController::class, 'cetak']);
    Route::get('buku-induk', [\App\Http\Controllers\Admin\DokumenController::class, 'bukuInduk']);
});

// Pendukung (guru) - full access within guru scope
Route::middleware(['auth', \App\Http\Middleware\IsGuru::class])->prefix('guru/pendukung')->group(function () {
    Route::get('walimurid', [\App\Http\Controllers\Admin\PendukungController::class, 'walimurid']);
    Route::get('walimurid/create', [\App\Http\Controllers\Admin\PendukungController::class, 'create']);
    Route::post('walimurid', [\App\Http\Controllers\Admin\PendukungController::class, 'store']);
    Route::get('walimurid/{id}/edit', [\App\Http\Controllers\Admin\PendukungController::class, 'edit']);
    Route::put('walimurid/{id}', [\App\Http\Controllers\Admin\PendukungController::class, 'update']);
    Route::delete('walimurid/{id}', [\App\Http\Controllers\Admin\PendukungController::class, 'destroy']);

    Route::get('kehadiran', [\App\Http\Controllers\Admin\PendukungController::class, 'kehadiran']);
    Route::post('kehadiran/update', [\App\Http\Controllers\Admin\PendukungController::class, 'updateKehadiran']);
});

// Dokumen (guru)
Route::middleware(['auth', \App\Http\Middleware\IsGuru::class])->prefix('guru/dokumen')->group(function () {
    Route::get('rapot', [\App\Http\Controllers\Guru\DokumenController::class, 'rapot']);
    Route::get('rapot/data', [\App\Http\Controllers\Admin\DokumenController::class, 'rapotData']);
    Route::get('rapot/cetak', [\App\Http\Controllers\Admin\DokumenController::class, 'cetak']);
    Route::get('buku-induk', [\App\Http\Controllers\Guru\DokumenController::class, 'bukuInduk']);
});

// Mata pelajaran CRUD for guru (allow guru to create/update/delete within their scope)
Route::middleware(['auth', \App\Http\Middleware\IsGuru::class])->prefix('guru/data-umum')->group(function () {
    Route::post('mata-pelajaran', [\App\Http\Controllers\Admin\DataUmum\DataUmumController::class, 'storeMataPelajaran'])->name('guru.mata-pelajaran.store');
    Route::get('mata-pelajaran/{id}/edit', [\App\Http\Controllers\Admin\DataUmum\DataUmumController::class, 'editMataPelajaran'])->name('guru.mata-pelajaran.edit');
    Route::put('mata-pelajaran/{id}', [\App\Http\Controllers\Admin\DataUmum\DataUmumController::class, 'updateMataPelajaran'])->name('guru.mata-pelajaran.update');
    Route::delete('mata-pelajaran/{id}', [\App\Http\Controllers\Admin\DataUmum\DataUmumController::class, 'destroyMataPelajaran'])->name('guru.mata-pelajaran.destroy');
});
