<?php

use App\Http\Controllers\GalleryController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\PackageController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\RevolutWebhookController;
use App\Http\Middleware\EnsureProfileIsComplete;
use App\Livewire\BookingFlow;
use App\Livewire\Checkout;
use App\Livewire\MixMasterOrder;
use App\Livewire\PackageCheckout;
use App\Livewire\ServiceOrderPage;
use App\Models\Page;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Public
|--------------------------------------------------------------------------
*/

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

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

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

/*
 * Content the studio edits itself: studio rules, terms, privacy. Registered
 * last among the public routes so a page slug can never shadow a real one.
 */
Route::get('/p/{page:slug}', function (Page $page) {
    abort_unless($page->is_published, 404);

    return view('pages.show', ['page' => $page]);
})->name('page');

/*
|--------------------------------------------------------------------------
| Payment callbacks
|--------------------------------------------------------------------------
|
| Revolut posts here. No session, no CSRF token, no authentication — the
| signature on the request is the authentication, which is why the controller
| verifies it before reading a single field.
|
| The path goes into the Revolut dashboard once and must not change, or
| deliveries stop arriving and the only symptom is bookings quietly failing
| to confirm.
|
*/

Route::post('/webhooks/revolut', RevolutWebhookController::class)
    ->name('webhooks.revolut')
    ->withoutMiddleware(['web']);

/*
|--------------------------------------------------------------------------
| Artist area
|--------------------------------------------------------------------------
|
| Everything past sign-in. The profile gate sits in front of booking rather
| than in front of the whole area, so a new customer can look around their
| account before being asked for a date of birth.
|
*/

Route::middleware('auth')->group(function () {
    Route::view('/account', 'account.dashboard')->name('account');

    Route::get('/account/profile', [ProfileController::class, 'edit'])
        ->name('profile.edit');
    Route::patch('/account/profile', [ProfileController::class, 'update'])
        ->name('profile.update');

    Route::get('/book', BookingFlow::class)
        ->middleware(EnsureProfileIsComplete::class)
        ->name('book');

    Route::get('/checkout/{booking:reference}', Checkout::class)
        ->middleware('can:view,booking')
        ->name('checkout');

    Route::post('/packages/{package:slug}/buy', [PackageController::class, 'buy'])
        ->name('package.buy');

    Route::get('/packages/checkout/{purchase:reference}', PackageCheckout::class)
        ->middleware('can:view,purchase')
        ->name('package.checkout');

    Route::get('/mix-master', MixMasterOrder::class)
        ->middleware(EnsureProfileIsComplete::class)
        ->name('mix');

    Route::get('/mix-master/{order:reference}', ServiceOrderPage::class)
        ->middleware('can:view,order')
        ->name('mix.order');
});

require __DIR__.'/auth.php';
