<?php

use App\Http\Controllers\Web\AuthController;
use App\Http\Controllers\Web\CartController;
use App\Http\Controllers\Web\CheckoutController;
use App\Http\Controllers\Web\CompanyController;
use App\Http\Controllers\Web\HomeController;
use App\Http\Controllers\Web\OrderController;
use App\Http\Controllers\Web\ProductController;
use App\Http\Controllers\Web\SupplierController;
use App\Http\Controllers\Web\FinanceController;
use App\Http\Controllers\Web\DeliveryController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
*/

// Public Routes
Route::get('/', [HomeController::class, 'index'])->name('home');
Route::get('/deals', [HomeController::class, 'deals'])->name('deals');
Route::get('/categories', [HomeController::class, 'categories'])->name('categories');
Route::get('/compare', [HomeController::class, 'compare'])->name('compare');
Route::get('/compare/search', [HomeController::class, 'compareSearch'])->name('compare.search');

// Products
Route::get('/products', [ProductController::class, 'index'])->name('products.index');
Route::get('/products/{id}', [ProductController::class, 'show'])->name('products.show');

// Authentication
Route::get('/login', [AuthController::class, 'showLogin'])->name('login');
Route::post('/login', [AuthController::class, 'login']);
Route::get('/register', [AuthController::class, 'showRegister'])->name('register');
Route::post('/register/company', [AuthController::class, 'registerCompany'])->name('register.company');
Route::post('/register/supplier', [AuthController::class, 'registerSupplier'])->name('register.supplier');
Route::post('/logout', [AuthController::class, 'logout'])->name('logout');

// Authenticated Routes
Route::middleware('auth')->group(function () {
    // Cart
    Route::get('/cart', [CartController::class, 'index'])->name('cart.index');
    Route::post('/cart/add', [CartController::class, 'add'])->name('cart.add');
    Route::patch('/cart/{id}', [CartController::class, 'update'])->name('cart.update');
    Route::delete('/cart/{id}', [CartController::class, 'remove'])->name('cart.remove');
    Route::delete('/cart', [CartController::class, 'clear'])->name('cart.clear');

    // Checkout
    Route::get('/checkout', [CheckoutController::class, 'index'])->name('checkout');
    Route::post('/checkout', [CheckoutController::class, 'process'])->name('checkout.process');

    // Payment callbacks
    Route::get('/payment/success/{orderId}', [CheckoutController::class, 'paymentSuccess'])->name('payment.success');
    Route::get('/payment/cancel/{orderId}', [CheckoutController::class, 'paymentCancel'])->name('payment.cancel');
    Route::get('/payment/error/{orderId}', [CheckoutController::class, 'paymentError'])->name('payment.error');
    Route::post('/payment/notify', [CheckoutController::class, 'paymentNotify'])->name('payment.notify')->withoutMiddleware('auth');

    // Orders
    Route::get('/orders', [OrderController::class, 'index'])->name('orders.index');
    Route::get('/orders/{id}', [OrderController::class, 'show'])->name('orders.show');
    Route::post('/orders/{id}/cancel', [OrderController::class, 'cancel'])->name('orders.cancel');
    Route::post('/orders/{id}/retry-payment', [OrderController::class, 'retryPayment'])->name('orders.retry-payment');

    // Delivery Calculator
    Route::get('/delivery/calculate', [DeliveryController::class, 'calculate'])->name('delivery.calculate');
    Route::post('/delivery/estimate', [DeliveryController::class, 'estimate'])->name('delivery.estimate');
});

// Company Dashboard Routes
Route::middleware('auth')->prefix('company')->name('company.')->group(function () {
    Route::get('/dashboard', [CompanyController::class, 'dashboard'])->name('dashboard');
    Route::get('/profile', [CompanyController::class, 'profile'])->name('profile');
    Route::put('/profile', [CompanyController::class, 'updateProfile'])->name('profile.update');
});

// Supplier Dashboard Routes
Route::middleware('auth')->prefix('supplier')->name('supplier.')->group(function () {
    Route::get('/dashboard', [SupplierController::class, 'dashboard'])->name('dashboard');
    
    // Products
    Route::get('/products', [SupplierController::class, 'products'])->name('products');
    Route::get('/products/create', [SupplierController::class, 'createProduct'])->name('products.create');
    Route::post('/products', [SupplierController::class, 'storeProduct'])->name('products.store');
    Route::get('/products/{id}/edit', [SupplierController::class, 'editProduct'])->name('products.edit');
    Route::put('/products/{id}', [SupplierController::class, 'updateProduct'])->name('products.update');
    Route::delete('/products/{id}', [SupplierController::class, 'deleteProduct'])->name('products.delete');
    
    // Orders
    Route::get('/orders', [SupplierController::class, 'orders'])->name('orders');
    Route::patch('/orders/{id}/status', [SupplierController::class, 'updateOrderStatus'])->name('orders.status');
    
    // Billing
    Route::get('/billing', [SupplierController::class, 'billing'])->name('billing');
    
    // Profile
    Route::get('/profile', [SupplierController::class, 'profile'])->name('profile');
    Route::put('/profile', [SupplierController::class, 'updateProfile'])->name('profile.update');

    // Delivery Rates
    Route::get('/delivery-rates', [DeliveryController::class, 'supplierRates'])->name('delivery-rates');
    Route::post('/delivery-rates', [DeliveryController::class, 'updateSupplierRates'])->name('delivery-rates.update');
});

// Admin/Finance Routes
Route::middleware('auth')->prefix('admin')->name('admin.')->group(function () {
    Route::get('/finance', [FinanceController::class, 'dashboard'])->name('finance.dashboard');
    Route::get('/finance/invoices', [FinanceController::class, 'invoices'])->name('finance.invoices');
    Route::get('/finance/invoices/{id}', [FinanceController::class, 'showInvoice'])->name('finance.invoices.show');
    Route::post('/finance/invoices/{id}/send', [FinanceController::class, 'sendInvoice'])->name('finance.invoices.send');
    Route::get('/finance/quotes', [FinanceController::class, 'quotes'])->name('finance.quotes');
    Route::get('/finance/payouts', [FinanceController::class, 'payouts'])->name('finance.payouts');
    Route::post('/finance/payouts/process', [FinanceController::class, 'processPayout'])->name('finance.payouts.process');
    Route::get('/finance/ledger', [FinanceController::class, 'ledger'])->name('finance.ledger');
    Route::get('/finance/reports', [FinanceController::class, 'reports'])->name('finance.reports');
    
    // Delivery Zone Management
    Route::get('/delivery-zones', [DeliveryController::class, 'zones'])->name('delivery-zones');
    Route::post('/delivery-zones', [DeliveryController::class, 'storeZone'])->name('delivery-zones.store');
    Route::put('/delivery-zones/{id}', [DeliveryController::class, 'updateZone'])->name('delivery-zones.update');
    Route::delete('/delivery-zones/{id}', [DeliveryController::class, 'deleteZone'])->name('delivery-zones.delete');
});
