<?php

use Illuminate\Support\Facades\Route;
use App\Modules\Production\Controllers\ProductionOrderController;
use App\Modules\Production\Controllers\RecipeController;

Route::middleware('auth')->group(function () {
    // 配方管理
    Route::resource('recipes', RecipeController::class);

    // 生產管理
    Route::middleware('permission:production_orders.view')->group(function () {
        Route::get('/production-orders', [ProductionOrderController::class, 'index'])->name('production-orders.index');
        
        Route::middleware('permission:production_orders.create')->group(function () {
            Route::get('/production-orders/create', [ProductionOrderController::class, 'create'])->name('production-orders.create');
            Route::post('/production-orders', [ProductionOrderController::class, 'store'])->name('production-orders.store');
        });
        
        Route::get('/production-orders/{productionOrder}', [ProductionOrderController::class, 'show'])->name('production-orders.show');
        
        Route::middleware('permission:production_orders.edit')->group(function () {
            Route::get('/production-orders/{productionOrder}/edit', [ProductionOrderController::class, 'edit'])->name('production-orders.edit');
            Route::put('/production-orders/{productionOrder}', [ProductionOrderController::class, 'update'])->name('production-orders.update');
        });

        Route::patch('/production-orders/{productionOrder}/update-status', [ProductionOrderController::class, 'updateStatus'])->name('production-orders.update-status');

        Route::middleware('permission:production_orders.delete')->group(function () {
            Route::delete('/production-orders/{productionOrder}', [ProductionOrderController::class, 'destroy'])->name('production-orders.destroy');
        });
    });

    // 生產管理 API
    Route::get('/api/production/warehouses/{warehouse}/inventories', [ProductionOrderController::class, 'getWarehouseInventories'])
        ->middleware('permission:production_orders.create')
        ->name('api.production.warehouses.inventories');

    Route::get('/api/production/products/{product}/inventories', [ProductionOrderController::class, 'getProductWarehouses'])
        ->middleware('permission:production_orders.create')
        ->name('api.production.products.inventories');

    Route::get('/api/production/recipes/latest-by-product/{productId}', [RecipeController::class, 'getLatestByProduct'])
        ->name('api.production.recipes.latest-by-product');

    Route::get('/api/production/recipes/by-product/{productId}', [RecipeController::class, 'getByProduct'])
        ->name('api.production.recipes.by-product');
});
