<?php
// Ragisha hanny azalia putri //
// 24.12.3182 //

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\WelcomeController;
use App\Http\Controllers\EventController;
use App\Http\Controllers\CheckoutController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\EventController as EventAdminController;
use App\Http\Controllers\Admin\AuthController;
use App\Http\Controllers\Admin\TransactionController;
use App\Http\Controllers\Admin\PartnerController;
use App\Http\Controllers\Admin\CategoryController;
use App\Http\Controllers\Admin\OversightController;
use Laravel\Socialite\Facades\Socialite;
use App\Http\Controllers\ReviewController;
use App\Http\Controllers\PartnerProfileController;
use App\Http\Controllers\Partner\AnalyticsController;
use App\Http\Controllers\GoogleSocialiteController;
use App\Http\Controllers\CheckInController;
use App\Http\Controllers\TicketScannerController;
use App\Http\Controllers\CertificateController;
use App\Mail\SendTicketMail;
use App\Models\Transaction; // IMPORT MODEL TRANSACTION BIAR TIDAK ERROR
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\AttendanceController;

/*
|--------------------------------------------------------------------------
| 1. RUTE PUBLIK & USER BIAYA
|--------------------------------------------------------------------------
*/
Route::get('/', WelcomeController::class)->name('home');

// Rute Detail Event
Route::get('/event/{event}', [EventController::class, 'show'])->name('events.show');
Route::get('/my-ticket', [EventController::class, 'ticket'])->name('ticket');

// Rute Halaman Statis Extra
Route::get('/tentang', function () { return '<h1>Ini adalah Halaman Tentang Aplikasi Event Hub</h1>'; });
Route::get('/kontak', function () { return view('contact'); });
Route::get('/profile', function () { return view('profile'); });
Route::get('/katalog', function () { return view('katalog'); });
Route::get('/bantuan', function () { return view('bantuan'); });

// --- ALUR CHECKOUT MIDTRANS ---
Route::get('/checkout/create/{event}', [CheckoutController::class, 'create'])->name('checkout.create');
Route::post('/checkout/store/{event}', [CheckoutController::class, 'store'])->name('checkout.store');
Route::get('/checkout/payment/{order_id}', [CheckoutController::class, 'payment'])->name('checkout.payment');
Route::get('/checkout/success/{order_id}', [CheckoutController::class, 'success'])->name('checkout.success');

// 🛠️ PERBAIKAN: Mengubah redirect agar tidak memaksa masuk ke admin.login saat user biasa berhasil login Google
Route::get('/login', function () {
    return redirect('/');
})->name('login');


/*
|--------------------------------------------------------------------------
| 2. RUTE AUTENTIKASI PARTNER (Bisa diakses tanpa login)
|--------------------------------------------------------------------------
*/
Route::prefix('partner')->name('partner.')->group(function () {
    Route::get('/login', [\App\Http\Controllers\Partner\AuthController::class, 'showLogin'])->name('login');
    Route::post('/login', [\App\Http\Controllers\Partner\AuthController::class, 'login'])->name('login.post');
    Route::get('/register', [\App\Http\Controllers\Partner\AuthController::class, 'showRegister'])->name('register');
    Route::post('/register', [\App\Http\Controllers\Partner\AuthController::class, 'register'])->name('register.post');
    Route::post('/logout', [\App\Http\Controllers\Partner\AuthController::class, 'logout'])->name('logout');
});


/*
|--------------------------------------------------------------------------
| 3. RUTE PROTEKSI PARTNER (Hanya bisa diakses setelah login)
|--------------------------------------------------------------------------
*/
Route::middleware(['partner'])->prefix('partner')->name('partner.')->group(function () {
    Route::get('/dashboard', [\App\Http\Controllers\Partner\DashboardController::class, 'index'])->name('dashboard');
    Route::get('/analytics', [AnalyticsController::class, 'index'])->name('analytics');

    Route::get('/events', [\App\Http\Controllers\Partner\EventController::class, 'index'])->name('events.index');
    Route::get('/events/create', [\App\Http\Controllers\Partner\EventController::class, 'create'])->name('events.create');
    Route::post('/events', [\App\Http\Controllers\Partner\EventController::class, 'store'])->name('events.store');
    Route::get('/events/{event}/edit', [\App\Http\Controllers\Partner\EventController::class, 'edit'])->name('events.edit');
    Route::put('/events/{event}', [\App\Http\Controllers\Partner\EventController::class, 'update'])->name('events.update');
    Route::delete('/events/{event}', [\App\Http\Controllers\Partner\EventController::class, 'destroy'])->name('events.destroy');
});


/*
|--------------------------------------------------------------------------
| 4. RUTE AUTENTIKASI ADMIN (Bisa diakses tanpa login)
|--------------------------------------------------------------------------
*/
Route::prefix('admin')->name('admin.')->group(function () {
    Route::get('/login', [AuthController::class, 'showLogin'])->name('login');
    Route::post('/login', [AuthController::class, 'login'])->name('login.post');
    Route::get('/auth/google', [AuthController::class, 'redirectToGoogle'])->name('google.login');
    Route::get('/auth/google/callback', [AuthController::class, 'handleGoogleCallback'])->name('google.callback');
    Route::post('/logout', [AuthController::class, 'logout'])->name('logout');
});


/*
|--------------------------------------------------------------------------
| 5. RUTE PROTEKSI ADMIN (Hanya bisa diakses jika sudah login)
|--------------------------------------------------------------------------
*/
Route::middleware(['auth'])->prefix('admin')->name('admin.')->group(function () {
    Route::get('/', [DashboardController::class, 'index'])->name('dashboard');
    Route::get('/dashboard', [DashboardController::class, 'index']); 

    Route::resource('events', EventAdminController::class);
    Route::resource('partners', PartnerController::class);
    Route::post('/partners/{partner}/approve', [PartnerController::class, 'approve'])->name('partners.approve');
    Route::post('/partners/{partner}/suspend', [PartnerController::class, 'suspend'])->name('partners.suspend');
    Route::get('/oversight', [OversightController::class, 'index'])->name('oversight');
    Route::resource('kategori', CategoryController::class)->names('categories');
    Route::get('transactions', [TransactionController::class, 'index'])->name('transactions.index');
});

Route::post('/midtrans/callback', [\App\Http\Controllers\MidtransWebhookController::class, 'handle']);
Route::post('/review/{event}', [ReviewController::class,'store'])->name('review.store');

// Rute Profil Penyelenggara (Partner)
Route::get('/partner/{partner}', [PartnerProfileController::class, 'show'])->name('partner.profile');

/*
|--------------------------------------------------------------------------
| 6. RUTE GOOGLE SSO UNTUK USER PUBLIK
|--------------------------------------------------------------------------
*/
Route::get('/auth/google', [GoogleSocialiteController::class, 'redirect'])->name('google.redirect');
Route::get('/auth/google/callback', [GoogleSocialiteController::class, 'callback'])->name('google.callback');

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

Route::get('/certificate', [CertificateController::class, 'index'])->name('certificate.index');
Route::get('/certificate/preview', [CertificateController::class, 'preview'])->name('certificate.preview');
Route::get('/certificate/download', [CertificateController::class, 'download'])->name('certificate.download');
Route::get('/scan-tiket', [TicketScannerController::class, 'index'])->name('scan.index');
Route::post('/scan-tiket', [TicketScannerController::class, 'check'])->name('scan.check');

/*
|--------------------------------------------------------------------------
| 7. RUTE JALUR DARURAT SIMULASI EMAIL & QR CODE (ANTI-404)
|--------------------------------------------------------------------------
*/
Route::get('/test-kirim-email', function () {
    // Cari data transaksi terakhir yang terdaftar di database kamu
    $transaction = Transaction::with('event')->latest()->first();
    
    if (!$transaction) {
        return "Database transaksi kosong. Silakan beli tiket 1 kali dulu di web agar ada datanya!";
    }
    
    // Paksa ubah status jadi success & simpan
    $transaction->status = 'success';
    $transaction->save();
    
    // Kirim email e-ticket
    Mail::to($transaction->customer_email)->send(new SendTicketMail($transaction));
    
    return "🔥 BOOM! Status Transaksi [" . $transaction->order_id . "] berhasil di-SET ke SUCCESS. Email E-Ticket berisi gambar QR Code resmi sudah meluncur ke Mailtrap Sandbox!";
});

Route::get('/attendance/scan/{order_id}', [AttendanceController::class, 'scan'])->name('attendance.scan');