<?php

use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\DashboardController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\SiswaController;
use App\Http\Controllers\SiswaKelasController;

/*
|--------------------------------------------------------------------------
| Auth Routes (Guest)
|--------------------------------------------------------------------------
*/
Route::middleware('guest')->group(function () {
    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')->middleware('auth');

/*
|--------------------------------------------------------------------------
| Protected Routes (Auth Required)
|--------------------------------------------------------------------------
*/
Route::middleware('auth')->group(function () {
    // Redirect root ke dashboard
    Route::get('/', fn() => redirect()->route('dashboard'));

    // Dashboard - semua role bisa akses
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
    Route::get('/ajax/search-nis', [\App\Http\Controllers\TagihanController::class, 'searchNis'])->name('ajax.search-nis');

    // ─── ADMIN ONLY ───────────────────────────────────────────────────────────
    Route::middleware('role:admin')->group(function () {
        // Data Master
        Route::get('/ajax/tahun-ajaran', [\App\Http\Controllers\TahunAjaranController::class, 'searchAjax'])->name('ajax.tahun-ajaran');
        Route::get('/ajax/kelas', [\App\Http\Controllers\KelasController::class, 'searchAjax'])->name('ajax.kelas');
        
        Route::resource('tahun-ajaran', \App\Http\Controllers\TahunAjaranController::class);
        Route::resource('kelas', \App\Http\Controllers\KelasController::class);
        Route::post('/kelas/{kelas}/toggle-status', [\App\Http\Controllers\KelasController::class, 'toggleStatus'])->name('kelas.toggle-status');
        
        Route::get('/siswa/download-template', [\App\Http\Controllers\SiswaController::class, 'downloadTemplate'])->name('siswa.download-template');
        Route::post('/siswa/import-excel', [\App\Http\Controllers\SiswaController::class, 'importExcel'])->name('siswa.import-excel');
        Route::put('/siswa/{siswa}/status', [\App\Http\Controllers\SiswaController::class, 'updateStatus'])->name('siswa.update-status');
        Route::post('/siswa/bulk-status', [\App\Http\Controllers\SiswaController::class, 'bulkUpdateStatus'])->name('siswa.bulk-status');
        Route::resource('siswa', \App\Http\Controllers\SiswaController::class);

        // ─── SISWA KELAS (Tahun Ajaran Mapping) ─────────────────────────────
        Route::get('/siswa-kelas', [SiswaKelasController::class, 'index'])
            ->name('siswa-kelas.index');

        Route::post('/siswa-kelas', [SiswaKelasController::class, 'store'])
            ->name('siswa-kelas.store');

        Route::put('/siswa-kelas/{id}', [SiswaKelasController::class, 'update'])
            ->name('siswa-kelas.update');
            
         Route::get('/siswa-kelas/export', [SiswaKelasController::class, 'export'])
            ->name('siswa-kelas.export');

        Route::post('/siswa-kelas/import', [SiswaKelasController::class, 'import'])
            ->name('siswa-kelas.import');
            
        // Kenaikan Kelas (Rollover)
        Route::get('/kenaikan-kelas', [\App\Http\Controllers\KenaikanKelasController::class, 'index'])->name('kenaikan-kelas.index');
        Route::post('/kenaikan-kelas/proses', [\App\Http\Controllers\KenaikanKelasController::class, 'proses'])->name('kenaikan-kelas.proses');
        Route::get('/kenaikan-kelas/export', [\App\Http\Controllers\KenaikanKelasController::class, 'export'])->name('kenaikan-kelas.export');
        Route::get('/kenaikan-kelas/download-template', [\App\Http\Controllers\KenaikanKelasController::class, 'downloadTemplate'])->name('kenaikan-kelas.download-template');
        Route::post('/kenaikan-kelas/import', [\App\Http\Controllers\KenaikanKelasController::class, 'import'])->name('kenaikan-kelas.import');
                        

        // Data Tarif Pembayaran
        Route::post('/tarif-pembayaran/copy-from-previous-year', [\App\Http\Controllers\TarifPembayaranController::class, 'copyFromPreviousYear'])->name('tarif-pembayaran.copy-year');
        Route::resource('tarif-pembayaran', \App\Http\Controllers\TarifPembayaranController::class);
        Route::post('/tarif-pembayaran/{tarif_pembayaran}/toggle-status', [\App\Http\Controllers\TarifPembayaranController::class, 'toggleStatus'])->name('tarif-pembayaran.toggle-status');

        // User Management
        Route::resource('users', \App\Http\Controllers\UserController::class);

        
    });

    // ─── ADMIN & BENDAHARA ────────────────────────────────────────────────────
    Route::middleware('role:admin,bendahara')->group(function () {
        // Transaksi Pembayaran
        Route::get('/transaksi/create', [\App\Http\Controllers\TransaksiPembayaranController::class, 'create'])->name('transaksi.create');
        Route::post('/transaksi', [\App\Http\Controllers\TransaksiPembayaranController::class, 'store'])->name('transaksi.store');
        Route::get('/transaksi/{transaksi}/bukti', [\App\Http\Controllers\TransaksiPembayaranController::class, 'bukti'])->name('transaksi.bukti');
        Route::post('/ajax/transaksi/kirim-wa-bukti', [\App\Http\Controllers\TransaksiPembayaranController::class, 'kirimWaBukti'])->name('ajax.transaksi.kirim-wa-bukti');
    });

    // ─── ADMIN, BENDAHARA, WALI KELAS ──────────────────────────────────────────
    Route::middleware('role:admin,bendahara,wali_kelas')->group(function () {
        // Tagihan
        Route::get('/tagihan', [\App\Http\Controllers\TagihanController::class, 'index'])->name('tagihan.index');
        Route::get('/tagihan/cari', [\App\Http\Controllers\TagihanController::class, 'cari'])->name('tagihan.cari');
        Route::patch('/tagihan/{tagihan}/tarif-lama', [\App\Http\Controllers\TagihanController::class, 'updateTarifLama'])->name('tagihan.update-tarif-lama');
        Route::get('/ajax/tagihan/daftar-tunggakan', [\App\Http\Controllers\TagihanController::class, 'daftarTunggakanSemua'])->name('ajax.tagihan.daftar-tunggakan');
        Route::post('/ajax/tagihan/kirim-wa', [\App\Http\Controllers\TagihanController::class, 'kirimWa'])->name('ajax.tagihan.kirim-wa');
        Route::post('/ajax/tagihan/kirim-wa-bulk', [\App\Http\Controllers\TagihanController::class, 'kirimWaBulk'])->name('ajax.tagihan.kirim-wa-bulk');
        Route::post('/tagihan/potongan-spp-bulk', [\App\Http\Controllers\TagihanController::class, 'updatePotonganSppBulk'])->name('tagihan.update-potongan-spp-bulk');

        // Wali Kelas
        Route::get('/wali-kelas/status-pembayaran', [\App\Http\Controllers\WaliKelasController::class, 'statusPembayaran'])->name('wali-kelas.status-pembayaran');
    });

    // ─── ADMIN, BENDAHARA, KEPALA SEKOLAH, YAYASAN, WALI KELAS ─────────────────────────────
    Route::middleware('role:admin,bendahara,kepala_sekolah,yayasan,wali_kelas')->group(function () {
        // Laporan
        Route::get('/laporan', [\App\Http\Controllers\LaporanController::class, 'index'])->name('laporan.index');
        Route::get('/laporan/pembayaran/export', [\App\Http\Controllers\LaporanController::class, 'exportPembayaran'])->name('laporan.pembayaran.export');
        Route::get('/laporan/tagihan/export', [\App\Http\Controllers\LaporanController::class, 'exportTagihan'])->name('laporan.tagihan.export');
        Route::get('/laporan/harian/export', [\App\Http\Controllers\LaporanController::class, 'exportHarian'])->name('laporan.harian.export');
        Route::get('/laporan/riwayat/export', [\App\Http\Controllers\LaporanController::class, 'exportRiwayat'])->name('laporan.riwayat.export');
        Route::get('/laporan/riwayat/cetak', [\App\Http\Controllers\LaporanController::class, 'cetakRiwayat'])->name('laporan.cetakRiwayat');
        Route::post('/ajax/laporan/kirim-wa-rekap', [\App\Http\Controllers\LaporanController::class, 'kirimWaRekap'])->name('ajax.laporan.kirim-wa-rekap');
        Route::get('/laporan/riwayat-wa', [\App\Http\Controllers\LaporanController::class, 'riwayatWa'])->name('laporan.riwayat-wa');
        Route::get('/laporan/riwayat-wa/cetak', [\App\Http\Controllers\LaporanController::class, 'cetakRiwayatWa'])->name('laporan.riwayat-wa.cetak');
    });

    
        
            
});

