<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

// ADMIN CONTROLLERS
use App\Http\Controllers\Admin\LoginController;
use App\Http\Controllers\Admin\AdminController;
use App\Http\Controllers\Admin\GuruController as AdminGuruController;
use App\Http\Controllers\Admin\KelasController as AdminKelasController;
use App\Http\Controllers\Admin\SiswaController as AdminSiswaController;
use App\Http\Controllers\Admin\MapelController as AdminMapelController;
use App\Http\Controllers\Admin\ChallengeController as AdminChallengeController;
use App\Http\Controllers\Admin\LeaderboardController as AdminLeaderboardController;
use App\Http\Controllers\Admin\ProfileController as AdminProfileController;
use App\Http\Controllers\Admin\MateriTugasController as AdminMateriTugasController;
use App\Http\Controllers\Admin\NotifikasiController as AdminNotifikasiController;

// GURU CONTROLLERS
use App\Http\Controllers\Guru\LoginController as GuruLoginController;
use App\Http\Controllers\Guru\DashboardController as GuruDashboardController;
use App\Http\Controllers\Guru\GuruController as GuruGuruController;
use App\Http\Controllers\Guru\KelasController as GuruKelasController;
use App\Http\Controllers\Guru\SiswaController as GuruSiswaController;
use App\Http\Controllers\Guru\ProfileController as GuruProfileController;
use App\Http\Controllers\Guru\MapelController as GuruMapelController;
use App\Http\Controllers\Guru\LeaderboardController as GuruLeaderboardController;
use App\Http\Controllers\Guru\NotifikasiController as GuruNotifikasiController;

//SISWA CONTROLLERS
use App\Http\Controllers\Siswa\LoginController as SiswaLoginController;
use App\Http\Controllers\Siswa\DashboardController as SiswaDashboardController;
use App\Http\Controllers\Siswa\MateriController as SiswaMateriController;
use App\Http\Controllers\Siswa\TugasController as SiswaTugasController;
use App\Http\Controllers\Siswa\ChallengeController as SiswaChallengeController;
use App\Http\Controllers\Siswa\LeaderboardController as SiswaLeaderboardController;
use App\Http\Controllers\Siswa\ProfileController as SiswaProfileController;
use App\Http\Controllers\Siswa\NotifikasiController as SiswaNotifikasiController;

// ====================
// LANDING PAGE
// ====================
Route::get('/', function () {
    return view('auth.landing-page');
})->name('landing-page');


// =======================================================
// LOGIN PAGES (VIEW ONLY)
// =======================================================

// LOGIN ADMIN
Route::get('/admin/login', function () {
    return view('auth.login-admin');
})->name('admin.login');

// LOGIN GURU
Route::get('/guru/login', [GuruLoginController::class, 'showLoginForm'])
    ->name('guru.login');

// LOGIN SISWA
Route::get('/siswa/login', [SiswaLoginController::class, 'showLoginForm'])
    ->name('siswa.login');


// =======================================================
// PROSES LOGIN (POST)
// =======================================================

// ADMIN LOGIN PROCESS
Route::post('/admin/login', [LoginController::class, 'loginAdmin'])
    ->name('admin.login.submit');

// GURU LOGIN PROCESS
Route::post('/guru/login', [GuruLoginController::class, 'login'])
    ->name('guru.login.submit');

// SISWA LOGIN PROCESS
Route::post('/siswa/login', [SiswaLoginController::class, 'login'])
    ->name('siswa.login.submit');

// =======================================================
// ADMIN AREA (HARUS LOGIN ADMIN)
// =======================================================
Route::middleware(['auth:admin'])->prefix('admin')->name('admin.')->group(function () {

    // DASHBOARD
    Route::get('/dashboard', [AdminController::class, 'dashboard'])->name('dashboard');

    // PROFILE
    Route::get('/profile',  [AdminProfileController::class, 'edit'])->name('profile.edit');
    Route::post('/profile', [AdminProfileController::class, 'updateAjax'])->name('profile.update');

    //GURU
    Route::get('/guru/kelas-by-mapel', [AdminGuruController::class, 'getKelasByMapel'])
        ->name('guru.kelasByMapel');
    Route::resource('guru', AdminGuruController::class);

    //SISWA KELAS DAN MAPEL
    Route::resource('siswa', AdminSiswaController::class);
    Route::resource('kelas', AdminKelasController::class);
    Route::resource('mapel', AdminMapelController::class);

    //HISTORI MATERI
    Route::get('/materi/history', [AdminMateriTugasController::class, 'historyMateri'])->name('materi.history');
    Route::delete('/materi/{id}', [AdminMateriTugasController::class, 'destroyMateri'])->name('materi.destroy');

    //HISTRORY TUGAS
    Route::get('/tugas/history',       [AdminMateriTugasController::class, 'historyTugas'])->name('tugas.history');
    Route::get('/tugas/{id}/detail',   [AdminMateriTugasController::class, 'detailTugas'])->name('tugas.detail');
    Route::delete('/tugas/{id}',       [AdminMateriTugasController::class, 'destroyTugas'])->name('tugas.destroy');

    //CHALLENGE
    Route::get('/challenge/{id}/edit-data', [AdminChallengeController::class, 'editData'])
        ->name('challenge.editData');
    Route::resource('challenge', AdminChallengeController::class);

    //LEADERBOARD
    Route::resource('leaderboard', AdminLeaderboardController::class)->only(['index']);

    // NOTIF
    Route::get('/notifikasi', [AdminNotifikasiController::class, 'index'])->name('notifikasi');

    //LOGOUT
    Route::post('/logout', [LoginController::class, 'logout'])->name('logout');

});

// =======================================================
// GURU AREA (HARUS LOGIN GURU)
// =======================================================
Route::middleware(['auth:guru'])->prefix('guru')->name('guru.')->group(function () {

    Route::get('/dashboard', [GuruDashboardController::class, 'index'])->name('dashboard');

    // Daftar Guru (Read Only)
    Route::get('/daftar-guru', [GuruGuruController::class, 'index'])->name('guru.index');

    // Daftar Kelas (Read Only)
    Route::get('/daftar-kelas', [GuruKelasController::class, 'index'])->name('kelas.index');

    // Daftar Siswa (Read Only)
    Route::get('/daftar-siswa', [GuruSiswaController::class, 'index'])->name('siswa.index');

    Route::get('/mapel', [GuruMapelController::class, 'index'])
    ->name('mapel.index');
    Route::post('/materi/store', [GuruMapelController::class, 'storeMateri'])->name('materi.store');
    Route::post('/tugas/store',  [GuruMapelController::class, 'storeTugas'])->name('tugas.store');

    Route::get('/leaderboard', [GuruLeaderboardController::class, 'index'])
    ->name('leaderboard.index');

    Route::get('/materi/history', [GuruMapelController::class, 'historyMateri'])->name('materi.history');
    Route::delete('/materi/{id}', [GuruMapelController::class, 'destroyMateri'])->name('materi.destroy');

    Route::get('/tugas/history', [GuruMapelController::class, 'historyTugas'])->name('tugas.history');
    Route::get('/tugas/{id}/detail', [GuruMapelController::class, 'detailTugas'])->name('tugas.detail');
    Route::delete('/tugas/{id}', [GuruMapelController::class, 'destroyTugas'])->name('tugas.destroy');

    Route::get('/profile',  [GuruProfileController::class, 'edit'])->name('profile.edit');
    Route::post('/profile', [GuruProfileController::class, 'updateAjax'])->name('profile.update');

    Route::get('/notifikasi', [GuruNotifikasiController::class, 'index'])->name('notifikasi');

    // LOGOUT GURU
    Route::post('/logout', [GuruLoginController::class, 'logout'])->name('logout');
});

// =======================================================
// SISWA AREA
// =======================================================
Route::middleware(['auth:siswa'])->prefix('siswa')->name('siswa.')->group(function () {

    Route::get('/dashboard', [SiswaDashboardController::class, 'index'])->name('dashboard');

    // MATERI
    Route::get('/materi', [SiswaMateriController::class, 'index'])->name('materi.index');
    Route::get('/materi/{id_mengajar}', [SiswaMateriController::class, 'show'])->name('materi.show');

    // TUGAS
    Route::get('/tugas', [SiswaTugasController::class, 'index'])->name('tugas.index');
    Route::get('/tugas/{id_tugas}', [SiswaTugasController::class, 'show'])->name('tugas.show');
    Route::post('/tugas/{id_tugas}/submit', [SiswaTugasController::class, 'submit'])->name('tugas.submit');

    // CHALLENGE SISWA
    Route::get('/challenge',                    [SiswaChallengeController::class, 'index'])->name('challenge.index');
    Route::get('/challenge/{id}/kerjakan',      [SiswaChallengeController::class, 'kerjakan'])->name('challenge.kerjakan');
    Route::post('/challenge/{id}/submit',       [SiswaChallengeController::class, 'submit'])->name('challenge.submit');
    Route::get('/challenge/{id}/hasil',         [SiswaChallengeController::class, 'hasil'])->name('challenge.hasil');

    //LEADERBOARD SISWA
    Route::get('/leaderboard', [SiswaLeaderboardController::class, 'index'])->name('leaderboard.index');
    Route::get('/leaderboard/json', [SiswaLeaderboardController::class, 'json'])->name('leaderboard.json');

    //PROFILE SISWA
    Route::get('/profile',  [SiswaProfileController::class, 'edit'])->name('profile.edit');
    Route::post('/profile', [SiswaProfileController::class, 'updateAjax'])->name('profile.update');

    // NOTIF
    Route::get('/notifikasi', [SiswaNotifikasiController::class, 'index'])->name('notifikasi');
    
    // LOGOUT SISWA
    Route::post('/logout', [SiswaLoginController::class, 'logout'])->name('logout');

});