<?php

namespace App\Http\Requests\WorkOrder;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StoreItemInspectionRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        return [
            'process_type' => ['required', 'string', Rule::in([
                'screen', 'slat', 'slat_jointbar', 'bending', 'bending_wip',
            ])],
            'inspection_data' => 'required|array',
            'inspection_data.productName' => 'nullable|string|max:200',
            'inspection_data.specification' => 'nullable|string|max:200',
            'inspection_data.bendingStatus' => ['nullable', Rule::in(['good', 'bad'])],
            'inspection_data.processingStatus' => ['nullable', Rule::in(['good', 'bad'])],
            'inspection_data.sewingStatus' => ['nullable', Rule::in(['good', 'bad'])],
            'inspection_data.assemblyStatus' => ['nullable', Rule::in(['good', 'bad'])],
            'inspection_data.length' => 'nullable|numeric',
            'inspection_data.width' => 'nullable|numeric',
            'inspection_data.height1' => 'nullable|numeric',
            'inspection_data.height2' => 'nullable|numeric',
            'inspection_data.length3' => 'nullable|numeric',
            'inspection_data.gap4' => 'nullable|numeric',
            'inspection_data.gapStatus' => ['nullable', Rule::in(['ok', 'ng'])],
            'inspection_data.gapPoints' => 'nullable|array',
            'inspection_data.gapPoints.*.left' => 'nullable|numeric',
            'inspection_data.gapPoints.*.right' => 'nullable|numeric',
            'inspection_data.judgment' => ['nullable', Rule::in(['pass', 'fail'])],
            'inspection_data.nonConformingContent' => 'nullable|string|max:1000',
            'inspection_data.templateValues' => 'nullable|array',
            'inspection_data.templateValues.*' => 'nullable',
            // 절곡 제품별 검사 데이터
            'inspection_data.products' => 'nullable|array',
            'inspection_data.products.*.id' => 'required_with:inspection_data.products|string',
            'inspection_data.products.*.bendingStatus' => ['nullable', Rule::in(['양호', '불량'])],
            'inspection_data.products.*.lengthMeasured' => 'nullable|string|max:50',
            'inspection_data.products.*.widthMeasured' => 'nullable|string|max:50',
            'inspection_data.products.*.gapPoints' => 'nullable|array',
            'inspection_data.products.*.gapPoints.*.point' => 'nullable|string',
            'inspection_data.products.*.gapPoints.*.designValue' => 'nullable|string',
            'inspection_data.products.*.gapPoints.*.measured' => 'nullable|string|max:50',
        ];
    }

    public function messages(): array
    {
        return [
            'process_type.required' => __('validation.required', ['attribute' => '공정 유형']),
            'process_type.in' => __('validation.in', ['attribute' => '공정 유형']),
            'inspection_data.required' => __('validation.required', ['attribute' => '검사 데이터']),
        ];
    }
}
