<?php

use App\Http\Controllers\adminController;
use App\Http\Controllers\PaymentController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\UserController;
use App\Models\payment;
use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Support\Facades\Route;

// Route::get('/', function () {
//     return view('welcome');
// });
Route::get('/', [adminController::class, 'first'])->name('index');

Route::get('/ticket', [adminController::class, 'welcome'])->name('welcome');

Route::get('/dashboard', function () {
    return view('dashboard');
})->middleware(['auth', 'verified'])->name('dashboard');

Route::middleware('auth')->group(function () {
    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');
});

// admin middleware
Route::middleware(['auth', 'admin'])->group(function () {
    // Ticket
    Route::get('admin/dashboard', [adminController::class, 'list'])->name('admin.list');
    Route::get('admin/ticket', [adminController::class, 'index'])->name('admin.dashboard');
    Route::get('admin/create-new-ticket', [adminController::class, 'create'])->name('admin.create');
    Route::post('admin/ticket', [adminController::class, 'store'])->name('admin.store');
    Route::get('admin/ticket/edit/{ticket}', [adminController::class, 'edit'])->name('admin.edit');
    Route::put('admin/ticket/update/{ticket}', [adminController::class, 'update'])->name('admin.update');
    Route::delete('admin/ticket/{ticket}', [adminController::class, 'destroy'])->name('admin.destroy');

    // User
    Route::get('admin/user', [UserController::class, 'index'])->name('admin.user');
    Route::get('admin/user/edit{user}', [UserController::class, 'edit'])->name('admin.edit_user');
    Route::put('admin/user/update{user}', [UserController::class, 'update'])->name('users.update');
    Route::delete('admin/user/{user}', [UserController::class, 'destroy'])->name('users.destroy');
    Route::get('users/create', [UserController::class, 'create'])->name('admin.create_user');
    Route::post('users', [UserController::class, 'store'])->name('users.store');

    // check_payment
    Route::get('admin/check-payment', [PaymentController::class, 'index'])->name('payment.index');
});

Route::get('/payment/create/{ticketId}', [PaymentController::class, 'create'])->name('payment.create');

Route::post('/payment/store', [PaymentController::class, 'store'])->name('payment.store');

// Route to display receipt page for review
Route::match(['get', 'post'], '/payment/receipt/{paymentId}', [PaymentController::class, 'receipt'])->name('payment.receipt');

Route::get('/payment/confirm/', [PaymentController::class, 'confirm'])->name('payment.confirm');

// Route::get('/payment/download-qr-code/{paymentId}', [PaymentController::class, 'downloadQrCodePdf'])->name('payment.downloadQrCodePdf');
Route::get('/download-ticket/{payment}', function (payment $payment) {
    $ticket = $payment::find($payment->ticket_id);

    $pdf = Pdf::loadView('payments.qrcode_pdf', [
        'payment' => $payment,
        'ticket' => $ticket
    ]);

    return $pdf->download('e-ticket-' . $payment->id . '.pdf');
})->name('ticket.download');

Route::put('/admin/toggle-availability/{ticket}', [AdminController::class, 'toggleAvailability'])->name('admin.toggleAvailability');

Route::get('/payment/qrcode/{tranId}', [PaymentController::class, 'showQrByTranId'])
    ->name('payment.qrcode.byTran');

Route::get('/payment/download-ticket/{paymentId}', [PaymentController::class, 'downloadTicketPDF'])
    ->name('payment.downloadTicket');


// 📥 Pushback & Verification (API Routes)
Route::post('/payment/pushback', [PaymentController::class, 'pushback'])->name('payment.pushback');
Route::get('/payment/verify-pending', [PaymentController::class, 'verifyPendingPayments'])->name('payment.verifyPending');

require __DIR__ . '/auth.php';
