<?php

namespace App\Http\Requests\Content;

use Illuminate\Foundation\Http\FormRequest;

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

    public function rules(): array
    {
        return [
            'name' => 'sometimes|required|string|max:50',
            'alias' => function ($attribute, $value, $fail) {
                $model = \App\Models\ContentModel::find($this->route('id'));
                if (! $model || $model->alias !== $value) {
                    $fail('模型别名不允许修改');
                }
            },
            'description' => 'nullable|string|max:255',
            'sort' => 'integer|min:0',
            'status' => 'integer|in:0,1',
            'field_config' => 'nullable|array',
            'field_config.fields' => 'nullable|array',
            'field_config.fields.*.key' => 'required_with:field_config.fields|string',
            'field_config.fields.*.label' => 'nullable|string',
            'field_config.fields.*.type' => 'in:main,extension',
            'field_config.fields.*.visible' => 'boolean',
            'field_config.fields.*.order' => 'integer|min:0',
        ];
    }

    public function attributes(): array
    {
        return [
            'name' => '模型名称',
            'alias' => '模型别名',
            'description' => '描述',
            'sort' => '排序',
            'status' => '状态',
        ];
    }

    public function messages(): array
    {
        return [
            'name.required' => '请输入模型名称',
            'name.max' => '模型名称不能超过50个字符',
            'alias.prohibited' => '模型别名不允许修改',
            'description.max' => '描述不能超过255个字符',
            'sort.min' => '排序值不能小于0',
            'status.in' => '状态值无效',
        ];
    }
}
