<?php

namespace App\Http\Requests\Config;

use Illuminate\Foundation\Http\FormRequest;

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

    public function rules(): array
    {
        $configId = $this->route('id');

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