<?php

namespace App\Http\Requests\Quote;

use Illuminate\Foundation\Http\FormRequest;

/**
 * BOM 기반 견적 산출 요청
 *
 * React 견적등록 화면에서 자동 견적 산출 시 사용됩니다.
 * 완제품 코드와 입력 변수를 받아 BOM 기반으로 품목/단가/금액을 계산합니다.
 */
class QuoteBomCalculateRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        return [
            // 필수 입력
            'finished_goods_code' => 'required|string|max:50',
            'W0' => 'required|numeric|min:100|max:20000',
            'H0' => 'required|numeric|min:100|max:20000',

            // 선택 입력 (기본값 있음)
            'QTY' => 'nullable|integer|min:1',
            'PC' => 'nullable|string|in:SCREEN,STEEL',
            'GT' => 'nullable|string|in:wall,ceiling,floor,mixed',
            'MP' => 'nullable|string|in:single,three',
            'CT' => 'nullable|string|in:basic,smart,premium',
            'WS' => 'nullable|numeric|min:0|max:500',
            'INSP' => 'nullable|numeric|min:0',

            // 디버그 모드 (개발용)
            'debug' => 'nullable|boolean',
        ];
    }

    public function attributes(): array
    {
        return [
            'finished_goods_code' => __('validation.attributes.finished_goods_code'),
            'W0' => __('validation.attributes.open_width'),
            'H0' => __('validation.attributes.open_height'),
            'QTY' => __('validation.attributes.quantity'),
            'PC' => __('validation.attributes.product_category'),
            'GT' => __('validation.attributes.guide_rail_type'),
            'MP' => __('validation.attributes.motor_power'),
            'CT' => __('validation.attributes.controller'),
            'WS' => __('validation.attributes.wing_size'),
            'INSP' => __('validation.attributes.inspection_fee'),
        ];
    }

    public function messages(): array
    {
        return [
            'finished_goods_code.required' => __('error.finished_goods_code_required'),
            'W0.required' => __('error.open_width_required'),
            'W0.min' => __('error.open_width_min'),
            'W0.max' => __('error.open_width_max'),
            'H0.required' => __('error.open_height_required'),
            'H0.min' => __('error.open_height_min'),
            'H0.max' => __('error.open_height_max'),
        ];
    }

    /**
     * 입력 변수 배열 반환 (FormulaEvaluatorService용)
     */
    public function getInputVariables(): array
    {
        $validated = $this->validated();

        return [
            'W0' => (float) $validated['W0'],
            'H0' => (float) $validated['H0'],
            'QTY' => (int) ($validated['QTY'] ?? 1),
            'PC' => $validated['PC'] ?? 'SCREEN',
            'GT' => $validated['GT'] ?? 'wall',
            'MP' => $validated['MP'] ?? 'single',
            'CT' => $validated['CT'] ?? 'basic',
            'WS' => (float) ($validated['WS'] ?? 50),
            'INSP' => (float) ($validated['INSP'] ?? 50000),
        ];
    }
}
