<?php

namespace App\Http\Requests\Design;

use Illuminate\Foundation\Http\FormRequest;

class SaveCompanyFormulaRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            'formula_expression' => 'required|string|max:2000',
            'parameters' => 'required|array',
            'parameters.*.key' => 'required|string|max:50',
            'parameters.*.label' => 'required|string|max:100',
            'parameters.*.type' => 'required|string|in:integer,decimal,string,boolean,array',
            'parameters.*.required' => 'required|boolean',
            'parameters.*.min' => 'nullable|numeric',
            'parameters.*.max' => 'nullable|numeric',
            'parameters.*.default' => 'nullable',
            'parameters.*.options' => 'nullable|array',
            'conditions' => 'nullable|array',
            'conditions.*.field' => 'nullable|string|max:50',
            'conditions.*.operator' => 'nullable|string|in:=,!=,>,>=,<,<=,in,not_in',
            'conditions.*.value' => 'nullable',
            'validation_rules' => 'nullable|array',
            'validation_rules.*.field' => 'nullable|string|max:50',
            'validation_rules.*.rule' => 'nullable|string|max:200',
            'validation_rules.*.message' => 'nullable|string|max:200',
            'description' => 'nullable|string|max:500',
        ];
    }

    /**
     * Get custom messages for validator errors.
     *
     * @return array<string, string>
     */
    public function messages(): array
    {
        return [
            'formula_expression.required' => __('error.validation.required'),
            'formula_expression.string' => __('error.validation.string'),
            'formula_expression.max' => __('error.validation.max.string'),
            'parameters.required' => __('error.validation.required'),
            'parameters.array' => __('error.validation.array'),
            'parameters.*.key.required' => __('error.validation.required'),
            'parameters.*.key.string' => __('error.validation.string'),
            'parameters.*.key.max' => __('error.validation.max.string'),
            'parameters.*.label.required' => __('error.validation.required'),
            'parameters.*.label.string' => __('error.validation.string'),
            'parameters.*.label.max' => __('error.validation.max.string'),
            'parameters.*.type.required' => __('error.validation.required'),
            'parameters.*.type.string' => __('error.validation.string'),
            'parameters.*.type.in' => __('error.validation.in'),
            'parameters.*.required.required' => __('error.validation.required'),
            'parameters.*.required.boolean' => __('error.validation.boolean'),
            'parameters.*.min.numeric' => __('error.validation.numeric'),
            'parameters.*.max.numeric' => __('error.validation.numeric'),
            'parameters.*.options.array' => __('error.validation.array'),
            'conditions.array' => __('error.validation.array'),
            'conditions.*.field.string' => __('error.validation.string'),
            'conditions.*.field.max' => __('error.validation.max.string'),
            'conditions.*.operator.string' => __('error.validation.string'),
            'conditions.*.operator.in' => __('error.validation.in'),
            'validation_rules.array' => __('error.validation.array'),
            'validation_rules.*.field.string' => __('error.validation.string'),
            'validation_rules.*.field.max' => __('error.validation.max.string'),
            'validation_rules.*.rule.string' => __('error.validation.string'),
            'validation_rules.*.rule.max' => __('error.validation.max.string'),
            'validation_rules.*.message.string' => __('error.validation.string'),
            'validation_rules.*.message.max' => __('error.validation.max.string'),
            'description.string' => __('error.validation.string'),
            'description.max' => __('error.validation.max.string'),
        ];
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array<string, string>
     */
    public function attributes(): array
    {
        return [
            'formula_expression' => '계산식 표현식',
            'parameters' => '파라미터 정의',
            'parameters.*.key' => '파라미터 키',
            'parameters.*.label' => '파라미터 레이블',
            'parameters.*.type' => '파라미터 타입',
            'parameters.*.required' => '필수 여부',
            'parameters.*.min' => '최소값',
            'parameters.*.max' => '최대값',
            'parameters.*.default' => '기본값',
            'parameters.*.options' => '옵션 목록',
            'conditions' => '적용 조건',
            'conditions.*.field' => '조건 필드',
            'conditions.*.operator' => '조건 연산자',
            'conditions.*.value' => '조건 값',
            'validation_rules' => '검증 규칙',
            'validation_rules.*.field' => '검증 필드',
            'validation_rules.*.rule' => '검증 규칙',
            'validation_rules.*.message' => '검증 메시지',
            'description' => '설명',
        ];
    }
}
