<?php

namespace App\Http\Requests\Approval;

use App\Models\Tenants\ApprovalForm;
use Illuminate\Foundation\Http\FormRequest;

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

    public function rules(): array
    {
        $categories = implode(',', ApprovalForm::CATEGORIES);

        return [
            'name' => 'required|string|max:100',
            'code' => 'required|string|max:50|regex:/^[a-zA-Z0-9_-]+$/',
            'category' => "nullable|string|in:{$categories}",
            'template' => 'required|array',
            'template.fields' => 'required|array',
            'template.fields.*.name' => 'required|string|max:50',
            'template.fields.*.type' => 'required|string|in:text,textarea,number,date,select,checkbox,file',
            'template.fields.*.label' => 'required|string|max:100',
            'template.fields.*.required' => 'nullable|boolean',
            'template.fields.*.options' => 'nullable|array',
            'is_active' => 'nullable|boolean',
        ];
    }

    public function messages(): array
    {
        return [
            'name.required' => __('validation.required', ['attribute' => '양식명']),
            'code.required' => __('validation.required', ['attribute' => '양식코드']),
            'code.regex' => __('validation.regex', ['attribute' => '양식코드']),
            'template.required' => __('validation.required', ['attribute' => '템플릿']),
            'template.fields.required' => __('validation.required', ['attribute' => '필드']),
        ];
    }
}
