<?php

use App\Http\Controllers\AuditLogController;
use App\Http\Controllers\CheckoutController;
use App\Http\Controllers\CheckoutPaymentController;
use App\Http\Controllers\Dashboard\CatalogCategoryController;
use App\Http\Controllers\Dashboard\CatalogPendingReviewController;
use App\Http\Controllers\Dashboard\CatalogVenueController;
use App\Http\Controllers\Dashboard\EventAttendeesController;
use App\Http\Controllers\Dashboard\EventFeaturedImageUploadController;
use App\Http\Controllers\Dashboard\OrganizerAccountStatusController;
use App\Http\Controllers\Dashboard\OrganizerApprovalController;
use App\Http\Controllers\Dashboard\AccountProfileController;
use App\Http\Controllers\Dashboard\MyEventsController;
use App\Http\Controllers\Dashboard\OrganizerProfileController;
use App\Http\Controllers\Dashboard\PaymentsController;
use App\Http\Controllers\Dashboard\PlatformFeeSettingsController;
use App\Http\Controllers\Dashboard\UsersController;
use App\Http\Controllers\Dashboard\VenueController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\EventController;
use App\Http\Controllers\LandingController;
use App\Http\Controllers\RegisterOrganizerController;
use App\Http\Controllers\Webhooks\PaypalWebhookController;
use App\Http\Controllers\Webhooks\StripeWebhookController;
use App\Http\Middleware\AuditHttpActivity;
use App\Http\Middleware\OrganizerApproved;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;

Route::get('/', [LandingController::class, 'index'])->name('landing');

Route::get('/eventos', [LandingController::class, 'events'])->name('landing.events');
Route::post('/eventos', [LandingController::class, 'events']);
Route::get('/eventos/{event:slug}', [LandingController::class, 'eventShow'])->name('landing.event.show');
Route::post('/eventos/{event:slug}/checkout', [CheckoutController::class, 'store'])
    ->middleware('throttle:checkout.store')
    ->name('landing.event.checkout');
Route::get('/eventos/{event:slug}/confirmacion/{checkoutOrder}', [CheckoutController::class, 'confirmation'])
    ->name('landing.event.checkout.confirmation');
Route::post('/eventos/{event:slug}/pago/{checkoutOrder}/stripe/intent', [CheckoutPaymentController::class, 'createStripePaymentIntent'])
    ->middleware('throttle:checkout.payment')
    ->name('landing.event.checkout.stripe.intent');
Route::post('/eventos/{event:slug}/pago/{checkoutOrder}/paypal/order', [CheckoutPaymentController::class, 'createPaypalOrder'])
    ->middleware('throttle:checkout.payment')
    ->name('landing.event.checkout.paypal.order');
Route::post('/eventos/{event:slug}/pago/{checkoutOrder}/paypal/capture', [CheckoutPaymentController::class, 'capturePaypalOrder'])
    ->middleware('throttle:checkout.payment')
    ->name('landing.event.checkout.paypal.capture');

Route::view('/organizador', 'landing.organizador')->name('organizador');

Route::middleware('guest')->group(function (): void {
    Route::get('/registro-organizador', [RegisterOrganizerController::class, 'create'])->name('register.organizer');
    Route::post('/registro-organizador', [RegisterOrganizerController::class, 'store'])->name('register.organizer.store');
});

Route::view('/registro-organizador/recibido', 'auth.register-organizer-thanks')->name('register.organizer.thanks');

Auth::routes();

Route::get('/home', static fn () => redirect()->route('dashboard'))->middleware('auth')->name('home');

/*
|--------------------------------------------------------------------------
| Panel (auth + traza HTTP en auditoría)
|--------------------------------------------------------------------------
| `AuditHttpActivity` registra cada petición autenticada bajo `/dashboard*` al finalizar (ver AuditLogger).
*/
Route::middleware(['auth', 'throttle:global', AuditHttpActivity::class])->group(function (): void {
    Route::get('/dashboard/organizer/cuenta', OrganizerAccountStatusController::class)
        ->name('dashboard.organizer.account-status');

    Route::middleware([OrganizerApproved::class])->group(function (): void {
        Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

        Route::prefix('dashboard')->name('dashboard.')->group(function (): void {
            Route::post('uploads/event-featured', [EventFeaturedImageUploadController::class, 'store'])->name('uploads.event-featured');
            Route::post('catalog/venues', [CatalogVenueController::class, 'store'])->name('catalog.venues.store');
            Route::post('catalog/categories', [CatalogCategoryController::class, 'store'])->name('catalog.categories.store');
            Route::get('catalog/review', [CatalogPendingReviewController::class, 'index'])->name('catalog.review.index');
            Route::post('catalog/venues/{venue}/approve', [CatalogPendingReviewController::class, 'approveVenue'])->name('catalog.venues.approve');
            Route::post('catalog/categories/{category}/approve', [CatalogPendingReviewController::class, 'approveCategory'])->name('catalog.categories.approve');
            Route::get('events/{event}/asistentes', [EventAttendeesController::class, 'index'])
                ->name('events.attendees.index');
            Route::get('events/{event}/asistentes/datos', [EventAttendeesController::class, 'data'])
                ->name('events.attendees.data');
            Route::post('events/{event}/asistentes/buscar', [EventAttendeesController::class, 'lookup'])
                ->middleware('throttle:90,1')
                ->name('events.attendees.lookup');
            Route::post('events/{event}/asistentes/escaneo', [EventAttendeesController::class, 'scan'])
                ->middleware('throttle:90,1')
                ->name('events.attendees.scan');
            Route::post('events/{event}/asistentes/{attendee}/check-in', [EventAttendeesController::class, 'checkIn'])
                ->middleware('throttle:120,1')
                ->name('events.attendees.check-in');
            Route::post('events/{event}/asistentes/{attendee}/undo-check-in', [EventAttendeesController::class, 'undoCheckIn'])
                ->middleware('throttle:120,1')
                ->name('events.attendees.undo-check-in');

            Route::post('venues/{id}/restore', [VenueController::class, 'restore'])
                ->whereNumber('id')
                ->name('venues.restore');
            Route::resource('venues', VenueController::class)->parameters(['venues' => 'venue']);

            Route::post('events/{event}/cancel', [EventController::class, 'cancel'])->name('events.cancel');
            Route::post('events/{id}/restore', [EventController::class, 'restore'])
                ->whereNumber('id')
                ->name('events.restore');
            Route::resource('events', EventController::class)->except(['show']);
            Route::get('audit-logs', [AuditLogController::class, 'index'])->name('audit-logs.index');
            Route::get('payments', [PaymentsController::class, 'index'])->name('payments.index');
            Route::get('payments/{order}/detail', [PaymentsController::class, 'detail'])->name('payments.detail');
            Route::post('payments/{order}/resend-tickets', [PaymentsController::class, 'resendTickets'])
                ->middleware('throttle:12,1')
                ->name('payments.resend-tickets');

            Route::middleware('can:manage-platform-settings')->group(function (): void {
                Route::get('settings/platform-fees', [PlatformFeeSettingsController::class, 'edit'])->name('settings.platform-fees');
                Route::put('settings/platform-fees', [PlatformFeeSettingsController::class, 'update'])->name('settings.platform-fees.update');
            });

            Route::get('organizers', [OrganizerApprovalController::class, 'index'])->name('organizers.index');
            Route::post('organizers/manual', [OrganizerApprovalController::class, 'storeManual'])->name('organizers.store-manual');
            Route::post('organizers/{user}/approve', [OrganizerApprovalController::class, 'approve'])->name('organizers.approve');
            Route::post('organizers/{user}/reject', [OrganizerApprovalController::class, 'reject'])->name('organizers.reject');
            Route::post('organizers/{user}/deactivate', [OrganizerApprovalController::class, 'deactivate'])->name('organizers.deactivate');
            Route::post('organizers/{user}/activate', [OrganizerApprovalController::class, 'activate'])->name('organizers.activate');
            Route::post('organizers/{user}/archive', [OrganizerApprovalController::class, 'archive'])->name('organizers.archive');
            Route::post('organizers/{id}/restore', [OrganizerApprovalController::class, 'restore'])
                ->whereNumber('id')
                ->name('organizers.restore');

            Route::get('users', [UsersController::class, 'index'])->name('users.index');
            Route::post('users', [UsersController::class, 'store'])->name('users.store');
            Route::get('users/{user}/edit', [UsersController::class, 'edit'])->name('users.edit');
            Route::put('users/{user}', [UsersController::class, 'update'])->name('users.update');
            Route::get('cuenta/perfil', [AccountProfileController::class, 'edit'])->name('account.profile');
            Route::put('cuenta/perfil', [AccountProfileController::class, 'update'])->name('account.profile.update');
            Route::post('cuenta/perfil/foto', [AccountProfileController::class, 'updateAvatar'])->name('account.profile.avatar');
            Route::delete('cuenta/perfil/foto', [AccountProfileController::class, 'destroyAvatar'])->name('account.profile.avatar.destroy');
            Route::put('cuenta/perfil/contrasena', [AccountProfileController::class, 'updatePassword'])->name('account.profile.password');
            Route::get('mis-eventos', [MyEventsController::class, 'index'])->name('my-events.index');

            Route::get('profile', [OrganizerProfileController::class, 'edit'])->name('profile');
            Route::put('profile', [OrganizerProfileController::class, 'update'])->name('profile.update');
        });
    });
});

/*
|--------------------------------------------------------------------------
| Webhooks de pago (Stripe/PayPal)
|--------------------------------------------------------------------------
| Importante: deben completar órdenes SOLO via webhook para evitar fraude.
*/
Route::post('/webhooks/stripe', StripeWebhookController::class)->name('webhooks.stripe');
Route::post('/webhooks/paypal', PaypalWebhookController::class)->name('webhooks.paypal');
