<?php

use App\Http\Controllers\AuthController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\GameController;
use App\Http\Controllers\Admin\JadwalController;
use App\Http\Controllers\Admin\KelasController;
use App\Http\Controllers\Admin\MataPelajaranController;
use App\Http\Controllers\Admin\UserController;
use App\Http\Controllers\Guru\GameController as GuruGameController;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
})->name('home');

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

Route::middleware('auth')->group(function () {
    Route::post('/logout', [AuthController::class, 'logout'])->name('logout');
    Route::get('/materi/{materi}/preview', [\App\Http\Controllers\Guru\MateriController::class, 'preview'])->name('materi.preview');
});

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

        Route::get('kelola-user/guru', [UserController::class, 'guru'])->name('users.guru');
        Route::get('kelola-user/guru/create', [UserController::class, 'createGuru'])->name('users.guru.create');
        Route::get('kelola-user/siswa', [UserController::class, 'siswa'])->name('users.siswa');
        Route::get('kelola-user/siswa/create', [UserController::class, 'createSiswa'])->name('users.siswa.create');
        Route::get('kelola-user/siswa/template', [UserController::class, 'downloadSiswaTemplate'])->name('users.siswa.template');
        Route::post('kelola-user/siswa/import', [UserController::class, 'importSiswa'])->name('users.siswa.import');
        Route::post('kelola-user/siswa/import/confirm', [UserController::class, 'confirmImportSiswa'])->name('users.siswa.import.confirm');
        Route::get('kelola-user/siswa/import/cancel', [UserController::class, 'cancelImportSiswa'])->name('users.siswa.import.cancel');
        Route::get('kelola-user/siswa/naik-kelas', [UserController::class, 'naikKelasForm'])->name('users.siswa.naik-kelas');
        Route::get('kelola-user/siswa/naik-kelas/siswa-by-kelas', [UserController::class, 'getSiswaByKelas'])->name('users.siswa.naik-kelas.siswa-by-kelas');
        Route::post('kelola-user/siswa/naik-kelas', [UserController::class, 'prosesNaikKelas'])->name('users.siswa.naik-kelas.store');

        Route::resource('kelola-user', UserController::class)
            ->parameters(['kelola-user' => 'user'])
            ->names('users')
            ->except(['show']);

        Route::resource('kelas', KelasController::class)
            ->parameters(['kelas' => 'kelas'])
            ->names('kelas')
            ->except(['create', 'show', 'edit']);

        Route::resource('mata-pelajaran', MataPelajaranController::class)
            ->parameters(['mata-pelajaran' => 'mataPelajaran'])
            ->names('mata-pelajaran')
            ->except(['create', 'show', 'edit']);

        Route::get('jadwal/guru/{guru}', [JadwalController::class, 'guru'])->name('jadwal.guru');
        Route::get('jadwal/guru/{guru}/create', [JadwalController::class, 'createForGuru'])->name('jadwal.guru.create');
        Route::get('jadwal/guru/{guru}/{jadwal}/edit', [JadwalController::class, 'editForGuru'])->name('jadwal.guru.edit');

        Route::resource('jadwal', JadwalController::class)
            ->parameters(['jadwal' => 'jadwal'])
            ->names('jadwal')
            ->except(['show']);

        Route::get('game/{game}/preview', [GameController::class, 'preview'])->name('game.preview');
        Route::resource('game', GameController::class)
            ->parameters(['game' => 'game'])
            ->names('game')
            ->except(['create', 'show', 'edit']);
    });

Route::middleware(['auth', 'role:guru'])
    ->prefix('guru')
    ->name('guru.')
    ->group(function () {
        Route::get('/dashboard', [\App\Http\Controllers\Guru\DashboardController::class, 'index'])->name('dashboard');

        // New Kelola Kelas Routes
        Route::get('/kelas', [\App\Http\Controllers\Guru\KelasController::class, 'index'])->name('kelas.index');
        Route::get('/kelas/{kelas}', [\App\Http\Controllers\Guru\KelasController::class, 'show'])->name('kelas.show');
        Route::get('/kelas/{kelas}/mapel/{mapel}', [\App\Http\Controllers\Guru\KelasController::class, 'mapel'])->name('kelas.mapel');
        Route::get('/kelas/{kelas}/mapel/{mapel}/materi', [\App\Http\Controllers\Guru\KelasController::class, 'materiMapel'])->name('kelas.mapel.materi');
        Route::get('/kelas/{kelas}/mapel/{mapel}/tugas', [\App\Http\Controllers\Guru\KelasController::class, 'tugasMapel'])->name('kelas.mapel.tugas');
        Route::get('/kelas/{kelas}/mapel/{mapel}/nilai', [\App\Http\Controllers\Guru\KelasController::class, 'nilaiMapel'])->name('kelas.mapel.nilai');
        Route::get('/kelas/{kelas}/mapel/{mapel}/absensi', [\App\Http\Controllers\Guru\KelasController::class, 'absensiMapel'])->name('kelas.mapel.absensi');
        Route::post('/kelas/{kelas}/mapel/{mapel}/absensi', [\App\Http\Controllers\Guru\KelasController::class, 'storeAbsensi'])->name('kelas.mapel.absensi.store');
        Route::get('/kelas/{kelas}/materi', [\App\Http\Controllers\Guru\KelasController::class, 'materi'])->name('kelas.materi');
        Route::get('/kelas/{kelas}/tugas', [\App\Http\Controllers\Guru\KelasController::class, 'tugas'])->name('kelas.tugas');
        Route::get('/kelas/{kelas}/absensi', [\App\Http\Controllers\Guru\KelasController::class, 'absensi'])->name('kelas.absensi');
        Route::get('/kelas/{kelas}/anggota', [\App\Http\Controllers\Guru\KelasController::class, 'anggota'])->name('kelas.anggota');
        Route::post('/kelas/salin-materi/{materi}', [\App\Http\Controllers\Guru\KelasController::class, 'salinMateri'])->name('kelas.salin-materi');
        Route::post('/kelas/salin-tugas/{tugas}', [\App\Http\Controllers\Guru\KelasController::class, 'salinTugas'])->name('kelas.salin-tugas');

        // Redirect deprecated index routes to new Kelas index
        Route::get('/materi', function() { return redirect()->route('guru.kelas.index'); })->name('materi');
        Route::get('/tugas', function() { return redirect()->route('guru.kelas.index'); })->name('tugas.index');
        Route::get('/absensi', function() { return redirect()->route('guru.kelas.index'); })->name('absensi');

        // Keep download & subresource CRUD for Materi/Tugas called inside classes
        Route::get('/materi/{materi}/download', [\App\Http\Controllers\Guru\MateriController::class, 'download'])->name('materi.download');
        Route::resource('materi', \App\Http\Controllers\Guru\MateriController::class)->except(['index', 'create']);

        // Ujian (Exam) routes for Guru
        Route::get('/soal', [\App\Http\Controllers\Guru\UjianController::class, 'index'])->name('soal');
        Route::post('/soal', [\App\Http\Controllers\Guru\UjianController::class, 'store'])->name('soal.store');
        Route::put('/soal/{ujian}', [\App\Http\Controllers\Guru\UjianController::class, 'update'])->name('soal.update');
        Route::delete('/soal/{ujian}', [\App\Http\Controllers\Guru\UjianController::class, 'destroy'])->name('soal.destroy');
        Route::post('/soal/{ujian}/tambah-soal', [\App\Http\Controllers\Guru\UjianController::class, 'tambahSoal'])->name('soal.tambah-soal');
        Route::put('/soal/{ujian}/edit-soal/{soal}', [\App\Http\Controllers\Guru\UjianController::class, 'editSoal'])->name('soal.edit-soal');
        Route::delete('/soal/{ujian}/hapus-soal/{soal}', [\App\Http\Controllers\Guru\UjianController::class, 'hapusSoal'])->name('soal.hapus-soal');
        Route::get('/soal/template', [\App\Http\Controllers\Guru\UjianController::class, 'downloadTemplate'])->name('soal.template');
        Route::post('/soal/{ujian}/import-preview', [\App\Http\Controllers\Guru\UjianController::class, 'importPreview'])->name('soal.import-preview');
        Route::post('/soal/{ujian}/import-save', [\App\Http\Controllers\Guru\UjianController::class, 'importSave'])->name('soal.import-save');

        Route::get('/game', [GuruGameController::class, 'index'])->name('game.index');
        Route::get('/game/{game}/main', [GuruGameController::class, 'main'])->name('game.main');

        // Tugas routes for Guru
        Route::get('tugas/{tugas}/pengumpulan/{pengumpulan}/preview', [\App\Http\Controllers\Guru\TugasController::class, 'previewPengumpulan'])->name('tugas.pengumpulan.preview');
        Route::get('tugas/{tugas}/pengumpulan/{pengumpulan}/download', [\App\Http\Controllers\Guru\TugasController::class, 'downloadPengumpulan'])->name('tugas.pengumpulan.download');
        Route::post('tugas/{tugas}/grade/{pengumpulan}', [\App\Http\Controllers\Guru\TugasController::class, 'grade'])->name('tugas.grade');
        Route::resource('tugas', \App\Http\Controllers\Guru\TugasController::class)
            ->parameters(['tugas' => 'tugas'])
            ->except(['index'])
            ->names('tugas');
        Route::resource('nilai', \App\Http\Controllers\Guru\NilaiController::class)->names('nilai');
    });

Route::middleware(['auth', 'role:siswa'])
    ->prefix('siswa')
    ->name('siswa.')
    ->group(function () {
        Route::get('/dashboard', [\App\Http\Controllers\Siswa\DashboardController::class, 'index'])->name('dashboard');

        Route::get('/mata-pelajaran', [\App\Http\Controllers\Siswa\MataPelajaranController::class, 'index'])->name('mapel');

        Route::get('/presensi', [\App\Http\Controllers\Siswa\PresensiController::class, 'index'])->name('presensi');

        Route::get('/nilai', [\App\Http\Controllers\Siswa\NilaiController::class, 'index'])->name('nilai');

        Route::get('/mata-pelajaran/{mapel}', [\App\Http\Controllers\Siswa\MataPelajaranController::class, 'show'])->name('mapel.detail');

        // Tugas routes for Siswa
        Route::get('tugas', [\App\Http\Controllers\Siswa\TugasController::class, 'index'])->name('tugas.index');
        Route::get('tugas/{tugas}', [\App\Http\Controllers\Siswa\TugasController::class, 'show'])->name('tugas.show');
        Route::post('tugas/{tugas}/submit', [\App\Http\Controllers\Siswa\TugasController::class, 'submit'])->name('tugas.submit');

        // Ujian (Exam) routes for Siswa
        Route::get('ujian', [\App\Http\Controllers\Siswa\UjianController::class, 'index'])->name('ujian.index');
        Route::get('ujian/{ujian}', [\App\Http\Controllers\Siswa\UjianController::class, 'show'])->name('ujian.show');
        Route::get('ujian/{ujian}/kerjakan', [\App\Http\Controllers\Siswa\UjianController::class, 'kerjakan'])->name('ujian.kerjakan');
        Route::post('ujian/{ujian}/submit', [\App\Http\Controllers\Siswa\UjianController::class, 'submit'])->name('ujian.submit');

        // Game routes for Siswa
        Route::get('game', [\App\Http\Controllers\Siswa\GameController::class, 'index'])->name('game.index');
        Route::get('game/{game}/main', [\App\Http\Controllers\Siswa\GameController::class, 'main'])->name('game.main');
    });
