<?php

use App\Http\Controllers\Auth\AuthenticatedSessionController;
use App\Http\Controllers\Backend\DashboardController;
use App\Http\Controllers\Backend\ContactMessageController;
use App\Http\Controllers\Backend\ContactSettingController;
use App\Http\Controllers\Backend\FoodCartController;
use App\Http\Controllers\Backend\FoodMenuItemController;
use App\Http\Controllers\Backend\OrderController;
use App\Http\Controllers\Backend\ProductController;
use App\Http\Controllers\Backend\ReelController;
use App\Http\Controllers\Backend\RolePermissionController;
use App\Http\Controllers\Backend\ShopController;
use App\Http\Controllers\Frontend\StorefrontController;
use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;

Route::get('/', [StorefrontController::class, 'index'])->name('storefront.home');
Route::get('/shops/{shop}', [StorefrontController::class, 'shop'])->name('storefront.shops.show');
Route::get('/shops/{shop}/landing', [StorefrontController::class, 'shopLanding'])->name('storefront.shops.landing');
Route::get('/products/{product}', [StorefrontController::class, 'productDetails'])->name('storefront.products.show');
Route::get('/reels', [StorefrontController::class, 'reels'])->name('storefront.reels');
Route::get('/about', [StorefrontController::class, 'about'])->name('storefront.about');
Route::get('/contact', [StorefrontController::class, 'contact'])->name('storefront.contact');
Route::post('/contact/message', [StorefrontController::class, 'storeContactMessage'])->name('storefront.contact.message');
Route::post('/reels/{reel}/view', [StorefrontController::class, 'incrementReelView'])->name('storefront.reels.view');
Route::post('/products/{product}/add-to-cart', [StorefrontController::class, 'addToCart'])->name('storefront.cart.add');
Route::get('/cart', [StorefrontController::class, 'cart'])->name('storefront.cart');
Route::post('/cart/items/{product}/quantity', [StorefrontController::class, 'updateCartQuantity'])->name('storefront.cart.quantity');
Route::post('/cart/checkout-cod', [StorefrontController::class, 'checkoutCod'])->name('storefront.checkout.cod');
Route::get('/food-carts', [StorefrontController::class, 'foodCarts'])->name('storefront.food-carts.index');
Route::get('/food-carts/{foodCart}', [StorefrontController::class, 'foodCartShow'])->name('storefront.food-carts.show');
Route::get('/food-carts/{foodCart}/search-items', [StorefrontController::class, 'searchFoodCartItems'])->name('storefront.food-carts.search-items');

//* terms and conditions and return policy routes */
Route::get('/terms', [StorefrontController::class, 'terms'])->name('storefront.terms');
Route::get('/returns', [StorefrontController::class, 'returns'])->name('storefront.returns');


Route::get('/dashboard', function () {
    return redirect()->route('admin.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');
    Route::post('/logout', [AuthenticatedSessionController::class, 'destroy'])
        ->name('logout');
});

// ** BACKEND ROUTES */
// Super-admin and authorized users can access backend sections using Spatie permissions.
Route::prefix('admin/')->name('admin.')->middleware(['auth'])->group(function () {

    Route::get('dashboard', [DashboardController::class, 'index'])->name('dashboard');

    //** ROLE AND PERMISSIONS */
    Route::prefix('roles-permissions')->name('roles-permissions.')->middleware([
        'permission:view_roles|create_roles|edit_roles|delete_roles|view_permissions|create_permissions|edit_permissions|delete_permissions|view_users|create_users|edit_users|delete_users',
    ])->group(function () {
        Route::get('/',                              [RolePermissionController::class, 'index'])->name('index');
        Route::post('/store',                        [RolePermissionController::class, 'store'])->name('store');
        Route::get('/{role}/edit',                   [RolePermissionController::class, 'edit'])->name('edit');
        Route::put('/{role}/update',                 [RolePermissionController::class, 'update'])->name('update');
        Route::delete('/{role}/destroy',             [RolePermissionController::class, 'destroy'])->name('destroy');

        // Permissions within roles
        Route::get('/{role}/permissions',            [RolePermissionController::class, 'getPermissions'])->name('permissions');
        Route::post('/{role}/assign-permissions',    [RolePermissionController::class, 'assignPermissions'])->name('assign-permissions');

        // Users within roles
        Route::get('/{role}/users',                  [RolePermissionController::class, 'getUsers'])->name('users');
        Route::post('/{role}/assign-users',          [RolePermissionController::class, 'assignUsers'])->name('assign-users');
    });

    //** SHOPS */
    // Authorization handled by ShopPolicy - staff assigned to shops can manage them
    Route::resource('shops', ShopController::class);
    Route::get('shops/{shop}/report', [ShopController::class, 'report'])->name('shops.report');
    Route::get('/users/search', [ShopController::class, 'searchUsers'])->name('users.search');

    //** FOOD CARTS */
    Route::resource('food-carts', FoodCartController::class)->except('show');
    Route::prefix('food-carts/{foodCart}/items')->name('food-carts.items.')->group(function () {
        Route::get('/', [FoodMenuItemController::class, 'index'])->name('index');
        Route::get('/create', [FoodMenuItemController::class, 'create'])->name('create');
        Route::post('/', [FoodMenuItemController::class, 'store'])->name('store');
        Route::get('/{item}/edit', [FoodMenuItemController::class, 'edit'])->name('edit');
        Route::put('/{item}', [FoodMenuItemController::class, 'update'])->name('update');
        Route::delete('/{item}', [FoodMenuItemController::class, 'destroy'])->name('destroy');
    });

    //** STAFF ASSIGNMENT */
    Route::get('staff-assignment', [ShopController::class, 'staffAssignment'])->name('staff-assignment.index')->middleware('role:super-admin');
    Route::post('staff-assignment', [ShopController::class, 'storeStaffAssignment'])->name('staff-assignment.store')->middleware('role:super-admin');
    Route::post('staff-assignment/assign-shops', [ShopController::class, 'assignShopsToUser'])->name('staff-assignment.assign-shops')->middleware('role:super-admin');
    Route::post('staff-assignment/create-user', [ShopController::class, 'createStaffUser'])->name('staff-assignment.create-user')->middleware('role:super-admin');
    Route::get('staff-assignment/shops-search', [ShopController::class, 'searchShopsForStaffAssignment'])->name('staff-assignment.shops-search')->middleware('role:super-admin');
    Route::get('staff-assignment/users/{user}/shops', [ShopController::class, 'getUserShopAssignments'])->name('staff-assignment.users.shops')->middleware('role:super-admin');
    Route::post('staff-assignment/users/{user}/shops', [ShopController::class, 'updateUserShopAssignments'])->name('staff-assignment.users.shops.update')->middleware('role:super-admin');
    Route::delete('staff-assignment/users/{user}', [ShopController::class, 'deleteStaffUser'])->name('staff-assignment.users.destroy')->middleware('role:super-admin');

    //** PRODUCTS */
    // Authorization handled by ProductPolicy - staff assigned to shops can manage products
    Route::prefix('shops/{shop}/products')->name('shops.products.')->group(function () {
        Route::get('/',                              [ProductController::class, 'index'])->name('index');
        Route::get('/create',                        [ProductController::class, 'create'])->name('create');
        Route::post('/',                             [ProductController::class, 'store'])->name('store');
        Route::get('/{product}',                     [ProductController::class, 'show'])->name('show');
        Route::get('/{product}/edit',                [ProductController::class, 'edit'])->name('edit');
        Route::put('/{product}',                     [ProductController::class, 'update'])->name('update');
        Route::delete('/{product}',                  [ProductController::class, 'destroy'])->name('destroy');
    });

    //** REELS */
    Route::resource('reels', ReelController::class)
        ->except('show')
        ->middleware('role:super-admin');
    Route::get('reels/shops/search', [ReelController::class, 'searchShops'])
        ->name('reels.shops.search')
        ->middleware('role:super-admin');

    //** ORDERS */
    Route::get('orders', [OrderController::class, 'index'])->name('orders.index');
    Route::get('orders/pending-count', [OrderController::class, 'pendingCount'])->name('orders.pending-count');
    Route::get('orders/{order}', [OrderController::class, 'show'])->name('orders.show');
    Route::put('orders/{order}/status', [OrderController::class, 'updateStatus'])->name('orders.update-status');

    //** CONTACT MESSAGES */
    Route::get('contact-messages', [ContactMessageController::class, 'index'])->name('contact-messages.index');
    Route::delete('contact-messages/{message}', [ContactMessageController::class, 'destroy'])->name('contact-messages.destroy');

    //** CONTACT SETTINGS */
    Route::get('contact-settings', [ContactSettingController::class, 'edit'])->name('contact-settings.edit');
    Route::post('contact-settings', [ContactSettingController::class, 'update'])->name('contact-settings.update');
});

require __DIR__ . '/auth.php';
