<?php

/**
 * 설계 관리 API 라우트 (v1)
 *
 * - 설계 모델 관리
 * - 모델 버전 관리
 * - BOM 템플릿 관리
 * - BOM 계산
 * - 감사 로그
 * - 모델셋 관리
 */

use App\Http\Controllers\Api\V1\Design\AuditLogController as DesignAuditLogController;
use App\Http\Controllers\Api\V1\Design\BomCalculationController;
use App\Http\Controllers\Api\V1\Design\BomTemplateController as DesignBomTemplateController;
use App\Http\Controllers\Api\V1\Design\DesignModelController;
use App\Http\Controllers\Api\V1\Design\ModelVersionController as DesignModelVersionController;
use App\Http\Controllers\Api\V1\ModelSetController;
use Illuminate\Support\Facades\Route;

// Design API (설계 관리)
Route::prefix('design')->group(function () {
    // Design Model API (설계 모델)
    Route::prefix('models')->group(function () {
        Route::get('', [DesignModelController::class, 'index'])->name('v1.design.models.index');
        Route::post('', [DesignModelController::class, 'store'])->name('v1.design.models.store');
        Route::get('/archived', [DesignModelController::class, 'archived'])->name('v1.design.models.archived');
        Route::get('/{id}', [DesignModelController::class, 'show'])->whereNumber('id')->name('v1.design.models.show');
        Route::put('/{id}', [DesignModelController::class, 'update'])->whereNumber('id')->name('v1.design.models.update');
        Route::delete('/{id}', [DesignModelController::class, 'destroy'])->whereNumber('id')->name('v1.design.models.destroy');
        Route::post('/{id}/archive', [DesignModelController::class, 'archive'])->whereNumber('id')->name('v1.design.models.archive');
        Route::post('/{id}/restore', [DesignModelController::class, 'restore'])->whereNumber('id')->name('v1.design.models.restore');

        // Model Version API (모델 버전)
        Route::get('/{modelId}/versions', [DesignModelVersionController::class, 'index'])->whereNumber('modelId')->name('v1.design.models.versions.index');
        Route::post('/{modelId}/versions', [DesignModelVersionController::class, 'createDraft'])->whereNumber('modelId')->name('v1.design.models.versions.store');
        Route::get('/{modelId}/versions/{id}', [DesignModelVersionController::class, 'show'])->whereNumber(['modelId', 'id'])->name('v1.design.models.versions.show');
        Route::put('/{modelId}/versions/{id}', [DesignModelVersionController::class, 'update'])->whereNumber(['modelId', 'id'])->name('v1.design.models.versions.update');
        Route::delete('/{modelId}/versions/{id}', [DesignModelVersionController::class, 'destroy'])->whereNumber(['modelId', 'id'])->name('v1.design.models.versions.destroy');
        Route::post('/{modelId}/versions/{id}/release', [DesignModelVersionController::class, 'release'])->whereNumber(['modelId', 'id'])->name('v1.design.models.versions.release');
        Route::post('/{modelId}/versions/{id}/clone', [DesignModelVersionController::class, 'clone'])->whereNumber(['modelId', 'id'])->name('v1.design.models.versions.clone');
        Route::get('/{modelId}/versions/{id}/diff', [DesignModelVersionController::class, 'diff'])->whereNumber(['modelId', 'id'])->name('v1.design.models.versions.diff');
    });

    // BOM Template API (BOM 템플릿)
    Route::prefix('bom-templates')->group(function () {
        Route::get('', [DesignBomTemplateController::class, 'index'])->name('v1.design.bom-templates.index');
        Route::post('', [DesignBomTemplateController::class, 'store'])->name('v1.design.bom-templates.store');
        Route::get('/{id}', [DesignBomTemplateController::class, 'show'])->whereNumber('id')->name('v1.design.bom-templates.show');
        Route::put('/{id}', [DesignBomTemplateController::class, 'update'])->whereNumber('id')->name('v1.design.bom-templates.update');
        Route::delete('/{id}', [DesignBomTemplateController::class, 'destroy'])->whereNumber('id')->name('v1.design.bom-templates.destroy');
        Route::put('/{id}/items/bulk-upsert', [DesignBomTemplateController::class, 'bulkUpsertItems'])->whereNumber('id')->name('v1.design.bom-templates.items.bulk-upsert');
        Route::put('/{id}/items', [DesignBomTemplateController::class, 'replaceItems'])->whereNumber('id')->name('v1.design.bom-templates.items.replace');
        Route::post('/{id}/items/reorder', [DesignBomTemplateController::class, 'reorderItems'])->whereNumber('id')->name('v1.design.bom-templates.items.reorder');
        Route::get('/{id}/summary', [DesignBomTemplateController::class, 'summary'])->whereNumber('id')->name('v1.design.bom-templates.summary');
        Route::get('/{id}/validate', [DesignBomTemplateController::class, 'validate'])->whereNumber('id')->name('v1.design.bom-templates.validate');
        Route::get('/{id}/diff', [DesignBomTemplateController::class, 'diff'])->whereNumber('id')->name('v1.design.bom-templates.diff');
        Route::post('/{id}/clone', [DesignBomTemplateController::class, 'cloneTemplate'])->whereNumber('id')->name('v1.design.bom-templates.clone');
        Route::post('/{id}/calculate-bom', [BomCalculationController::class, 'calculateBom'])->whereNumber('id')->name('v1.design.bom-templates.calculate-bom');
    });

    // Version BOM Templates API (버전별 BOM 템플릿)
    Route::prefix('versions')->group(function () {
        Route::get('/{versionId}/bom-templates', [DesignBomTemplateController::class, 'listByVersion'])->whereNumber('versionId')->name('v1.design.versions.bom-templates.index');
        Route::post('/{versionId}/bom-templates', [DesignBomTemplateController::class, 'upsertTemplate'])->whereNumber('versionId')->name('v1.design.versions.bom-templates.store');
    });

    // BOM Calculation API (BOM 계산)
    Route::prefix('bom-calculation')->group(function () {
        Route::post('/calculate', [BomCalculationController::class, 'calculate'])->name('v1.design.bom-calculation.calculate');
        Route::post('/preview', [BomCalculationController::class, 'preview'])->name('v1.design.bom-calculation.preview');
        Route::get('/form-schema/{versionId}', [BomCalculationController::class, 'getFormSchema'])->whereNumber('versionId')->name('v1.design.bom-calculation.form-schema');
        Route::post('/formulas/test', [BomCalculationController::class, 'testFormula'])->name('v1.design.formulas.test');
    });

    // Model Estimate Parameters API (모델 견적 파라미터)
    Route::get('/models/{modelId}/estimate-parameters', [BomCalculationController::class, 'getEstimateParameters'])->whereNumber('modelId')->name('v1.design.models.estimate-parameters');

    // Company Formulas API (회사별 수식 관리)
    Route::prefix('companies/{companyName}/formulas')->group(function () {
        Route::get('', [BomCalculationController::class, 'getCompanyFormulas'])->name('v1.design.companies.formulas');
        Route::post('/{formulaType}', [BomCalculationController::class, 'saveCompanyFormula'])->name('v1.design.companies.formulas.save');
    });

    // Audit Log API (감사 로그)
    Route::prefix('audit-logs')->group(function () {
        Route::get('', [DesignAuditLogController::class, 'index'])->name('v1.design.audit-logs.index');
        Route::get('/{id}', [DesignAuditLogController::class, 'show'])->whereNumber('id')->name('v1.design.audit-logs.show');
    });
});

// Model Set API (모델셋 관리)
Route::prefix('model-sets')->group(function () {
    Route::get('', [ModelSetController::class, 'index'])->name('v1.model-sets.index');
    Route::post('', [ModelSetController::class, 'store'])->name('v1.model-sets.store');
    Route::get('/active', [ModelSetController::class, 'active'])->name('v1.model-sets.active');
    Route::get('/{id}', [ModelSetController::class, 'show'])->whereNumber('id')->name('v1.model-sets.show');
    Route::put('/{id}', [ModelSetController::class, 'update'])->whereNumber('id')->name('v1.model-sets.update');
    Route::delete('/{id}', [ModelSetController::class, 'destroy'])->whereNumber('id')->name('v1.model-sets.destroy');
    Route::patch('/{id}/toggle', [ModelSetController::class, 'toggle'])->whereNumber('id')->name('v1.model-sets.toggle');
    Route::post('/{id}/clone', [ModelSetController::class, 'clone'])->whereNumber('id')->name('v1.model-sets.clone');
    Route::put('/{id}/items', [ModelSetController::class, 'updateItems'])->whereNumber('id')->name('v1.model-sets.items');
    Route::get('/{id}/fields', [ModelSetController::class, 'getCategoryFields'])->whereNumber('id')->name('v1.model-sets.fields');
    Route::get('/{id}/bom-templates', [ModelSetController::class, 'getBomTemplates'])->whereNumber('id')->name('v1.model-sets.bom-templates');
    Route::get('/{id}/estimate-parameters', [ModelSetController::class, 'getEstimateParameters'])->whereNumber('id')->name('v1.model-sets.estimate-parameters');
    Route::post('/{id}/calculate-bom', [ModelSetController::class, 'calculateBom'])->whereNumber('id')->name('v1.model-sets.calculate-bom');
});
