<?php

use App\Http\Controllers\Admin\AuthController;
use App\Http\Controllers\Admin\CategoryController;
use App\Http\Controllers\Admin\CouponController;
use App\Http\Controllers\Admin\CustomerController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\DeliveryAgentController;
use App\Http\Controllers\Admin\DeliveryOrderController;
use App\Http\Controllers\Admin\InventoryController;
use App\Http\Controllers\Admin\OrderController;
use App\Http\Controllers\Admin\PageController;
use App\Http\Controllers\Admin\ProductController;
use App\Http\Controllers\Admin\SettingsController;
use App\Http\Controllers\Admin\SliderController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Admin Routes
|--------------------------------------------------------------------------
*/

Route::prefix('admin')->name('admin.')->group(function () {
    // Guest routes
    Route::middleware('guest')->group(function () {
        Route::get('/login', [AuthController::class, 'showLoginForm'])->name('login');
        Route::post('/login', [AuthController::class, 'login']);
    });

    // Protected admin routes
    Route::middleware(['auth', 'admin.or.subadmin'])->group(function () {
        Route::post('/logout', [AuthController::class, 'logout'])->name('logout');

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

        // Categories
        Route::resource('categories', CategoryController::class);

        // Products
        Route::resource('products', ProductController::class);
        Route::post('products/{product}/variants', [ProductController::class, 'storeVariant'])->name('products.variants.store');
        Route::put('products/variants/{variant}', [ProductController::class, 'updateVariant'])->name('products.variants.update');
        Route::delete('products/images/{image}', [ProductController::class, 'deleteImage'])->name('products.images.delete');
        Route::delete('products/{product}/video', [ProductController::class, 'deleteVideo'])->name('products.video.delete');

        // Orders
        Route::get('orders', [OrderController::class, 'index'])->name('orders.index');
        Route::get('orders/{order}', [OrderController::class, 'show'])->name('orders.show');
        Route::put('orders/{order}/status', [OrderController::class, 'updateStatus'])->name('orders.status.update');
        Route::put('orders/{order}/assign', [OrderController::class, 'assign'])->name('orders.assign');
        Route::get('orders/{order}/invoice', [OrderController::class, 'invoice'])->name('orders.invoice');

        // Customers
        Route::get('customers', [CustomerController::class, 'index'])->name('customers.index');
        Route::get('customers/{customer}', [CustomerController::class, 'show'])->name('customers.show');
        Route::put('customers/{customer}/status', [CustomerController::class, 'updateStatus'])->name('customers.status.update');

        // Inventory
        Route::get('inventory', [InventoryController::class, 'index'])->name('inventory.index');
        Route::put('inventory/{variant}/stock', [InventoryController::class, 'updateStock'])->name('inventory.stock.update');
        Route::get('inventory/{variant}/history', [InventoryController::class, 'history'])->name('inventory.history');

        // Coupons
        Route::resource('coupons', CouponController::class);

        // Pages (CMS)
        Route::resource('pages', PageController::class);

        // Sliders (Home carousel)
        Route::resource('sliders', SliderController::class);

        // Delivery Management
        Route::prefix('delivery')->name('delivery.')->group(function () {
            Route::resource('agents', DeliveryAgentController::class);
            Route::get('orders', [DeliveryOrderController::class, 'index'])->name('orders.index');
            Route::get('orders/{order}', [DeliveryOrderController::class, 'show'])->name('orders.show');
            Route::post('orders/{order}/assign', [DeliveryOrderController::class, 'assign'])->name('orders.assign');
        });

        // Settings (Admin only)
        Route::middleware('admin')->group(function () {
            Route::get('settings', [SettingsController::class, 'index'])->name('settings.index');
            Route::put('settings', [SettingsController::class, 'update'])->name('settings.update');
            Route::post('settings', [SettingsController::class, 'store'])->name('settings.store');
        });
    });
});
