<?php

namespace App\Http\Requests\Admin\Settings;

use App\Models\KycForm;
use Illuminate\Foundation\Http\FormRequest;

class KycFormRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return auth('admin')->check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            'label' => 'required|string|max:191',
            'field_type' => 'required|string|max:191',
            'required' => 'required|in:0,1',
            'options' => 'required_if:field_type,select,file',
            'placeholder' => 'nullable|string|max:191',
            'instructions' => 'nullable|string|max:255',
        ];
    }

    public function validated($key = null, $default = null)
    {
        $data = parent::validated();

        if (! isset($data['position']) && $this->method() === 'POST') {
            $data['position'] = KycForm::max('position') + 1;
        }

        return $data;
    }
}
