<?php

use App\Http\Controllers\AdminKampus\HistoryController;
use App\Http\Controllers\AdminKampus\RectorWelcomeController;  // Import Controller RectorWelcome
use App\Http\Controllers\AdminKampus\TimelinePendaftaranController;
use App\Http\Controllers\AdminKampus\AlurPendaftaranController; // Import Controller AlurPendaftaran
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;
use App\Http\Controllers\ProfileController;
use App\Http\Middleware\RoleMiddleware;
use App\Http\Controllers\LandingPageController;
use App\Http\Controllers\SejarahController;
use App\Http\Controllers\VisiMisiController;
use App\Http\Controllers\StrukturOrganisasiController;
use App\Http\Controllers\BiayaJurusanController; 
use App\Http\Controllers\FasilitasController; 
use App\Http\Controllers\SistemPerkuliahanController; 
use App\Http\Controllers\KurikulumController;
use App\Http\Controllers\BrosurController; 
use App\Http\Controllers\GalleryController; // Controller Gallery (Frontend)
use App\Http\Controllers\NewsController; // Controller News (Frontend)
use App\Http\Controllers\PendaftaranController;
use App\Http\Controllers\TracerStudyController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\Admin\CampusController;
use App\Http\Controllers\Admin\UserController;
use App\Http\Controllers\Admin\ManajemenEmailController;
// 💡 Import Controller Admin Kampus yang sudah ada
use App\Http\Controllers\AdminKampus\VisiMisiController as AdminVisiMisiController;
use App\Http\Controllers\AdminKampus\StrukturOrganisasiController as AdminStrukturOrganisasiController;
use App\Http\Controllers\AdminKampus\BrosurController as AdminBrosurController; 

// ✅ Import Controller Admin Kampus yang baru/terkait
use App\Http\Controllers\AdminKampus\FasilitasController as AdminFasilitasController;
use App\Http\Controllers\AdminKampus\SistemPerkuliahanController as AdminSistemPerkuliahanController;
use App\Http\Controllers\AdminKampus\JurusanController; 
use App\Http\Controllers\AdminKampus\NewsController as AdminNewsController; // Import NewsController Admin Kampus
use App\Http\Controllers\AdminKampus\GalleryController as AdminGalleryController; // 🆕 BARIS BARU: Import GalleryController Admin Kampus
use App\Http\Controllers\AdminKampus\KurikulumController as AdminKurikulumController; // 🆕 BARIS BARU: Import GalleryController Admin Kampus
use App\Http\Controllers\AdminKampus\TracerStudyController as AdminTracerStudyController; // Import TracerStudyController
use App\Http\Controllers\AdminKampus\PendaftaranMahasiswaPOController; // 🆕 BARIS BARU: Import Controller Pendaftaran Mahasiswa

/*
|-------------------------------------------------------------------------- 
| Web Routes
|-------------------------------------------------------------------------- 
*/

// ========================================================================
// 1. RUTE AREA PENGGUNA TERAUTENTIKASI (AUTH AREA) & AUTENTIKASI DASAR
// ========================================================================

require __DIR__.'/auth.php'; 

// Rute Dashboard Default (Fallback)
Route::get('/dashboard', [DashboardController::class, 'index'])
    ->middleware(['auth', 'verified'])
    ->name('dashboard');

// Rute untuk pengguna terautentikasi (Profil & CMS)
Route::middleware('auth')->group(function () {
    // Rute Profil Pengguna
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
    
    // RUTE CMS ADMIN (Manajemen Data)
    Route::prefix('admin')->group(function () {
        
        // Rute Dashboard Khusus Berbasis Role
        Route::get('/super', [DashboardController::class, 'superAdminIndex'])
            ->name('admin.super.dashboard');
            
        Route::get('/campus', [DashboardController::class, 'campusAdminIndex'])
            ->name('admin.campus.dashboard');

        // --- RUTE RESOURCE CAMPUS & USER (HANYA UNTUK SUPERADMIN) ---
        Route::middleware(RoleMiddleware::class . ':superadmin')->prefix('super')->name('admin.super.')->group(function () {
            
            // --- MANAJEMEN KAMPUS ---
            Route::put('kampus/{campus}/toggle-status', [CampusController::class, 'toggleStatus'])->name('kampus.toggle-status');

            // Rute CRUD Resource Kampus
            Route::resource('kampus', CampusController::class)
                ->names([
                    'index'   => 'kampus.index', 
                    'create'  => 'kampus.create', 
                    'store'   => 'kampus.store', 
                    'show'    => 'kampus.show', 
                    'edit'    => 'kampus.edit', 
                    'update'  => 'kampus.update', 
                    'destroy' => 'kampus.destroy', 
                ]);
                
            // MANAJEMAN PENGGUNA (USER)
            // Rute CRUD Resource Pengguna
            Route::resource('users', UserController::class)
                ->names([
                    'index'   => 'users.index', 
                    'create'  => 'users.create', 
                    'store'   => 'users.store', 
                    'edit'    => 'users.edit', 
                    'update'  => 'users.update', 
                    'destroy' => 'users.destroy', 
                ]);

            // MANAJEMEN EMAIL KAMPUS (USER)
            // Rute CRUD Resource Pengguna
            Route::resource('manajemen-email', ManajemenEmailController::class)
                ->names([
                    'index'   => 'manajemen-email.index', 
                    'create'  => 'manajemen-email.create', 
                    'store'   => 'manajemen-email.store', 
                    'edit'    => 'manajemen-email.edit', 
                    'update'  => 'manajemen-email.update', 
                    'destroy' => 'manajemen-email.destroy', 
                ]);
        });
        
        // --- RUTE ADMIN KAMPUS (BERDASARKAN campus_id USER) ---
        Route::prefix('campus')->group(function () {
            
            // --- RUTE ADMIN KAMPUS RECTOR WELCOME (SINGLETON) ---
            Route::prefix('rector-welcome')->name('admin.kampus.rectorWelcome.')->group(function () {
                Route::get('/', [RectorWelcomeController::class, 'index'])->name('index');
                Route::get('/create', [RectorWelcomeController::class, 'create'])->name('create');
                Route::post('/', [RectorWelcomeController::class, 'store'])->name('store');
                Route::get('/edit', [RectorWelcomeController::class, 'edit'])->name('edit');
                Route::put('/', [RectorWelcomeController::class, 'update'])->name('update');
                Route::delete('/', [RectorWelcomeController::class, 'destroy'])->name('destroy');
            });
            // --- RUTE ADMIN KAMPUS Timeline Pendaftaran ---
            Route::prefix('timeline')->name('admin.kampus.timeline.')->group(function () {
                Route::get('/', [TimelinePendaftaranController::class, 'index'])->name('index');
                Route::get('/create', [TimelinePendaftaranController::class, 'create'])->name('create');
                Route::post('/', [TimelinePendaftaranController::class, 'store'])->name('store');
                Route::get('/{id}/edit', [TimelinePendaftaranController::class, 'edit'])->name('edit');
                Route::post('/{id}', [TimelinePendaftaranController::class, 'update'])->name('update');
                Route::delete('/{id}', [TimelinePendaftaranController::class, 'destroy'])->name('destroy');
                Route::post('/{id}/toggle-active', [TimelinePendaftaranController::class, 'toggleActive'])->name('toggleActive');   
            });

            Route::prefix('kurikulum')->name('admin.kampus.kurikulum.')->group(function () {
                // Menampilkan semua kurikulum
                Route::get('/', [AdminKurikulumController::class, 'index'])->name('index');

                // Menampilkan form untuk membuat kurikulum baru
                Route::get('/create', [AdminKurikulumController::class, 'create'])->name('create');
                
                // Menyimpan kurikulum baru
                Route::post('/', [AdminKurikulumController::class, 'store'])->name('store');
                
                // Menampilkan form untuk mengedit kurikulum
                Route::get('/{id}/edit', [AdminKurikulumController::class, 'edit'])->name('edit');
                
                // Memperbarui kurikulum yang sudah ada
                Route::put('/{id}', [AdminKurikulumController::class, 'update'])->name('update');
                
                // Menghapus kurikulum
                Route::delete('/{id}', [AdminKurikulumController::class, 'destroy'])->name('destroy');
            });

            // --- RUTE ADMIN KAMPUS Alur Pendaftaran ---
            Route::prefix('alur-pendaftaran')->name('admin.kampus.alurPendaftaran.')->group(function () {
                // Menampilkan semua langkah pendaftaran
                Route::get('/', [AlurPendaftaranController::class, 'index'])->name('index');

                // Menampilkan form untuk membuat langkah pendaftaran baru
                Route::get('/create', [AlurPendaftaranController::class, 'create'])->name('create');
                
                // Menyimpan langkah pendaftaran baru
                Route::post('/', [AlurPendaftaranController::class, 'store'])->name('store');
                
                // Menampilkan form untuk mengedit langkah pendaftaran
                Route::get('/{id}/edit', [AlurPendaftaranController::class, 'edit'])->name('edit');
                
                // Memperbarui langkah pendaftaran yang sudah ada
                Route::put('/{id}', [AlurPendaftaranController::class, 'update'])->name('update');
                
                // Menghapus langkah pendaftaran
                Route::delete('/{id}', [AlurPendaftaranController::class, 'destroy'])->name('destroy');
                
                // Mengubah status aktif/non-aktif langkah pendaftaran
                Route::post('/{id}/toggle-status', [AlurPendaftaranController::class, 'toggleStatus'])->name('toggleStatus');
            });
            // --- RUTE ADMIN KAMPUS SEJARAH (SINGLETON) ---
            Route::prefix('sejarah')->name('admin.kampus.sejarah.')->group(function () {
                Route::get('/', [HistoryController::class, 'index'])->name('index');
                Route::get('/create', [HistoryController::class, 'create'])->name('create');
                Route::post('/', [HistoryController::class, 'store'])->name('store');
                Route::get('/edit', [HistoryController::class, 'edit'])->name('edit');
                Route::put('/', [HistoryController::class, 'update'])->name('update');
                Route::delete('/', [HistoryController::class, 'destroy'])->name('destroy');
            });

            // RUTE ADMIN KAMPUS VISI & MISI (SINGLETON)
            Route::prefix('visimisi')->name('admin.kampus.visimisi.')->group(function () {
                Route::get('/', [AdminVisiMisiController::class, 'index'])->name('index');
                Route::get('/create', [AdminVisiMisiController::class, 'create'])->name('create');
                Route::post('/', [AdminVisiMisiController::class, 'store'])->name('store');
                Route::get('/edit', [AdminVisiMisiController::class, 'edit'])->name('edit');
                Route::match(['put', 'patch'], '/', [AdminVisiMisiController::class, 'update'])->name('update'); 
                Route::delete('/', [AdminVisiMisiController::class, 'destroy'])->name('destroy');
            });

            // RUTE ADMIN KAMPUS STRUKTUR ORGANISASI (SINGLETON)
            Route::prefix('struktur-organisasi')->name('admin.kampus.organisasi.')->group(function () {
                Route::get('/', [AdminStrukturOrganisasiController::class, 'index'])->name('index'); 
                Route::get('/create', [AdminStrukturOrganisasiController::class, 'create'])->name('create');
                Route::post('/', [AdminStrukturOrganisasiController::class, 'store'])->name('store');
                Route::get('/edit', [AdminStrukturOrganisasiController::class, 'edit'])->name('edit');
                Route::match(['put', 'patch'], '/', [AdminStrukturOrganisasiController::class, 'update'])->name('update'); 
                Route::delete('/', [AdminStrukturOrganisasiController::class, 'destroy'])->name('destroy');
            });
            
            // RUTE ADMIN KAMPUS BROSUR (SINGLETON)
            Route::prefix('brosur')->name('admin.kampus.brosur.')->group(function () {
                Route::get('/', [AdminBrosurController::class, 'index'])->name('index');
                Route::get('/create', [AdminBrosurController::class, 'create'])->name('create');
                Route::post('/', [AdminBrosurController::class, 'store'])->name('store');
                Route::get('/edit', [AdminBrosurController::class, 'edit'])->name('edit');
                Route::match(['put', 'patch'], '/', [AdminBrosurController::class, 'update'])->name('update');
                Route::delete('/', [AdminBrosurController::class, 'destroy'])->name('destroy');
            });

            // --- RUTE ADMIN KAMPUS TRACER STUDY ---
            Route::prefix('tracer-study')->name('admin.kampus.tracerStudy.')->group(function () {

                // Menampilkan daftar Tracer Study
                Route::get('/', [AdminTracerStudyController::class, 'index'])->name('index');
                
                // Menampilkan form untuk membuat Tracer Study baru
                Route::get('/create', [AdminTracerStudyController::class, 'create'])->name('create');
                
                // Menyimpan Tracer Study baru
                Route::post('/', [AdminTracerStudyController::class, 'store'])->name('store');
                
                // Menampilkan form untuk mengedit Tracer Study
                Route::get('/{id}/edit', [AdminTracerStudyController::class, 'edit'])->name('edit');
                
                // Memperbarui Tracer Study yang sudah ada
                Route::put('/{id}', [AdminTracerStudyController::class, 'update'])->name('update');
                
                // Menghapus Tracer Study
                Route::delete('/{id}', [AdminTracerStudyController::class, 'destroy'])->name('destroy');
                
                // Halaman Konfirmasi Sukses Setelah Submit (GET)
                Route::get('/sukses/{id}', [AdminTracerStudyController::class, 'showSuccess'])->name('sukses');
                
                // Menampilkan Detail Tracer Study Berdasarkan ID
                Route::get('/{id}', [AdminTracerStudyController::class, 'show'])->name('show');
            });

            Route::resource('pendaftaran-po', PendaftaranMahasiswaPOController::class)
                ->names('admin.kampus.pendaftaranPO')
                ->except(['show']); // Tidak perlu show, karena semua ditampilkan di index
            // Akhir penambahan Rute Pendaftaran Mahasiswa PO Admin Kampus
            // RUTE ADMIN KAMPUS FASILITAS (RESOURCE/LIST)
            Route::resource('fasilitas', AdminFasilitasController::class)
                ->names('admin.kampus.fasilitas');
                
            // Rute khusus untuk mengubah status Fasilitas
            Route::patch('fasilitas/{facility}/status', [AdminFasilitasController::class, 'updateStatus'])
                ->name('admin.kampus.fasilitas.update.status');

            // RUTE ADMIN KAMPUS SISTEM PERKULIAHAN (RESOURCE/LIST)
            Route::resource('sistem-perkuliahan', AdminSistemPerkuliahanController::class)
                ->names('admin.kampus.sistem_perkuliahan');

            // Rute khusus untuk mengubah status Sistem Perkuliahan
            Route::patch('sistem-perkuliahan/{sistem_perkuliahan}/status', [AdminSistemPerkuliahanController::class, 'updateStatus'])
                ->name('admin.kampus.sistem_perkuliahan.update.status');
            
            // RUTE ADMIN KAMPUS JURUSAN & BIAYA KULIAH (RESOURCE/LIST)
            Route::resource('jurusan', JurusanController::class)
                ->names('admin.kampus.jurusan')
                ;

            // RUTE ADMIN KAMPUS BERITA (NEWS) (RESOURCE/LIST)
            Route::resource('berita', AdminNewsController::class)
                ->names('admin.kampus.berita');
                
            // Rute khusus untuk mengubah status publikasi Berita
            Route::patch('berita/{beritum}/status', [AdminNewsController::class, 'updateStatus'])
                ->name('admin.kampus.berita.update.status');
            
            // 🆕 BARIS BARU: RUTE ADMIN KAMPUS GALERI (RESOURCE/LIST)
            Route::resource('galeri', AdminGalleryController::class)
                ->names('admin.kampus.galeri');
            // Akhir penambahan Rute Galeri Admin Kampus

        }); 
        // Akhir dari Route::prefix('campus')

    });
});
    

// ========================================================================
// 2. RUTE HALAMAN PUBLIK (PUBLIC PAGES)
// ========================================================================

// RUTE UTAMA: HOME PAGE GLOBAL
Route::get('/', [LandingPageController::class, 'index'])->name('home');

// --- RUTE SPESIFIK KAMPUS BERDASARKAN {slug} ---

// --- Sub-halaman "Tentang" ---
Route::get('/{slug}/sejarah', [SejarahController::class, 'show'])->name('campus.sejarah');
Route::get('/{slug}/visi-misi', [VisiMisiController::class, 'show'])->name('campus.visimisi');
Route::get('/{slug}/struktur-organisasi', [StrukturOrganisasiController::class, 'show'])->name('campus.struktur');

// --- Sub-halaman "Akademik" ---
Route::get('/{slug}/fasilitas', [FasilitasController::class, 'show'])->name('campus.fasilitas');
Route::get('/{slug}/perkuliahan', [SistemPerkuliahanController::class, 'show'])->name('campus.perkuliahan');
Route::get('/{slug}/kurikulum', [KurikulumController::class, 'show'])->name('campus.kurikulum');

// --- Sub-halaman "Biaya Kuliah & Info Lain" ---
Route::get('/{slug}/biaya', [BiayaJurusanController::class, 'show'])->name('campus.biaya');
Route::get('/{slug}/brosur', [BrosurController::class, 'show'])->name('campus.brosur');

// --- Sub-halaman "Galeri" ---
Route::get('/{slug}/galeri', [GalleryController::class, 'show'])->name('campus.galeri');

// --- Sub-halaman "Berita" ---
// 1. Rute Detail Berita
Route::get('/{slug}/berita/{id}', [NewsController::class, 'showDetail'])
    ->name('campus.berita.detail')
    ->where('id', '[0-9]+');
// 2. Rute Arsip Berita (Daftar semua berita)
Route::get('/{slug}/berita', [NewsController::class, 'show'])->name('campus.berita'); 

// --- Rute Kontak ---
Route::get('/{slug}/kontak', [LandingPageController::class, 'contactShow'])->name('campus.kontak');

// ========================================================================
// 3. RUTE PENDAFTARAN ONLINE (PO) 
// ========================================================================

// 1. Menampilkan Formulir Pendaftaran (GET)
Route::get('/{slug}/pendaftaran', [PendaftaranController::class, 'create'])->name('pendaftaran.create');

// 2. Memproses Pengiriman Formulir (POST)
Route::post('/{slug}/pendaftaran', [PendaftaranController::class, 'store'])->name('pendaftaran.store');

// 3. Halaman Konfirmasi Sukses Setelah Submit (GET)
Route::get('/{slug}/pendaftaran/sukses/{id}', [PendaftaranController::class, 'showSuccess'])->name('pendaftaran.sukses');


// ========================================================================
// 3. RUTE TRACER STUDY
// ========================================================================

// 1. Menampilkan Formulir Tracer Study (GET)
Route::get('/{slug}/tracer-study', [TracerStudyController::class, 'create'])->name('tracerStudy.create');

// 2. Memproses Pengiriman Formulir Tracer Study (POST)
Route::post('/{slug}/tracer-study', [TracerStudyController::class, 'store'])->name('tracerStudy.store');

// 3. Halaman Konfirmasi Sukses Setelah Submit (GET)
Route::get('/{slug}/tracer-study/sukses/{id}', [TracerStudyController::class, 'showSuccess'])->name('tracerStudy.sukses');

// 4. Halaman Detail Tracer Study Berdasarkan ID (GET)
Route::get('/{slug}/tracer-study/{id}', [TracerStudyController::class, 'show'])->name('tracerStudy.show');

// ========================================================================
// 4. RUTE Halaman Utama (Catch-All Kampus Spesifik)
// ========================================================================

Route::get('/{slug}', [LandingPageController::class, 'show'])
    ->where('slug', '[a-z0-9-]+')
    ->name('campus.show');
