<?php

use App\Http\Controllers\AuthController;
use App\Http\Controllers\BackendController\BerandaController;
use App\Http\Controllers\BackendController\BeritaController;
use App\Http\Controllers\BackendController\BiayaController;
use App\Http\Controllers\BackendController\BrosurController;
use App\Http\Controllers\BackendController\KurikulumController;
use App\Http\Controllers\BackendController\HalamanController;
use App\Http\Controllers\BackendController\MenuController;
use App\Http\Controllers\BackendController\POController;
use App\Http\Controllers\BackendController\SettingController;
use App\Http\Controllers\BackendController\TracerController;
use App\Http\Controllers\FrontendController\HomeController;
use App\Http\Controllers\FrontendController\TracerStudyController;
use App\Http\Controllers\galleryController;

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/clear', function () {
    Artisan::call('config:cache');
    Artisan::call('config:clear');
    Artisan::call('view:clear');
    return 'Cleared!';
});

// ========== FRONTEND ROUTES (TANPA MIDDLEWARE) ==========
Route::namespace('Home')->group(function () {
    Route::get('/', [App\Http\Controllers\FrontendController\HomeController::class, 'index'])->name('fe-home');
    Route::get('/event-kampus', [App\Http\Controllers\FrontendController\HomeController::class, 'eventKampus'])->name('fe-event-kampus');
    Route::get('/kegiatan-mahasiswa', [App\Http\Controllers\FrontendController\HomeController::class, 'kegiatanKampus'])->name('fe-kegiatan-kampus');
    Route::get('/fasilitas-kampus', [App\Http\Controllers\FrontendController\HomeController::class, 'fasilitasKampus'])->name('fe-fasilitas-kampus');
    Route::get('/sejarah-singkat', [App\Http\Controllers\FrontendController\HomeController::class, 'sejarah'])->name('fe-sejarah');
    Route::get('/visi-misi', [App\Http\Controllers\FrontendController\HomeController::class, 'visimisi'])->name('fe-visimisi');
    Route::get('/Biaya-kuliah', [App\Http\Controllers\FrontendController\HomeController::class, 'biayakuliah'])->name('fe-biayakuliah');
    Route::get('/struktur-organisasi', [App\Http\Controllers\FrontendController\HomeController::class, 'strukturorganisasi'])->name('fe-strukturorganisasi');
    Route::get('/page/{id}', [App\Http\Controllers\FrontendController\HomeController::class, 'pageShow'])->name('fe-page');
    Route::get('/brosur', [App\Http\Controllers\FrontendController\HomeController::class, 'brosur'])->name('fe-brosur');
    Route::post('/kontak-form', [App\Http\Controllers\FrontendController\HomeController::class, 'storeKontak'])->name('store.fe-kontak');
    // Route::get('/biaya-jurusan', [App\Http\Controllers\FrontendController\HomeController::class, 'biayajurusan'])->name('fe.biayajurusan');
    Route::get('/kurikulum', [App\Http\Controllers\FrontendController\HomeController::class, 'kurikulum'])->name('fe.kurikulum');

    Route::get('/get-jurusan/{kelas}', [App\Http\Controllers\FrontendController\HomeController::class, 'getJurusanByKelas'])->name('get.jurusan.by.kelas');
    
    Route::get('/detail-berita/{id}', [HomeController::class, 'detailberita'])->name('fe-detailberita');

    Route::get('/kontak-form', [App\Http\Controllers\FrontendController\HomeController::class, 'kontak'])->name('fe-kontak');

    Route::get('/pendaftaran', [App\Http\Controllers\FrontendController\HomeController::class, 'showForm'])->name('registration.show');
    Route::post('/pendaftaran/simpan', [App\Http\Controllers\FrontendController\HomeController::class, 'storeRegistration'])->name('registration.store');
    Route::get('/pendaftaran/success/{no_daftar}', [App\Http\Controllers\FrontendController\HomeController::class, 'showSuccessPage'])->name('registration.success');

    Route::get('/pendaftaran/cetak/{no_daftar}', [App\Http\Controllers\FrontendController\HomeController::class, 'cetak'])->name('cetak.confirm');
    
    Route::get('/pendaftaran/download-pdf/{no_daftar}', [App\Http\Controllers\FrontendController\HomeController::class, 'downloadPDF'])->name('download.pdf');
    
    Route::get('/tracer-study', [TracerStudyController::class, 'index'])->name('tracer-study');
    Route::post('/tracer-study', [TracerStudyController::class, 'store'])->name('tracer-study.store');
    Route::get('/tracer-study/success', [TracerStudyController::class, 'success'])->name('tracer-study.success');
});

// ========== AUTH ROUTES (GUEST) ==========
Route::middleware('guest')->group(function () {
    Route::get('/cobalogink2', [AuthController::class, 'login'])->name('login');
    Route::post('/cobalogink2', [AuthController::class, 'dologin'])->name('login.post');
});

// ========== ADMIN ROUTES (AUTH) ==========
Route::middleware('auth')->group(function () {
    
    // ROUTE YANG BISA DIAKSES SEMUA ADMIN (TERMASUK ID 13)
    Route::post('/logout', [AuthController::class, 'logout'])->name('logout');
    Route::get('/dashboard', [AuthController::class, 'dashboard'])->name('dashboard');
    
    Route::get('/profile', [AuthController::class, 'profile'])->name('profile');
    Route::put('/profile/update/{id}', [AuthController::class, 'updateProfile'])->name('profile.update');
    Route::get('/change-password', [AuthController::class, 'changePassword'])->name('changePassword');
    Route::post('/change-password', [AuthController::class, 'doChangePassword'])->name('change.password');
    
    // GALLERY - Bisa diakses semua admin
    Route::get('/gallery', [galleryController::class, 'index'])->name('gallery.index');
    Route::get('/gallery/tambah-foto', [galleryController::class, 'create'])->name('gallery.create');
    Route::post('/gallery/tambah-foto', [galleryController::class, 'store'])->name('gallery.store');
    Route::delete('/gallery/delete-foto/{id}', [galleryController::class, 'destroy'])->name('gallery.delete');
    Route::get('/gallery/{id}/edit', [GalleryController::class, 'show'])->name('gallery.edit');
    Route::put('/gallery/update-foto/{id}', [GalleryController::class, 'update'])->name('gallery.update');
    
    // KONTAK - Bisa diakses semua admin
    Route::get('/kontak', [galleryController::class, 'kontak'])->name('kontak.index');
    Route::delete('/kontak/{id}', [galleryController::class, 'destroyKontak'])->name('kontak.destroy');
    
    // DATA PENDAFTARAN - Bisa diakses semua admin
    Route::get('/data-pendaftaran', [POController::class, 'daftar'])->name('daftar');
    Route::get('/data-pendaftaran/detil/{id}', [POController::class, 'detil'])->name('daftar.detil');
    Route::get('/data-pendaftaran/{id}', [POController::class, 'hapus'])->name('daftar.hapus');
    Route::get('/data-pendaftaran/cetak', [POController::class, 'cetak'])->name('daftar.cetak');
    
    // BERITA - Bisa diakses semua admin
    Route::get('/berita', [BeritaController::class, 'index'])->name('berita');
    Route::get('/berita/create', [BeritaController::class, 'create'])->name('berita.create');
    Route::post('/berita/create', [BeritaController::class, 'store'])->name('berita.store');
    Route::post('/berita/upload-image', [BeritaController::class, 'uploadImage'])->name('berita.upload.image'); // <-- TAMBAHKAN INI
    Route::get('berita/edit/{id}', [BeritaController::class, 'edit'])->name('berita.edit');
    Route::put('berita/{id}', [BeritaController::class, 'update'])->name('berita.update');
    Route::delete('/berita/{id}', [BeritaController::class, 'destroy'])->name('berita.destroy');
    
    // BROSUR - Bisa diakses semua admin
    Route::get('/data-brosur', [BrosurController::class, 'index'])->name('brosur.index');
    Route::get('/data-brosur/tambah', [BrosurController::class, 'create'])->name('brosur.create');
    Route::post('/data-brosur/tambah', [BrosurController::class, 'store'])->name('brosur.store');
    Route::delete('/data-brosur/delete/{id}', [BrosurController::class, 'destroy'])->name('brosur.delete');
    Route::get('/data-brosur/{id}/edit', [BrosurController::class, 'edit'])->name('brosur.edit');
    Route::put('/data-brosur/update/{id}', [BrosurController::class, 'update'])->name('brosur.update');
    
    // TRACER STUDY - Bisa diakses semua admin
    Route::get('/tracer-study-data', [TracerController::class, 'index'])->name('tracer.index');
    Route::get('/tracer-study-data/{id}', [TracerController::class, 'show'])->name('tracer.show');
    Route::delete('/tracer-study-data/{id}', [TracerController::class, 'destroy'])->name('tracer.destroy');
    Route::get('/tracer-study-export', [TracerController::class, 'export'])->name('tracer.export');
    
    // ========== ROUTE YANG HANYA UNTUK ADMIN BIASA (BUKAN ID 13) ==========
    
    // MENU - Hanya admin biasa
    Route::middleware('menu.access:menu')->group(function () {
        Route::get('/menu', [MenuController::class, 'index'])->name('menu');
        Route::get('/menu/create', [MenuController::class, 'create'])->name('menu.create');
        Route::post('/menu', [MenuController::class, 'store'])->name('menu.store');
        Route::get('/menu/edit/{id}', [MenuController::class, 'edit'])->name('menu.edit');
        Route::put('/menu/{id}', [MenuController::class, 'update'])->name('menu.update');
        Route::delete('/menu/{id}', [MenuController::class, 'destroy'])->name('menu.destroy');
    });
    
    // PAGES - Hanya admin biasa
    Route::middleware('menu.access:pages')->group(function () {
        Route::get('/pages', [HalamanController::class, 'index'])->name('pages');
        Route::get('/pages/create', [HalamanController::class, 'create'])->name('pages.create');
        Route::post('/pages', [HalamanController::class, 'store'])->name('pages.store');
        Route::get('pages/edit/{id}', [HalamanController::class, 'edit'])->name('pages.edit');
        Route::put('pages/{id}', [HalamanController::class, 'update'])->name('pages.update');
        Route::delete('/pages/{id}', [HalamanController::class, 'destroy'])->name('pages.destroy');
    });
    
    // SETTINGS - Hanya admin biasa
    Route::middleware('menu.access:settings')->group(function () {
        Route::get('/settings', [SettingController::class, 'index'])->name('settings');
        Route::get('/settings/create', [SettingController::class, 'create'])->name('settings.create');
        Route::post('/settings', [SettingController::class, 'store'])->name('settings.store');
        Route::get('/settings/edit/{id}', [SettingController::class, 'edit'])->name('settings.edit');
        Route::put('/settings/{id}', [SettingController::class, 'update'])->name('settings.update');
        Route::delete('/settings/{id}', [SettingController::class, 'destroy'])->name('settings.destroy');
        Route::get('settings/{id}/toggle-status', [SettingController::class, 'toggleStatus'])->name('settings.toggleStatus');
    });
    
    // USER MANAGEMENT - Hanya admin biasa

    Route::middleware('menu.access:user')->group(function () {
        Route::get('/table-user', [AuthController::class, 'index'])->name('user.index');
        Route::get('/create-user', [AuthController::class, 'create'])->name('user.create');
        Route::post('/create-user', [AuthController::class, 'store'])->name('user.store');
        Route::delete('/delete-user/{id}', [AuthController::class, 'delete'])->name('user.delete');
        Route::put('/extend-masa-aktif/{id}', [AuthController::class, 'extendMasaAktif'])->name('user.extend.masa_aktif');
    });
    
    // BIAYA - Hanya admin biasa
    Route::middleware('menu.access:biaya')->group(function () {
        Route::get('/data-biaya', [BiayaController::class, 'index'])->name('biaya.index');
        Route::get('/data-biaya/create', [BiayaController::class, 'create'])->name('biaya.create');
        Route::post('/data-biaya', [BiayaController::class, 'store'])->name('biaya.store');
        Route::get('/data-biaya/edit/{id}', [BiayaController::class, 'edit'])->name('biaya.edit');
        Route::put('/data-biaya/{id}', [BiayaController::class, 'update'])->name('biaya.update');
        Route::delete('/data-biaya/{id}', [BiayaController::class, 'destroy'])->name('biaya.destroy');
    });
    
    // KURIKULUM - Hanya admin biasa
    Route::middleware('menu.access:kurikulum')->group(function () {
        Route::get('/data-kurikulum', [KurikulumController::class, 'index'])->name('kurikulum.index');
        Route::get('/data-kurikulum/tambah', [KurikulumController::class, 'create'])->name('kurikulum.create');
        Route::post('/data-kurikulum/tambah', [KurikulumController::class, 'store'])->name('kurikulum.store');
        Route::delete('/data-kurikulum/delete/{id}', [KurikulumController::class, 'destroy'])->name('kurikulum.delete');
        Route::get('/data-kurikulum/{id}/edit', [KurikulumController::class, 'edit'])->name('kurikulum.edit');
        Route::put('/data-kurikulum/update/{id}', [KurikulumController::class, 'update'])->name('kurikulum.update');
    });
    
    // BERANDA - Hanya admin biasa (jika diperlukan)
    Route::middleware('menu.access:beranda')->group(function () {
        Route::get('/beranda', [BerandaController::class, 'index'])->name('beranda');
        Route::get('/beranda/create', [BerandaController::class, 'create'])->name('beranda.create');
        Route::post('/beranda', [BerandaController::class, 'store'])->name('beranda.store');
        Route::get('beranda/edit/{id}', [BerandaController::class, 'edit'])->name('beranda.edit');
        Route::delete('/beranda', [BerandaController::class, 'destroy'])->name('beranda.destroy');
    });
});