<?php

/**
 * 생산 관리 API 라우트 (v1)
 *
 * - 공정 관리
 * - 작업지시 관리
 * - 작업실적 관리
 * - 검사 관리
 */

use App\Http\Controllers\Api\V1\InspectionController;
use App\Http\Controllers\Api\V1\WorkOrderController;
use App\Http\Controllers\Api\V1\WorkResultController;
use App\Http\Controllers\V1\ProcessController;
use Illuminate\Support\Facades\Route;

// Process API (공정 관리)
Route::prefix('processes')->group(function () {
    Route::get('', [ProcessController::class, 'index'])->name('v1.processes.index');
    Route::get('/options', [ProcessController::class, 'options'])->name('v1.processes.options');
    Route::get('/stats', [ProcessController::class, 'stats'])->name('v1.processes.stats');
    Route::post('', [ProcessController::class, 'store'])->name('v1.processes.store');
    Route::delete('', [ProcessController::class, 'destroyMany'])->name('v1.processes.destroy-many');
    Route::get('/{id}', [ProcessController::class, 'show'])->whereNumber('id')->name('v1.processes.show');
    Route::put('/{id}', [ProcessController::class, 'update'])->whereNumber('id')->name('v1.processes.update');
    Route::delete('/{id}', [ProcessController::class, 'destroy'])->whereNumber('id')->name('v1.processes.destroy');
    Route::patch('/{id}/toggle', [ProcessController::class, 'toggleActive'])->whereNumber('id')->name('v1.processes.toggle');
});

// Work Order API (작업지시 관리)
Route::prefix('work-orders')->group(function () {
    // 기본 CRUD
    Route::get('', [WorkOrderController::class, 'index'])->name('v1.work-orders.index');                                          // 목록
    Route::get('/stats', [WorkOrderController::class, 'stats'])->name('v1.work-orders.stats');                                    // 통계
    Route::post('', [WorkOrderController::class, 'store'])->name('v1.work-orders.store');                                         // 생성
    Route::get('/{id}', [WorkOrderController::class, 'show'])->whereNumber('id')->name('v1.work-orders.show');                    // 상세
    Route::put('/{id}', [WorkOrderController::class, 'update'])->whereNumber('id')->name('v1.work-orders.update');                // 수정
    Route::delete('/{id}', [WorkOrderController::class, 'destroy'])->whereNumber('id')->name('v1.work-orders.destroy');           // 삭제

    // 상태 및 담당자 관리
    Route::patch('/{id}/status', [WorkOrderController::class, 'updateStatus'])->whereNumber('id')->name('v1.work-orders.status');     // 상태 변경
    Route::patch('/{id}/assign', [WorkOrderController::class, 'assign'])->whereNumber('id')->name('v1.work-orders.assign');           // 담당자 배정

    // 벤딩 공정 상세 토글
    Route::patch('/{id}/bending/toggle', [WorkOrderController::class, 'toggleBendingField'])->whereNumber('id')->name('v1.work-orders.bending-toggle');

    // 이슈 관리
    Route::post('/{id}/issues', [WorkOrderController::class, 'addIssue'])->whereNumber('id')->name('v1.work-orders.issues.store');                              // 이슈 등록
    Route::patch('/{id}/issues/{issueId}/resolve', [WorkOrderController::class, 'resolveIssue'])->whereNumber('id')->name('v1.work-orders.issues.resolve');     // 이슈 해결

    // 품목 상태 변경
    Route::patch('/{id}/items/{itemId}/status', [WorkOrderController::class, 'updateItemStatus'])->whereNumber('id')->whereNumber('itemId')->name('v1.work-orders.items.status');

    // 자재 관리
    Route::get('/{id}/materials', [WorkOrderController::class, 'materials'])->whereNumber('id')->name('v1.work-orders.materials');                              // 자재 목록 조회
    Route::post('/{id}/material-inputs', [WorkOrderController::class, 'registerMaterialInput'])->whereNumber('id')->name('v1.work-orders.material-inputs');     // 자재 투입 등록
});

// Work Result API (작업실적 관리)
Route::prefix('work-results')->group(function () {
    // 기본 CRUD
    Route::get('', [WorkResultController::class, 'index'])->name('v1.work-results.index');                                            // 목록
    Route::get('/stats', [WorkResultController::class, 'stats'])->name('v1.work-results.stats');                                      // 통계
    Route::post('', [WorkResultController::class, 'store'])->name('v1.work-results.store');                                           // 생성
    Route::get('/{id}', [WorkResultController::class, 'show'])->whereNumber('id')->name('v1.work-results.show');                      // 상세
    Route::put('/{id}', [WorkResultController::class, 'update'])->whereNumber('id')->name('v1.work-results.update');                  // 수정
    Route::delete('/{id}', [WorkResultController::class, 'destroy'])->whereNumber('id')->name('v1.work-results.destroy');             // 삭제

    // 상태 토글
    Route::patch('/{id}/inspection', [WorkResultController::class, 'toggleInspection'])->whereNumber('id')->name('v1.work-results.inspection');     // 검사 상태 토글
    Route::patch('/{id}/packaging', [WorkResultController::class, 'togglePackaging'])->whereNumber('id')->name('v1.work-results.packaging');        // 포장 상태 토글
});

// Inspection API (검사 관리)
Route::prefix('inspections')->group(function () {
    Route::get('', [InspectionController::class, 'index'])->name('v1.inspections.index');                                              // 목록
    Route::get('/stats', [InspectionController::class, 'stats'])->name('v1.inspections.stats');                                        // 통계
    Route::post('', [InspectionController::class, 'store'])->name('v1.inspections.store');                                             // 생성
    Route::get('/{id}', [InspectionController::class, 'show'])->whereNumber('id')->name('v1.inspections.show');                        // 상세
    Route::put('/{id}', [InspectionController::class, 'update'])->whereNumber('id')->name('v1.inspections.update');                    // 수정
    Route::delete('/{id}', [InspectionController::class, 'destroy'])->whereNumber('id')->name('v1.inspections.destroy');               // 삭제
    Route::patch('/{id}/complete', [InspectionController::class, 'complete'])->whereNumber('id')->name('v1.inspections.complete');     // 완료 처리
});
