<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ShopController;
use App\Http\Controllers\CartController;
use App\Http\Controllers\CheckoutController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\Admin\AuthController as AdminAuthController;
use App\Http\Controllers\Admin\DashboardController as AdminDashboardController;
use App\Http\Controllers\Admin\ProductController as AdminProductController;
use App\Http\Controllers\Admin\CategoryController as AdminCategoryController;
use App\Http\Controllers\Admin\OrderController as AdminOrderController;
use App\Http\Controllers\Admin\CouponController as AdminCouponController;
use App\Http\Controllers\Admin\CustomerController as AdminCustomerController;
use App\Http\Controllers\Admin\BlogPostController as AdminBlogPostController;

/*
|--------------------------------------------------------------------------
| Storefront Routes
|--------------------------------------------------------------------------
*/

Route::get('/', [ShopController::class, 'home'])->name('home');
Route::get('/shop/{category?}', [ShopController::class, 'shop'])->name('shop');
Route::get('/product/{slug}', [ShopController::class, 'product'])->name('product');
Route::get('/about', [ShopController::class, 'about'])->name('about');
Route::get('/contact', [ShopController::class, 'contact'])->name('contact');
Route::get('/blog', [ShopController::class, 'blog'])->name('blog');
Route::get('/blog/{slug}', [ShopController::class, 'blogPost'])->name('blog.show');

// Shopping Bag & Checkout
Route::get('/cart', [CartController::class, 'index'])->name('cart');
Route::post('/cart/add', [CartController::class, 'add'])->name('cart.add');
Route::post('/cart/update', [CartController::class, 'update'])->name('cart.update');
Route::post('/cart/remove', [CartController::class, 'remove'])->name('cart.remove');
Route::post('/cart/coupon', [CartController::class, 'applyCoupon'])->name('cart.coupon');
Route::post('/cart/coupon/remove', [CartController::class, 'removeCoupon'])->name('cart.coupon.remove');

Route::get('/checkout', [CheckoutController::class, 'index'])->name('checkout');
Route::post('/checkout', [CheckoutController::class, 'store'])->name('checkout.store');
Route::get('/order-confirmation/{order_number}', [CheckoutController::class, 'success'])->name('checkout.success');

// Live Order Tracking
Route::get('/track-order/{order_number?}', [\App\Http\Controllers\OrderTrackingController::class, 'index'])->name('order.track');
Route::post('/track-order', [\App\Http\Controllers\OrderTrackingController::class, 'search'])->name('order.track.search');

// Customer Auth & Portal
Route::get('/login', [AuthController::class, 'showLogin'])->name('login');
Route::post('/login', [AuthController::class, 'login'])->name('login.post');
Route::get('/register', [AuthController::class, 'showRegister'])->name('register');
Route::post('/register', [AuthController::class, 'register'])->name('register.post');
Route::post('/logout', [AuthController::class, 'logout'])->name('logout');

Route::middleware(['auth'])->group(function () {
    Route::get('/account', [AuthController::class, 'account'])->name('account');
    Route::post('/account/profile', [AuthController::class, 'updateProfile'])->name('account.profile.update');
    Route::post('/account/address', [AuthController::class, 'updateAddress'])->name('account.address.update');
    Route::post('/account/password', [AuthController::class, 'updatePassword'])->name('account.password.update');
});

/*
|--------------------------------------------------------------------------
| Admin Panel Routes (/admin)
|--------------------------------------------------------------------------
*/

Route::prefix('admin')->group(function () {
    Route::get('/login', [AdminAuthController::class, 'showLoginForm'])->name('admin.login.form');
    Route::post('/login', [AdminAuthController::class, 'login'])->name('admin.login');
    Route::post('/logout', [AdminAuthController::class, 'logout'])->name('admin.logout');

    Route::middleware(['admin'])->as('admin.')->group(function () {
        Route::get('/', [AdminDashboardController::class, 'index']);
        Route::get('/dashboard', [AdminDashboardController::class, 'index'])->name('dashboard');

        // Products CRUD
        Route::resource('products', AdminProductController::class);
        Route::post('products/{product}/toggle', [AdminProductController::class, 'toggleActive'])->name('products.toggle');

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

        // Orders Management
        Route::resource('orders', AdminOrderController::class)->only(['index', 'show']);
        Route::post('orders/{order}/status', [AdminOrderController::class, 'updateStatus'])->name('orders.status');
        Route::get('orders/{order}/invoice', [AdminOrderController::class, 'invoice'])->name('orders.invoice');
        Route::get('orders/{order}/packing-slip', [AdminOrderController::class, 'packingSlip'])->name('orders.packing_slip');

        // Coupons Management
        Route::resource('coupons', AdminCouponController::class)->only(['index', 'store', 'destroy']);
        Route::post('coupons/{coupon}/toggle', [AdminCouponController::class, 'toggle'])->name('coupons.toggle');

        // Customers Directory
        Route::get('customers', [AdminCustomerController::class, 'index'])->name('customers.index');
        Route::get('customers/{customer}', [AdminCustomerController::class, 'show'])->name('customers.show');

        // Blog Management
        Route::resource('posts', AdminBlogPostController::class);
    });
});
