<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\LandingPageController;
use App\Http\Controllers\Superadmin\UserController;
use App\Http\Controllers\Superadmin\RoleController;
use App\Http\Controllers\Superadmin\SystemSettingsController;
use App\Http\Controllers\Superadmin\SchoolController;
use App\Http\Controllers\DisabilityTypeController;
use App\Http\Controllers\StudentController;

// Landing & Auth Routes
Route::get('/', [LandingPageController::class, 'index'])->name('landing');

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

// Protected Routes (require authentication)
Route::middleware(['auth'])->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
    
    // Superadmin Routes
    Route::middleware(['role:superadmin'])->prefix('superadmin')->name('superadmin.')->group(function () {
        Route::resource('kab-kota', 'App\Http\Controllers\Superadmin\KabKotaController');
        Route::resource('kcd', 'App\Http\Controllers\Superadmin\KcdController');
        Route::resource('schools', SchoolController::class);
        Route::post('schools/{id}/toggle-status', [SchoolController::class, 'toggleStatus'])->name('schools.toggle-status');
        Route::get('schools-export', [SchoolController::class, 'export'])->name('schools.export');
        Route::get('schools-template', [SchoolController::class, 'downloadTemplate'])->name('schools.download-template');
        Route::post('schools-import', [SchoolController::class, 'import'])->name('schools.import');
        
        // Employee Routes
        Route::resource('employees', 'App\Http\Controllers\Superadmin\EmployeeController');
        Route::patch('employees/{employee}/toggle-status', ['App\Http\Controllers\Superadmin\EmployeeController', 'toggleStatus'])->name('employees.toggle-status');
        
        // Employee Assignment Routes
        Route::resource('employee-assignments', 'App\Http\Controllers\Superadmin\EmployeeAssignmentController');
        Route::patch('employee-assignments/{employeeAssignment}/toggle-status', ['App\Http\Controllers\Superadmin\EmployeeAssignmentController', 'toggleStatus'])->name('employee-assignments.toggle-status');
        
        // User-School Mapping Routes
        Route::resource('user-school-mapping', 'App\Http\Controllers\Superadmin\UserSchoolMappingController');
        
        // Academic Year Routes
        Route::get('/academic-years', 'App\Http\Controllers\Superadmin\AcademicYearController@index')->name('academic-years.index');
        Route::get('/academic-years/create', 'App\Http\Controllers\Superadmin\AcademicYearController@create')->name('academic-years.create');
        Route::post('/academic-years', 'App\Http\Controllers\Superadmin\AcademicYearController@store')->name('academic-years.store');
        Route::get('/academic-years/{id}/edit', 'App\Http\Controllers\Superadmin\AcademicYearController@edit')->name('academic-years.edit');
        Route::put('/academic-years/{id}', 'App\Http\Controllers\Superadmin\AcademicYearController@update')->name('academic-years.update');
        Route::post('/academic-years/{id}/activate', 'App\Http\Controllers\Superadmin\AcademicYearController@activate')->name('academic-years.activate');
        Route::delete('/academic-years/{id}', 'App\Http\Controllers\Superadmin\AcademicYearController@destroy')->name('academic-years.destroy');
        
        // Daya Tampung Routes
        Route::get('/daya-tampung', [App\Http\Controllers\Superadmin\DayaTampungController::class, 'index'])->name('daya-tampung.index');
        Route::get('/daya-tampung/{id}', [App\Http\Controllers\Superadmin\DayaTampungController::class, 'show'])->name('daya-tampung.show');
        
        Route::resource('users', UserController::class);
        Route::post('users/{user}/reset-password', [UserController::class, 'resetPassword'])->name('users.resetPassword');
        Route::resource('roles', RoleController::class);
        Route::get('/system-settings', [SystemSettingsController::class, 'index'])->name('settings.index');
        Route::post('/system-settings', [SystemSettingsController::class, 'update'])->name('settings.update');
        Route::post('/system-settings/clear-cache', [SystemSettingsController::class, 'clearCache'])->name('settings.clearCache');
        Route::post('/system-settings/clear-logs', [SystemSettingsController::class, 'clearLogs'])->name('settings.clearLogs');
        Route::get('/logs', function() { return view('superadmin.logs'); })->name('logs');
        
        // Disability Type Management Routes (Super Admin Only)
        Route::resource('disability-types', DisabilityTypeController::class);
        Route::patch('disability-types/{disabilityType}/toggle-active', [DisabilityTypeController::class, 'toggleActive'])->name('disability-types.toggle-active');
    });
    
    // Admin Dinas Routes
    Route::middleware(['role:admin_dinas'])->prefix('admin-dinas')->name('admin-dinas.')->group(function () {
        Route::get('/sekolah', 'App\Http\Controllers\AdminDinas\SekolahController@index')->name('sekolah.index');
        Route::get('/sekolah/create', 'App\Http\Controllers\AdminDinas\SekolahController@create')->name('sekolah.create');
        Route::post('/sekolah', 'App\Http\Controllers\AdminDinas\SekolahController@store')->name('sekolah.store');
        Route::get('/sekolah/{id}', 'App\Http\Controllers\AdminDinas\SekolahController@show')->name('sekolah.show');
        Route::get('/sekolah/{id}/edit', 'App\Http\Controllers\AdminDinas\SekolahController@edit')->name('sekolah.edit');
        Route::put('/sekolah/{id}', 'App\Http\Controllers\AdminDinas\SekolahController@update')->name('sekolah.update');
        Route::delete('/sekolah/{id}', 'App\Http\Controllers\AdminDinas\SekolahController@destroy')->name('sekolah.destroy');
        Route::post('/sekolah/{id}/unlock', 'App\Http\Controllers\AdminDinas\SekolahController@unlock')->name('sekolah.unlock');
        Route::get('/sekolah-export', 'App\Http\Controllers\AdminDinas\SekolahController@export')->name('sekolah.export');
        Route::get('/sekolah-template', 'App\Http\Controllers\AdminDinas\SekolahController@downloadTemplate')->name('sekolah.download-template');
        Route::post('/sekolah-import', 'App\Http\Controllers\AdminDinas\SekolahController@import')->name('sekolah.import');
        Route::get('/pengawas', 'App\Http\Controllers\AdminDinas\PengawasController@index')->name('pengawas');
        Route::get('/mapping-kcd', function() { return view('admin-dinas.mapping-kcd'); })->name('mapping-kcd');
        
        // PSAJ Management Routes
        Route::prefix('psaj')->name('psaj.')->group(function () {
            Route::get('/', [App\Http\Controllers\AdminDinas\PsajController::class, 'index'])->name('index');
            Route::get('/{id}/show', [App\Http\Controllers\AdminDinas\PsajController::class, 'show'])->name('show');
            Route::post('/{id}/unlock', [App\Http\Controllers\AdminDinas\PsajController::class, 'unlock'])->name('unlock');
            Route::post('/bulk-unlock', [App\Http\Controllers\AdminDinas\PsajController::class, 'bulkUnlock'])->name('bulk-unlock');
        });
        
        // Murid Management Routes
        Route::get('/murid-baru', [App\Http\Controllers\AdminDinas\MuridController::class, 'muridBaru'])->name('murid-baru');
        Route::get('/naik-jenjang', [App\Http\Controllers\AdminDinas\MuridController::class, 'naikJenjang'])->name('naik-jenjang');
        Route::get('/murid/{id}/detail', [App\Http\Controllers\AdminDinas\MuridController::class, 'detail'])->name('murid.detail');
        
        // Announcement Management Routes
        Route::resource('announcements', App\Http\Controllers\AdminDinas\AnnouncementController::class);
        Route::post('announcements/{announcement}/toggle-status', [App\Http\Controllers\AdminDinas\AnnouncementController::class, 'toggleStatus'])->name('announcements.toggle-status');
    });
    
    // Admin KCD Routes
    Route::middleware(['role:admin_kcd'])->prefix('admin-kcd')->name('admin-kcd.')->group(function () {
        Route::get('/sekolah', [App\Http\Controllers\AdminKcd\SekolahController::class, 'index'])->name('sekolah');
        
        Route::get('/pengawas', [App\Http\Controllers\AdminKcd\PengawasController::class, 'index'])->name('pengawas');
        Route::post('/pengawas/{employee}/reset-password', [App\Http\Controllers\AdminKcd\PengawasController::class, 'resetPassword'])->name('pengawas.resetPassword');
        
        Route::get('/mapping-sekolah', [App\Http\Controllers\AdminKcd\MappingSekolahController::class, 'index'])->name('mapping-sekolah');
        Route::post('/mapping-sekolah', [App\Http\Controllers\AdminKcd\MappingSekolahController::class, 'store'])->name('mapping-sekolah.store');
        Route::delete('/mapping-sekolah/{id}', [App\Http\Controllers\AdminKcd\MappingSekolahController::class, 'destroy'])->name('mapping-sekolah.destroy');
        Route::post('/mapping-sekolah/unassign', [App\Http\Controllers\AdminKcd\MappingSekolahController::class, 'unassignSchool'])->name('mapping-sekolah.unassign');
    });
    
    // Pengawas Routes
    Route::middleware(['role:pengawas'])->prefix('pengawas')->name('pengawas.')->group(function () {
        Route::get('/sekolah-binaan', function() { return view('pengawas.sekolah-binaan'); })->name('sekolah-binaan');
        Route::get('/verifikasi-sptjm', function() { return view('pengawas.verifikasi-sptjm'); })->name('verifikasi-sptjm');
    });
    
    // Admin Sekolah Routes
    Route::middleware(['role:admin_sekolah'])->prefix('admin-sekolah')->name('admin-sekolah.')->group(function () {
        Route::get('/profil', [App\Http\Controllers\AdminSekolah\ProfilController::class, 'index'])->name('profil');
        Route::put('/profil/update', [App\Http\Controllers\AdminSekolah\ProfilController::class, 'update'])->name('profil.update');
        
        Route::get('/sptjm', function() { return view('admin-sekolah.sptjm'); })->name('sptjm');
        
        // GTK Routes
        Route::prefix('gtk')->name('gtk.')->group(function () {
            Route::get('/guru', [App\Http\Controllers\AdminSekolah\GtkController::class, 'guru'])->name('guru');
            Route::get('/tendik', [App\Http\Controllers\AdminSekolah\GtkController::class, 'tendik'])->name('tendik');
        });
        
        // Murid Routes
        Route::get('/murid', [App\Http\Controllers\AdminSekolah\MuridController::class, 'index'])->name('murid');
        
        // Murid Baru Routes
        Route::prefix('murid-baru')->name('murid-baru.')->group(function () {
            Route::get('/daya-tampung', [App\Http\Controllers\AdminSekolah\MuridBaruController::class, 'dayaTampung'])->name('daya-tampung');
            Route::post('/daya-tampung/rombel-saat-ini', [App\Http\Controllers\AdminSekolah\MuridBaruController::class, 'storeRombelSaatIni'])->name('daya-tampung.rombel-saat-ini');
            Route::post('/daya-tampung/rencana-rombel', [App\Http\Controllers\AdminSekolah\MuridBaruController::class, 'storeRencanaRombel'])->name('daya-tampung.rencana-rombel');
            Route::post('/daya-tampung/slb', [App\Http\Controllers\AdminSekolah\MuridBaruController::class, 'storeDayaTampungSlb'])->name('daya-tampung.slb');
            Route::get('/input', [App\Http\Controllers\AdminSekolah\MuridBaruController::class, 'input'])->name('input');
            Route::post('/input/store', [App\Http\Controllers\AdminSekolah\MuridBaruController::class, 'store'])->name('input.store');
            Route::get('/daftar', [App\Http\Controllers\AdminSekolah\MuridBaruController::class, 'daftar'])->name('daftar');
            Route::get('/{id}/detail', [App\Http\Controllers\AdminSekolah\MuridBaruController::class, 'detail'])->name('detail');
            Route::delete('/{id}/delete', [App\Http\Controllers\AdminSekolah\MuridBaruController::class, 'destroy'])->name('delete');
        });
        
        // Naik Jenjang Routes
        Route::prefix('naik-jenjang')->name('naik-jenjang.')->group(function () {
            Route::get('/', [App\Http\Controllers\AdminSekolah\NaikJenjangController::class, 'index'])->name('index');
            Route::get('/input', [App\Http\Controllers\AdminSekolah\NaikJenjangController::class, 'input'])->name('input');
            Route::post('/store', [App\Http\Controllers\AdminSekolah\NaikJenjangController::class, 'store'])->name('store');
            Route::get('/{id}/detail', [App\Http\Controllers\AdminSekolah\NaikJenjangController::class, 'detail'])->name('detail');
            Route::delete('/{id}/delete', [App\Http\Controllers\AdminSekolah\NaikJenjangController::class, 'destroy'])->name('delete');
        });
        
        // PSAJ Routes - CRUD for PSAJ Participants
        Route::prefix('psaj')->name('psaj.')->group(function () {
            Route::get('/{jenjang?}', [App\Http\Controllers\AdminSekolah\PsajController::class, 'index'])->name('index');
            Route::get('/create/new', [App\Http\Controllers\AdminSekolah\PsajController::class, 'create'])->name('create');
            Route::post('/', [App\Http\Controllers\AdminSekolah\PsajController::class, 'store'])->name('store');
            Route::post('/submit', [App\Http\Controllers\AdminSekolah\PsajController::class, 'submit'])->name('submit');
            Route::get('/kartu-batch/print', [App\Http\Controllers\AdminSekolah\PsajController::class, 'generateKartuBatch'])->name('kartu-batch');
            Route::get('/{id}/show', [App\Http\Controllers\AdminSekolah\PsajController::class, 'show'])->name('show');
            Route::get('/{id}/edit', [App\Http\Controllers\AdminSekolah\PsajController::class, 'edit'])->name('edit');
            Route::put('/{id}', [App\Http\Controllers\AdminSekolah\PsajController::class, 'update'])->name('update');
            Route::delete('/{id}', [App\Http\Controllers\AdminSekolah\PsajController::class, 'destroy'])->name('destroy');
            Route::get('/{id}/kartu', [App\Http\Controllers\AdminSekolah\PsajController::class, 'generateKartu'])->name('kartu');
        });
    });
    
    // Student Routes (accessible by multiple roles)
    Route::middleware(['role:superadmin,admin_kcd,admin_sekolah,kepala_sekolah'])->group(function () {
        Route::resource('students', StudentController::class);
        Route::get('students/get-default-kelas', [StudentController::class, 'getDefaultKelas'])->name('students.get-default-kelas');
    });
    
    // Guru Routes
    Route::middleware(['role:guru'])->prefix('guru')->name('guru.')->group(function () {
        Route::get('/evaluasi', function() { return view('guru.evaluasi'); })->name('evaluasi');
    });
});
