<?php

namespace App\Http\Requests\Config;

use Illuminate\Foundation\Http\FormRequest;

class StoreConfigRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()->isAdmin();
    }

    public function rules(): array
    {
        return [
            'field_name' => ['required', 'string', 'max:150'],
            'field_key'  => ['required', 'string', 'max:100', 'unique:checklist_configs,field_key', 'regex:/^[a-z0-9_]+$/'],
            'field_type' => ['required', 'in:text,number,boolean,select'],
            'options'    => ['nullable', 'array'],
            'options.*'  => ['string'],
            'is_required'=> ['sometimes', 'boolean'],
            'is_active'  => ['sometimes', 'boolean'],
            'sort_order' => ['sometimes', 'integer', 'min:0'],
        ];
    }

    public function messages(): array
    {
        return [
            'field_key.regex' => 'Field key must contain only lowercase letters, numbers, and underscores.',
        ];
    }
}
