<?php

use App\Http\Controllers\Frontend\CartController;
use App\Http\Controllers\Frontend\CategoryController;
use App\Http\Controllers\Frontend\CheckoutController;
use App\Http\Controllers\Frontend\HomeController;
use App\Http\Controllers\Frontend\NewsletterController;
use App\Http\Controllers\Frontend\OrderController;
use App\Http\Controllers\Frontend\PageController;
use App\Http\Controllers\Frontend\ProductController;
use App\Http\Controllers\Frontend\ReviewController;
use App\Http\Controllers\Frontend\ShopController;
use App\Http\Controllers\Frontend\WishlistController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Frontend (Storefront) Routes
|--------------------------------------------------------------------------
| Public-facing routes for the Niayarsa storefront.
*/

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

Route::get('categories', [CategoryController::class, 'index'])->name('categories.index');

Route::get('about', [PageController::class, 'about'])->name('pages.about');
Route::get('contact', [PageController::class, 'contact'])->name('pages.contact');
Route::post('contact', [PageController::class, 'sendContact'])->middleware('throttle:5,1')->name('pages.contact.send');

Route::post('newsletter/subscribe', [NewsletterController::class, 'store'])->middleware('throttle:5,1')->name('newsletter.subscribe');

Route::prefix('shop')->name('shop.')->group(function () {
    // Static segments must be registered before the {product} wildcard,
    // otherwise "new-arrivals" / "sale" would be swallowed as a slug.
    Route::get('/', [ShopController::class, 'index'])->name('index');
    Route::get('new-arrivals', [ShopController::class, 'newArrivals'])->name('new-arrivals');
    Route::get('sale', [ShopController::class, 'sale'])->name('sale');
    Route::get('{product}', [ProductController::class, 'show'])->name('product');
});

// Guest-friendly — cart works before login, matching the guest-checkout
// design from Step 2. Guest carts merge into the user's cart on login
// (MergeGuestCartOnLogin, Step 5).
Route::prefix('cart')->name('cart.')->group(function () {
    // Static segments (add/{product}, coupon) must be registered before
    // the {item} wildcard, otherwise "coupon" would be swallowed as an
    // item ID — same class of bug as the shop routes in Step 8.
    Route::get('/', [CartController::class, 'index'])->name('index');
    Route::post('add/{product}', [CartController::class, 'store'])->name('add');
    Route::post('coupon', [CartController::class, 'applyCoupon'])->name('coupon.apply');
    Route::delete('coupon', [CartController::class, 'removeCoupon'])->name('coupon.remove');
    Route::patch('{item}', [CartController::class, 'update'])->name('update');
    Route::delete('{item}', [CartController::class, 'destroy'])->name('remove');
});

Route::get('checkout', [CheckoutController::class, 'index'])->name('checkout.index');
Route::post('checkout', [CheckoutController::class, 'store'])->middleware('throttle:10,1')->name('checkout.store');
Route::post('checkout/buy-now/{product}', [CheckoutController::class, 'buyNow'])->name('checkout.buyNow');

// Guest-viewable — OrderController::show itself checks ownership or the
// "just placed this as a guest" session flash, since a guest has no
// account to authenticate against later.
Route::get('orders/{order}', [OrderController::class, 'show'])->name('orders.show');

Route::middleware('auth')->group(function () {
    Route::get('wishlist', [WishlistController::class, 'index'])->name('wishlist.index');
    Route::post('wishlist/{product}', [WishlistController::class, 'toggle'])->name('wishlist.toggle');

    Route::post('shop/{product}/reviews', [ReviewController::class, 'store'])->middleware('throttle:10,1')->name('reviews.store');

    Route::get('account/orders', [OrderController::class, 'accountIndex'])->name('account.orders');
});
