<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;

class UpdateOfferFinanceFieldConfigRequest extends FormRequest
{
    public function authorize(): bool
    {
        return Auth::check();
    }

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

        return [
            'key' => [
                'sometimes',
                'required',
                'string',
                'max:64',
                'regex:/^[a-z][a-z0-9_]*$/',
                Rule::unique('offer_finance_field_configs', 'key')->ignore($id),
            ],
            'label' => ['sometimes', 'required', 'string', 'max:255'],
            'type' => ['sometimes', 'required', Rule::in(['text', 'number', 'currency'])],
            'suffix' => ['nullable', 'string', 'max:32'],
            'position' => ['nullable', 'integer', 'min:0'],
        ];
    }
}
