<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Artisan;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\StudentController;
use App\Http\Controllers\KelasController;
use App\Http\Controllers\SchoolProfileController;
use App\Http\Controllers\SubjectController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\MessageController;
use App\Http\Controllers\ScheduleController;
use App\Http\Controllers\TeachingMaterialController;
use App\Http\Controllers\JournalController;
use App\Http\Controllers\AnnualProgramController;
use App\Http\Controllers\SemesterProgramController;
use App\Http\Controllers\LearningModuleController;
use App\Http\Controllers\LearningObjectiveController;
use App\Http\Controllers\AssessmentController;
use App\Http\Controllers\PenilaianController;
use App\Http\Controllers\AttendanceRecapController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\LkpdController; // <--- PASTIKAN BARIS INI ADA

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

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

Route::middleware(['auth'])->group(function () {

    // A. DASHBOARD & PROFILE
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
    Route::put('/profile/password', [ProfileController::class, 'updatePassword'])->name('profile.password');
    Route::put('/profile/picture', [ProfileController::class, 'updatePicture'])->name('profile.picture');

    // B. FITUR PESAN
    Route::get('/pesan', [MessageController::class, 'index'])->name('message.index');
    Route::post('/pesan/kirim', [MessageController::class, 'store'])->name('message.store');
    Route::get('/pesan/baca/{id}', [MessageController::class, 'markAsRead'])->name('message.read');
    Route::delete('/pesan/hapus/{id}', [MessageController::class, 'destroy'])->name('message.destroy');

    // C. MASTER DATA (REFERENSI)
    
    // 1. Rute Custom Siswa (WAJIB DI ATAS RESOURCE)
    Route::get('referensi/siswa/kelas/{id}', [StudentController::class, 'showClass'])->name('students.showClass');
    Route::post('referensi/siswa/import', [StudentController::class, 'import'])->name('siswa.import');
    Route::get('referensi/siswa/template', [StudentController::class, 'downloadTemplate'])->name('siswa.template');
    
    // 2. Resource Siswa (WAJIB DI BAWAH RUTE CUSTOM SISWA)
    Route::resource('referensi/siswa', StudentController::class)->names('students');
    
    // 3. Rute Master Data Lainnya
    Route::post('referensi/kelas', [KelasController::class, 'store'])->name('classes.store');
    Route::put('referensi/kelas/{id}', [KelasController::class, 'update'])->name('classes.update');
    Route::delete('referensi/kelas/{id}', [KelasController::class, 'destroy'])->name('classes.destroy');
    
    Route::resource('referensi/sekolah', SchoolProfileController::class)->only(['index', 'update'])->names('sekolah');
    Route::post('referensi/sekolah/jam', [SchoolProfileController::class, 'storeLessonHour'])->name('sekolah.jam.store');
    Route::delete('referensi/sekolah/jam/{id}', [SchoolProfileController::class, 'destroyLessonHour'])->name('sekolah.jam.destroy');
    
    Route::resource('referensi/pengguna', UserController::class)->names('pengguna');
    Route::resource('referensi/mapel', SubjectController::class)->names('mapel');
    
// D. PEMBELAJARAN
    Route::prefix('pembelajaran/jadwal')->name('jadwal.')->group(function () {
    Route::get('/', [ScheduleController::class, 'index'])->name('index');
    Route::post('/', [ScheduleController::class, 'store'])->name('store');
    Route::delete('/delete-group', [ScheduleController::class, 'destroyGroup'])->name('destroyGroup');
    Route::delete('/{id}', [ScheduleController::class, 'destroy'])->name('destroy');
    Route::get('/get-events', [ScheduleController::class, 'getEvents'])->name('getEvents');
    Route::get('/teacher-subjects/{teacherId}', [ScheduleController::class, 'getTeacherSubjects'])->name('getTeacherSubjects');
        
    // --- PERBAIKAN DI SINI ---
    Route::get('/print', [ScheduleController::class, 'print'])->name('print');
        
        // Route Khusus Pekan Tidak Efektif (Memperbaiki Error Nama)
        Route::post('/pekan-tidak-efektif', [ScheduleController::class, 'storePekanTidakEfektif'])->name('storePekanTidakEfektif');
        Route::delete('/pekan-tidak-efektif/{id}', [ScheduleController::class, 'destroyPekanTidakEfektif'])->name('destroyPekanTidakEfektif');
    });


    // Route Pembelajaran Lainnya
    Route::get('pembelajaran/bahan-ajar', [TeachingMaterialController::class, 'index'])->name('bahanajar.index');
    Route::post('pembelajaran/bahan-ajar/ask', [TeachingMaterialController::class, 'askAi'])->name('bahanajar.ask');
    Route::resource('pembelajaran/jurnal', JournalController::class)->names('jurnal');
    Route::get('/ajax/classes', [JournalController::class, 'getClasses'])->name('ajax.classes');
    Route::get('/ajax/get-topics', [JournalController::class, 'getTopics'])->name('ajax.topics');
    Route::get('/ajax/get-students', [JournalController::class, 'getStudents'])->name('ajax.students');
    Route::get('/ajax/last-meeting', [App\Http\Controllers\JournalController::class, 'getLastMeeting'])->name('ajax.last_meeting');
    
    // E. PERANGKAT AJAR (Kunci Perbaikan Error Disini)
    
    // --- 1. PROTA ---
    
    Route::post('/prota/generate-ai', [AnnualProgramController::class, 'generateAiProta'])->name('api.ai.generate-prota');
    Route::get('/prota/print/{id}', [AnnualProgramController::class, 'printSingle'])->name('prota.printSingle');
    Route::get('/prota/cetak/laporan', [AnnualProgramController::class, 'print'])->name('prota.print');
    Route::resource('prota', AnnualProgramController::class)->names('prota');

    // --- 2. PROMES ---
    Route::post('/promes/get-prota', [SemesterProgramController::class, 'getProtaData'])->name('api.promes.get-prota');
    Route::post('/promes/generate-ai', [SemesterProgramController::class, 'generateAiPromes'])->name('api.ai.generate-promes');
    Route::get('/promes/print/{id}', [SemesterProgramController::class, 'printSingle'])->name('promes.printSingle');
    Route::get('/promes/cetak', [SemesterProgramController::class, 'print'])->name('promes.print');
    Route::resource('promes', SemesterProgramController::class)->names('promes');

    // --- 3. ATP ---
    Route::post('/atp/generate-ai', [LearningObjectiveController::class, 'generateAiTP'])->name('api.ai.generate-tp');
    Route::get('/atp/print/{id}', [LearningObjectiveController::class, 'printSingle'])->name('atp.printSingle');
    Route::get('atp/cetak/data', [LearningObjectiveController::class, 'print'])->name('atp.print');
    Route::resource('atp', LearningObjectiveController::class)->names('atp');

    // --- 4. MODUL AJAR ---
    Route::get('/modul/print/{id}', [LearningModuleController::class, 'printSingle'])->name('modul.printSingle');
    Route::post('/modul/print', [LearningModuleController::class, 'printBulk'])->name('modul.print');
    Route::post('/modul/generate-ai', [LearningModuleController::class, 'generateAISteps'])->name('api.generateAI');
    Route::get('/api/modul/get-next-meeting', [App\Http\Controllers\LearningModuleController::class, 'getNextMeeting'])->name('api.getNextMeeting');
    Route::resource('modul', LearningModuleController::class)->names('modul');

    // F. ASESMEN & PENILAIAN
    Route::resource('asesmen', AssessmentController::class)->names('asesmen');
    Route::get('asesmen/cetak/data', [AssessmentController::class, 'print'])->name('asesmen.print');    
    Route::post('/asesmen/cetak-banyak', [AssessmentController::class, 'printBulk'])->name('asesmen.print_bulk');
    
    // --- PINDAHKAN ROUTE KHUSUS KE ATAS (SEBELUM RESOURCE) ---
    Route::get('/penilaian/cetak/print', [PenilaianController::class, 'print'])->name('penilaian.print');
    Route::get('/penilaian/{id}/get-data', [PenilaianController::class, 'getDataForEdit'])->name('penilaian.get_data');
    Route::get('/ajax/penilaian/classes', [PenilaianController::class, 'getClasses'])->name('ajax.penilaian.classes');
    Route::get('/api/get-siswa/{kelas_id}', [PenilaianController::class, 'getSiswaByKelas'])->name('api.get_siswa');
    
    // --- BARU RESOURCE DI BAWAHNYA ---
    Route::resource('penilaian', PenilaianController::class);

    // G. LAPORAN
    Route::get('/laporan/rekap-kehadiran', [AttendanceRecapController::class, 'index'])->name('rekap.index');
    Route::post('/laporan/rekap-kehadiran', [AttendanceRecapController::class, 'filter'])->name('rekap.filter');
    Route::get('/laporan/rekap-kehadiran/cetak', [AttendanceRecapController::class, 'print'])->name('rekap.print');
    Route::get('pembelajaran/laporan', [ReportController::class, 'index'])->name('laporan.index');
    Route::get('pembelajaran/laporan/cetak', [ReportController::class, 'print'])->name('laporan.print');

    // H. API HELPER / AJAX
    Route::get('/api/get-cp/{subjectId}', [LearningModuleController::class, 'getCP'])->name('api.getCP');
    Route::post('/api/get-tp', [LearningModuleController::class, 'getTP'])->name('api.getTP');
    Route::get('/ajax/cp/{subject_id}', [LearningModuleController::class, 'getCP']); 
    Route::get('/ajax/tp', [LearningModuleController::class, 'getTP'])->name('ajax.getTP');
    Route::get('/ajax/rekap/classes', [AttendanceRecapController::class, 'getClasses'])->name('ajax.rekap.classes');

    // Route Khusus Print & AI
    Route::post('/lkpd/print-bulk', [LkpdController::class, 'printBulk'])->name('lkpd.print_bulk');
    Route::post('/lkpd/generate-ai', [LkpdController::class, 'generateAi'])->name('lkpd.generate_ai');
    
    // Route CRUD Standar
    Route::resource('lkpd', LkpdController::class);
});

Route::get('/bersih-bersih', function() {
    Artisan::call('optimize:clear');
    return "<h1>✅ SISTEM BERSIH!</h1><br><a href='/'>Kembali ke Dashboard</a>";
});