<?php

/**
 * 재고 관리 API 라우트 (v1)
 *
 * - 품목 관리
 * - BOM 관리
 * - 재고 현황
 * - 입고/출하 관리
 * - 매입 관리
 * - 노무비 관리
 */

use App\Http\Controllers\Api\V1\ItemsBomController;
use App\Http\Controllers\Api\V1\ItemsController;
use App\Http\Controllers\Api\V1\ItemsFileController;
use App\Http\Controllers\Api\V1\LaborController;
use App\Http\Controllers\Api\V1\PurchaseController;
use App\Http\Controllers\Api\V1\ReceivingController;
use App\Http\Controllers\Api\V1\ShipmentController;
use App\Http\Controllers\Api\V1\StockController;
use App\Http\Controllers\Api\V1\VehicleDispatchController;
use Illuminate\Support\Facades\Route;

// Items API (품목 관리)
Route::prefix('items')->group(function () {
    Route::get('', [ItemsController::class, 'index'])->name('v1.items.index');
    Route::post('', [ItemsController::class, 'store'])->name('v1.items.store');
    Route::get('/options', [ItemsController::class, 'options'])->name('v1.items.options');
    Route::get('/stats', [ItemsController::class, 'stats'])->name('v1.items.stats');
    Route::get('/stats-by-type', [ItemsController::class, 'statsByItemType'])->name('v1.items.stats-by-type');
    Route::get('/code/{code}', [ItemsController::class, 'showByCode'])->name('v1.items.show-by-code');
    Route::delete('/bulk', [ItemsController::class, 'bulkDestroy'])->name('v1.items.bulk-destroy');
    Route::delete('/batch', [ItemsController::class, 'batchDestroy'])->name('v1.items.batch-destroy');
    Route::get('/{id}', [ItemsController::class, 'show'])->whereNumber('id')->name('v1.items.show');
    Route::put('/{id}', [ItemsController::class, 'update'])->whereNumber('id')->name('v1.items.update');
    Route::delete('/{id}', [ItemsController::class, 'destroy'])->whereNumber('id')->name('v1.items.destroy');
    Route::patch('/{id}/toggle', [ItemsController::class, 'toggle'])->whereNumber('id')->name('v1.items.toggle');
});

// Items BOM API (품목 BOM)
Route::prefix('items/{id}/bom')->group(function () {
    Route::get('', [ItemsBomController::class, 'index'])->whereNumber('id')->name('v1.items.bom.index');               // BOM 목록
    Route::post('', [ItemsBomController::class, 'store'])->whereNumber('id')->name('v1.items.bom.store');              // BOM 항목 추가
    Route::put('/bulk-upsert', [ItemsBomController::class, 'bulkUpsert'])->whereNumber('id')->name('v1.items.bom.bulk-upsert'); // BOM 일괄 저장
    Route::post('/reorder', [ItemsBomController::class, 'reorder'])->whereNumber('id')->name('v1.items.bom.reorder'); // BOM 순서 변경
    Route::get('/summary', [ItemsBomController::class, 'summary'])->whereNumber('id')->name('v1.items.bom.summary');   // BOM 요약
    Route::get('/validate', [ItemsBomController::class, 'validate'])->whereNumber('id')->name('v1.items.bom.validate'); // BOM 검증
    Route::get('/list-all', [ItemsBomController::class, 'listAll'])->whereNumber('id')->name('v1.items.bom.list-all'); // BOM 전체 목록
    Route::get('/by-category', [ItemsBomController::class, 'listCategories'])->whereNumber('id')->name('v1.items.bom.by-category'); // 카테고리별 BOM
    Route::get('/tree', [ItemsBomController::class, 'tree'])->whereNumber('id')->name('v1.items.bom.tree');            // BOM 트리 구조
    Route::post('/replace', [ItemsBomController::class, 'replace'])->whereNumber('id')->name('v1.items.bom.replace'); // BOM 항목 대체
    Route::get('/{bomId}', [ItemsBomController::class, 'show'])->whereNumber(['id', 'bomId'])->name('v1.items.bom.show'); // BOM 항목 상세
    Route::put('/{bomId}', [ItemsBomController::class, 'update'])->whereNumber(['id', 'bomId'])->name('v1.items.bom.update'); // BOM 항목 수정
    Route::delete('/{bomId}', [ItemsBomController::class, 'destroy'])->whereNumber(['id', 'bomId'])->name('v1.items.bom.destroy'); // BOM 항목 삭제
});

// Items File API (품목 파일)
Route::prefix('items/{id}/files')->group(function () {
    Route::get('', [ItemsFileController::class, 'index'])->whereNumber('id')->name('v1.items.files.index');            // 파일 목록
    Route::post('', [ItemsFileController::class, 'upload'])->whereNumber('id')->name('v1.items.files.upload');         // 파일 업로드
    Route::get('/{fileId}', [ItemsFileController::class, 'show'])->whereNumber(['id', 'fileId'])->name('v1.items.files.show'); // 파일 상세
    Route::delete('/{fileId}', [ItemsFileController::class, 'delete'])->whereNumber(['id', 'fileId'])->name('v1.items.files.delete'); // 파일 삭제
});

// Labor API (노무비 관리)
Route::prefix('labor')->group(function () {
    Route::get('', [LaborController::class, 'index'])->name('v1.labor.index');
    Route::post('', [LaborController::class, 'store'])->name('v1.labor.store');
    Route::get('/active', [LaborController::class, 'active'])->name('v1.labor.active');
    Route::get('/summary', [LaborController::class, 'summary'])->name('v1.labor.summary');
    Route::get('/stats', [LaborController::class, 'stats'])->name('v1.labor.stats');
    Route::post('/bulk-upsert', [LaborController::class, 'bulkUpsert'])->name('v1.labor.bulk-upsert');
    Route::delete('/bulk', [LaborController::class, 'bulkDestroy'])->name('v1.labor.bulk-destroy');
    Route::get('/{id}', [LaborController::class, 'show'])->whereNumber('id')->name('v1.labor.show');
    Route::put('/{id}', [LaborController::class, 'update'])->whereNumber('id')->name('v1.labor.update');
    Route::delete('/{id}', [LaborController::class, 'destroy'])->whereNumber('id')->name('v1.labor.destroy');
});

// Purchase API (매입 관리)
Route::prefix('purchases')->group(function () {
    Route::get('', [PurchaseController::class, 'index'])->name('v1.purchases.index');
    Route::post('', [PurchaseController::class, 'store'])->name('v1.purchases.store');
    Route::get('/summary', [PurchaseController::class, 'summary'])->name('v1.purchases.summary');
    Route::get('/dashboard-detail', [PurchaseController::class, 'dashboardDetail'])->name('v1.purchases.dashboard-detail');
    Route::post('/bulk-update-type', [PurchaseController::class, 'bulkUpdatePurchaseType'])->name('v1.purchases.bulk-update-type');
    Route::post('/bulk-update-tax-received', [PurchaseController::class, 'bulkUpdateTaxReceived'])->name('v1.purchases.bulk-update-tax-received');
    Route::get('/{id}', [PurchaseController::class, 'show'])->whereNumber('id')->name('v1.purchases.show');
    Route::put('/{id}', [PurchaseController::class, 'update'])->whereNumber('id')->name('v1.purchases.update');
    Route::delete('/{id}', [PurchaseController::class, 'destroy'])->whereNumber('id')->name('v1.purchases.destroy');
    Route::post('/{id}/confirm', [PurchaseController::class, 'confirm'])->whereNumber('id')->name('v1.purchases.confirm');
});

// Receiving API (입고 관리)
Route::prefix('receivings')->group(function () {
    Route::get('', [ReceivingController::class, 'index'])->name('v1.receivings.index');
    Route::post('', [ReceivingController::class, 'store'])->name('v1.receivings.store');
    Route::get('/stats', [ReceivingController::class, 'stats'])->name('v1.receivings.stats');
    Route::get('/{id}', [ReceivingController::class, 'show'])->whereNumber('id')->name('v1.receivings.show');
    Route::put('/{id}', [ReceivingController::class, 'update'])->whereNumber('id')->name('v1.receivings.update');
    Route::delete('/{id}', [ReceivingController::class, 'destroy'])->whereNumber('id')->name('v1.receivings.destroy');
    Route::post('/{id}/process', [ReceivingController::class, 'process'])->whereNumber('id')->name('v1.receivings.process');
});

// Stock API (재고 현황)
Route::prefix('stocks')->group(function () {
    Route::get('', [StockController::class, 'index'])->name('v1.stocks.index');
    Route::get('/stats', [StockController::class, 'stats'])->name('v1.stocks.stats');
    Route::get('/stats-by-type', [StockController::class, 'statsByItemType'])->name('v1.stocks.stats-by-type');
    Route::get('/{id}', [StockController::class, 'show'])->whereNumber('id')->name('v1.stocks.show');
});

// Shipment API (출하 관리)
Route::prefix('shipments')->group(function () {
    Route::get('', [ShipmentController::class, 'index'])->name('v1.shipments.index');
    Route::get('/stats', [ShipmentController::class, 'stats'])->name('v1.shipments.stats');
    Route::get('/stats-by-status', [ShipmentController::class, 'statsByStatus'])->name('v1.shipments.stats-by-status');
    Route::get('/options/lots', [ShipmentController::class, 'lotOptions'])->name('v1.shipments.options.lots');
    Route::get('/options/logistics', [ShipmentController::class, 'logisticsOptions'])->name('v1.shipments.options.logistics');
    Route::get('/options/vehicle-tonnage', [ShipmentController::class, 'vehicleTonnageOptions'])->name('v1.shipments.options.vehicle-tonnage');
    Route::post('', [ShipmentController::class, 'store'])->name('v1.shipments.store');
    Route::get('/{id}', [ShipmentController::class, 'show'])->whereNumber('id')->name('v1.shipments.show');
    Route::put('/{id}', [ShipmentController::class, 'update'])->whereNumber('id')->name('v1.shipments.update');
    Route::patch('/{id}/status', [ShipmentController::class, 'updateStatus'])->whereNumber('id')->name('v1.shipments.status');
    Route::delete('/{id}', [ShipmentController::class, 'destroy'])->whereNumber('id')->name('v1.shipments.destroy');
});

// Vehicle Dispatch API (배차차량 관리)
Route::prefix('vehicle-dispatches')->group(function () {
    Route::get('', [VehicleDispatchController::class, 'index'])->name('v1.vehicle-dispatches.index');
    Route::get('/stats', [VehicleDispatchController::class, 'stats'])->name('v1.vehicle-dispatches.stats');
    Route::get('/{id}', [VehicleDispatchController::class, 'show'])->whereNumber('id')->name('v1.vehicle-dispatches.show');
    Route::put('/{id}', [VehicleDispatchController::class, 'update'])->whereNumber('id')->name('v1.vehicle-dispatches.update');
});
