<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\CalendarController;
use App\Http\Controllers\AdminBookingController;
use App\Http\Controllers\IzinPenerimaanTamuController;
use App\Http\Controllers\AdminAuthController;
use App\Http\Controllers\SecurityAuthController;
use App\Http\Controllers\NotifikasiController;
use App\Http\Controllers\WebPushController;
use App\Http\Controllers\AppCodeController;


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

# ============================
# GERBANG KODE AKSES (GATE)
# ============================
Route::get('/gate/{app}', [AppCodeController::class, 'index'])->name('app.gate');
Route::post('/gate/verify', [AppCodeController::class, 'verify'])->name('app.gate.verify');


# ============================
# LOGIN & LOGOUT ADMIN (GENERIC)
# ============================
Route::get('/login', function () {
    return view('auth.login');
})->middleware('guest')->name('login');

Route::post('/login', function (Request $request) {
    $credentials = $request->only('email', 'password');
    if (Auth::attempt($credentials)) {
        $request->session()->regenerate();
        return redirect('/booking-admin/meeting_room_1/bookings');
    }
    return back()->withErrors(['email' => 'Email atau password salah.']);
});

Route::post('/logout', function () {
    Auth::logout();
    request()->session()->invalidate();
    request()->session()->regenerateToken();
    return redirect('/login');
})->name('logout');


# ============================
# HALAMAN UTAMA (INDEX)
# ============================
Route::get('/', function () {
    return view('index'); 
});


# ========================================================
# APLIKASI 1: CALENDAR (PROTECTED BY 8-DIGIT CODE)
# ========================================================
Route::middleware(['check.app.code:calendar'])->group(function () {
    
    # Daftar semua ruangan
    Route::get('/calendar', function () {
        return view('calendar.index');
    })->name('calendar.index');

    # Kalender tiap ruangan
    Route::get('/calendar/room/{room}', [CalendarController::class, 'showRoom'])
        ->where('room', '.*');

});


# ========================================================
# APLIKASI 2: IZIN TAMU (PROTECTED BY 8-DIGIT CODE)
# ========================================================
Route::middleware(['check.app.code:izin_tamu'])->group(function () {

    # Resource utama Izin Tamu
    Route::resource('izin_tamu', IzinPenerimaanTamuController::class)->except(['show']);

    # ✅ ROUTE BARCODE PENDEK (Scan via Token)
    Route::get('/s/{token}', [IzinPenerimaanTamuController::class, 'scanByToken'])->name('tamu.scan_token');

    # Safety Result & Export (Public/User)
    Route::get('/izin_tamu/{id}/safety', [IzinPenerimaanTamuController::class, 'safetyResult'])->name('izin_tamu.safety');
    Route::get('/izin_tamu/export_all_safety', [IzinPenerimaanTamuController::class, 'exportAllSafety'])->name('izin_tamu.export_all_safety');
    Route::get('/izin_tamu/{id}/export-pdf', [IzinPenerimaanTamuController::class, 'exportPdf'])->name('izin_tamu.export_pdf');
    Route::get('/izin_tamu/{id}/export-excel', [IzinPenerimaanTamuController::class, 'exportExcel'])->name('izin_tamu.export_excel');

    # QR Scan & Store Signature
    Route::post('/izin-tamu/{id}/checklist', [IzinPenerimaanTamuController::class, 'updateChecklist'])->name('izin_tamu.update_checklist');
    Route::get('/izin_tamu/{id}/scan', [IzinPenerimaanTamuController::class, 'scan'])->name('izin_tamu.scan');
    Route::post('/izin_tamu/{id}/scan', [IzinPenerimaanTamuController::class, 'storeScan'])->name('izin_tamu.storeScan');
    Route::post('/izin_tamu/{id}/signature', [IzinPenerimaanTamuController::class, 'storeSignature'])->name('izin_tamu.storeSignature');

    # Thanks Page
    Route::get('/izin_tamu/{id}/thanks', [IzinPenerimaanTamuController::class, 'thanks'])
        ->name('izin_tamu.thanks_page')
        ->middleware('no-back-history');

});


# ============================
# ADMIN BOOKING (Harus Login Admin)
# ============================
Route::prefix('booking-admin')->middleware(['auth'])->group(function () {
    Route::get('/{room}/bookings', [AdminBookingController::class, 'index'])->name('booking-admin.bookings.index');
    Route::get('/{room}/bookings/{id}/edit', [AdminBookingController::class, 'edit'])->name('booking-admin.bookings.edit');
    Route::put('/{room}/bookings/{id}', [AdminBookingController::class, 'update'])->name('booking-admin.bookings.update');
    Route::delete('/{room}/bookings/{id}', [AdminBookingController::class, 'destroy'])->name('booking-admin.bookings.destroy');
    Route::get('/{room}/bookings/export', [AdminBookingController::class, 'export'])->name('booking-admin.bookings.export');
});


# ==================================
# IZIN TAMU ADMIN (Harus Login Admin)
# ==================================
Route::prefix('admin')->middleware(['web'])->group(function () {
    Route::get('/login', [AdminAuthController::class, 'showLoginForm'])->middleware('guest:admin')->name('admin.login');
    Route::post('/login', [AdminAuthController::class, 'login'])->middleware('guest:admin');
    Route::get('/logout', [AdminAuthController::class, 'logout'])->name('admin.logout');

    Route::middleware('auth:admin')->group(function () {
        Route::get('/', [IzinPenerimaanTamuController::class, 'indexAdmin'])->name('admin.index');
        Route::get('/notif', [NotifikasiController::class, 'cekNotif'])->name('admin.notif');
        Route::get('/izin_tamu/admin/export-filter', [IzinPenerimaanTamuController::class, 'exportFilter'])->name('izin_tamu.admin.export_filter');
        Route::get('/admin/izin/export-excel', [IzinPenerimaanTamuController::class, 'exportExcelAdmin'])->name('izin_tamu.admin.export_excel');
    });
});


# ========================================
# IZIN TAMU SECURITY (Harus Login Security)
# ========================================
Route::prefix('security')->middleware(['web'])->group(function () {
    Route::get('/login', [SecurityAuthController::class, 'showLoginForm'])->name('security.login');
    Route::post('/login', [SecurityAuthController::class, 'login']);
    Route::get('/logout', [SecurityAuthController::class, 'logout'])->name('security.logout');

    Route::middleware('auth:security')->group(function () {
        Route::get('/dashboard', [IzinPenerimaanTamuController::class, 'indexSecurity'])->name('izin_tamu.index_security');
        Route::get('/refresh', [IzinPenerimaanTamuController::class, 'refreshSecurityData'])->name('security.refresh');
        Route::get('/izin_tamu/{id}/barcode', [IzinPenerimaanTamuController::class, 'showBarcode'])->name('security.izin_tamu.barcode');
        Route::post('/izin-tamu/{id}/update-security', [IzinPenerimaanTamuController::class, 'updateSecurity'])->name('izin_tamu.update_security');    
    });
});

# Push Notification (Umum)
Route::post('/webpush/subscribe', [WebPushController::class, 'subscribe'])->name('webpush.subscribe');