<?php

use Illuminate\Support\Facades\Route;
use Modules\WMSInventoryCore\App\Http\Controllers\AdjustmentController;
use Modules\WMSInventoryCore\App\Http\Controllers\AdjustmentTypeController;
use Modules\WMSInventoryCore\App\Http\Controllers\DashboardController;
use Modules\WMSInventoryCore\App\Http\Controllers\ProductController;
use Modules\WMSInventoryCore\App\Http\Controllers\ReportController;
use Modules\WMSInventoryCore\App\Http\Controllers\SettingsController;
use Modules\WMSInventoryCore\App\Http\Controllers\TransferController;
use Modules\WMSInventoryCore\App\Http\Controllers\UnitController;
use Modules\WMSInventoryCore\App\Http\Controllers\VendorController;
use Modules\WMSInventoryCore\App\Http\Controllers\WarehouseController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::middleware(['auth', 'verified'])->prefix('inventory')->name('wmsinventorycore.')->group(function () {
    // Test route to verify file is loading
    Route::get('/test-route-loading', function () {
        return 'Routes are loading';
    })->name('test.loading');
    // Dashboard - requires view permission
    Route::middleware(['permission:wmsinventory.view-dashboard'])->group(function () {
        Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard.index');
    });

    // Product search - accessible without specific permission for purchase order creation
    Route::get('/products/search', [ProductController::class, 'searchProducts'])->name('products.search');

    // Units - grouped by permission type
    Route::middleware(['permission:wmsinventory.view-units'])->group(function () {
        Route::get('/units', [UnitController::class, 'index'])->name('units.index');
        Route::get('/units/data', [UnitController::class, 'getDataAjax'])->name('units.data');
        Route::get('/units/{unit}', [UnitController::class, 'show'])->name('units.show');
    });

    Route::middleware(['permission:wmsinventory.create-unit'])->group(function () {
        Route::post('/units', [UnitController::class, 'store'])->name('units.store');
    });

    Route::middleware(['permission:wmsinventory.edit-unit'])->group(function () {
        Route::put('/units/{unit}', [UnitController::class, 'update'])->name('units.update');
        Route::patch('/units/{unit}', [UnitController::class, 'update']);
    });

    Route::middleware(['permission:wmsinventory.delete-unit'])->group(function () {
        Route::delete('/units/{unit}', [UnitController::class, 'destroy'])->name('units.destroy');
    });

    // Warehouses - grouped by permission type
    // Create routes must come before parameterized routes to avoid 404 errors
    Route::middleware(['permission:wmsinventory.create-warehouse'])->group(function () {
        Route::get('/warehouses/create', [WarehouseController::class, 'create'])->name('warehouses.create');
        Route::post('/warehouses', [WarehouseController::class, 'store'])->name('warehouses.store');
    });

    Route::middleware(['permission:wmsinventory.view-warehouses'])->group(function () {
        Route::get('/warehouses', [WarehouseController::class, 'index'])->name('warehouses.index');
        Route::get('/warehouses/data', [WarehouseController::class, 'getDataAjax'])->name('warehouses.data');
        Route::get('/warehouses/search', [WarehouseController::class, 'searchWarehouses'])->name('warehouses.search');
        Route::get('/warehouses/{warehouse}', [WarehouseController::class, 'show'])->name('warehouses.show');
    });

    Route::middleware(['permission:wmsinventory.view-warehouse-inventory'])->group(function () {
        Route::get('/warehouses/{warehouse}/inventory/data', [WarehouseController::class, 'getInventoryDataAjax'])->name('warehouse.inventory.data');
    });

    Route::middleware(['permission:wmsinventory.edit-warehouse'])->group(function () {
        Route::get('/warehouses/{warehouse}/edit', [WarehouseController::class, 'edit'])->name('warehouses.edit');
        Route::put('/warehouses/{warehouse}', [WarehouseController::class, 'update'])->name('warehouses.update');
        Route::patch('/warehouses/{warehouse}', [WarehouseController::class, 'update']);
    });

    Route::middleware(['permission:wmsinventory.delete-warehouse'])->group(function () {
        Route::delete('/warehouses/{warehouse}', [WarehouseController::class, 'destroy'])->name('warehouses.destroy');
    });

    Route::middleware(['permission:wmsinventory.manage-warehouse-zones'])->group(function () {
        Route::post('/warehouses/{warehouse}/zones', [WarehouseController::class, 'storeZone'])->name('warehouses.zones.store');
        Route::put('/warehouses/{warehouse}/zones/{zone}', [WarehouseController::class, 'updateZone'])->name('warehouses.zones.update');
        Route::delete('/warehouses/{warehouse}/zones/{zone}', [WarehouseController::class, 'destroyZone'])->name('warehouses.zones.destroy');
    });

    // Adjustments - grouped by permission type
    // Create routes must come before parameterized routes to avoid 404 errors
    Route::middleware(['permission:wmsinventory.create-adjustment'])->group(function () {
        Route::get('/adjustments/create', [AdjustmentController::class, 'create'])->name('adjustments.create');
        Route::post('/adjustments', [AdjustmentController::class, 'store'])->name('adjustments.store');
    });

    Route::middleware(['permission:wmsinventory.view-adjustments'])->group(function () {
        Route::get('/adjustments', [AdjustmentController::class, 'index'])->name('adjustments.index');
        Route::get('/adjustments/data', [AdjustmentController::class, 'getDataAjax'])->name('adjustments.data');
        Route::get('/adjustments/warehouse-products', [AdjustmentController::class, 'getWarehouseProducts'])->name('adjustments.warehouse-products');
        Route::get('/adjustments/{adjustment}', [AdjustmentController::class, 'show'])->name('adjustments.show');
    });

    Route::middleware(['permission:wmsinventory.edit-adjustment'])->group(function () {
        Route::get('/adjustments/{adjustment}/edit', [AdjustmentController::class, 'edit'])->name('adjustments.edit');
        Route::put('/adjustments/{adjustment}', [AdjustmentController::class, 'update'])->name('adjustments.update');
        Route::patch('/adjustments/{adjustment}', [AdjustmentController::class, 'update']);
    });

    Route::middleware(['permission:wmsinventory.approve-adjustment'])->group(function () {
        Route::patch('/adjustments/{adjustment}/approve', [AdjustmentController::class, 'approve'])->name('adjustments.approve');
    });

    Route::middleware(['permission:wmsinventory.delete-adjustment'])->group(function () {
        Route::delete('/adjustments/{adjustment}', [AdjustmentController::class, 'destroy'])->name('adjustments.destroy');
    });

    // Adjustment Types - grouped by permission type
    Route::middleware(['permission:wmsinventory.view-adjustment-types'])->group(function () {
        Route::get('/adjustment-types', [AdjustmentTypeController::class, 'index'])->name('adjustmenttypes.index');
        Route::get('/adjustment-types/data', [AdjustmentTypeController::class, 'getDataAjax'])->name('adjustment-types.data');
        Route::get('/adjustment-types/{adjustmentType}', [AdjustmentTypeController::class, 'show'])->name('adjustmenttypes.show');
    });

    Route::middleware(['permission:wmsinventory.create-adjustment-type'])->group(function () {
        Route::post('/adjustment-types', [AdjustmentTypeController::class, 'store'])->name('adjustmenttypes.store');
    });

    Route::middleware(['permission:wmsinventory.edit-adjustment-type'])->group(function () {
        Route::put('/adjustment-types/{adjustmentType}', [AdjustmentTypeController::class, 'update'])->name('adjustmenttypes.update');
        Route::patch('/adjustment-types/{adjustmentType}', [AdjustmentTypeController::class, 'update']);
    });

    Route::middleware(['permission:wmsinventory.delete-adjustment-type'])->group(function () {
        Route::delete('/adjustment-types/{adjustmentType}', [AdjustmentTypeController::class, 'destroy'])->name('adjustmenttypes.destroy');
    });

    // Transfers - grouped by permission type
    // Create routes must come before parameterized routes to avoid 404 errors
    Route::middleware(['permission:wmsinventory.create-transfer'])->group(function () {
        Route::get('/transfers/create', [TransferController::class, 'create'])->name('transfers.create');
        Route::post('/transfers', [TransferController::class, 'store'])->name('transfers.store');
    });

    Route::middleware(['permission:wmsinventory.view-transfers'])->group(function () {
        Route::get('/transfers', [TransferController::class, 'index'])->name('transfers.index');
        Route::get('/transfers/data', [TransferController::class, 'getDataAjax'])->name('transfers.data');
        Route::get('/transfers/warehouse-products', [TransferController::class, 'getWarehouseProducts'])->name('transfers.warehouse-products');
        Route::get('/transfers/{transfer}', [TransferController::class, 'show'])->name('transfers.show');
        Route::get('/transfers/{transfer}/print', [TransferController::class, 'print'])->name('transfers.print');
    });

    Route::middleware(['permission:wmsinventory.edit-transfer'])->group(function () {
        Route::get('/transfers/{transfer}/edit', [TransferController::class, 'edit'])->name('transfers.edit');
        Route::put('/transfers/{transfer}', [TransferController::class, 'update'])->name('transfers.update');
        Route::patch('/transfers/{transfer}', [TransferController::class, 'update']);
    });

    Route::middleware(['permission:wmsinventory.approve-transfer'])->group(function () {
        Route::post('/transfers/{transfer}/approve', [TransferController::class, 'approve'])->name('transfers.approve');
    });

    Route::middleware(['permission:wmsinventory.ship-transfer'])->group(function () {
        Route::post('/transfers/{transfer}/ship', [TransferController::class, 'ship'])->name('transfers.ship');
    });

    Route::middleware(['permission:wmsinventory.receive-transfer'])->group(function () {
        Route::post('/transfers/{transfer}/receive', [TransferController::class, 'receive'])->name('transfers.receive');
    });

    Route::middleware(['permission:wmsinventory.cancel-transfer'])->group(function () {
        Route::post('/transfers/{transfer}/cancel', [TransferController::class, 'cancel'])->name('transfers.cancel');
    });

    Route::middleware(['permission:wmsinventory.delete-transfer'])->group(function () {
        Route::delete('/transfers/{transfer}', [TransferController::class, 'destroy'])->name('transfers.destroy');
    });

    // Vendors - grouped by permission type
    Route::middleware(['permission:wmsinventory.view-vendors'])->group(function () {
        Route::get('/vendors', [VendorController::class, 'index'])->name('vendors.index');
        Route::get('/vendors/data', [VendorController::class, 'getDataAjax'])->name('vendors.data');
    });

    Route::middleware(['permission:wmsinventory.search-vendors'])->group(function () {
        Route::get('/vendors/search', [VendorController::class, 'searchVendors'])->name('vendors.search');
    });

    Route::middleware(['permission:wmsinventory.create-vendor'])->group(function () {
        Route::get('/vendors/create', [VendorController::class, 'create'])->name('vendors.create');
        Route::post('/vendors', [VendorController::class, 'store'])->name('vendors.store');
    });

    Route::middleware(['permission:wmsinventory.edit-vendor'])->group(function () {
        Route::get('/vendors/{vendor}/edit', [VendorController::class, 'edit'])->name('vendors.edit');
        Route::put('/vendors/{vendor}', [VendorController::class, 'update'])->name('vendors.update');
        Route::patch('/vendors/{vendor}', [VendorController::class, 'update']);
    });

    Route::middleware(['permission:wmsinventory.view-vendors'])->group(function () {
        Route::get('/vendors/{vendor}', [VendorController::class, 'show'])->name('vendors.show');
    });

    Route::middleware(['permission:wmsinventory.delete-vendor'])->group(function () {
        Route::delete('/vendors/{vendor}', [VendorController::class, 'destroy'])->name('vendors.destroy');
    });

    // Reports - grouped by permission type
    Route::middleware(['permission:wmsinventory.view-inventory-valuation'])->group(function () {
        Route::get('/reports/inventory-valuation', [ReportController::class, 'inventoryValuation'])->name('reports.inventory-valuation');
    });

    Route::middleware(['permission:wmsinventory.view-stock-movement'])->group(function () {
        Route::get('/reports/stock-movement', [ReportController::class, 'stockMovement'])->name('reports.stock-movement');
    });

    Route::middleware(['permission:wmsinventory.view-low-stock'])->group(function () {
        Route::get('/reports/low-stock', [ReportController::class, 'lowStock'])->name('reports.low-stock');
    });

    // Settings
    Route::middleware(['permission:wmsinventory.manage-settings'])->prefix('settings')->name('settings.')->group(function () {
        Route::get('/', [SettingsController::class, 'index'])->name('index');
        Route::post('/update', [SettingsController::class, 'update'])->name('update');
    });
});
