<?php

namespace App\Modules\Pilkades\Http\Requests\Public;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

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

    public function rules(): array
    {
        return [
            'type' => ['required', Rule::in(['nama', 'nik'])],
            'query' => [
                'required',
                'string',
                function ($attribute, $value, $fail) {
                    if ($this->input('type') === 'nik' && ! preg_match('/^\d{16}$/', $value)) {
                        $fail('NIK harus berupa 16 digit angka.');

                        return;
                    }

                    if ($this->input('type') === 'nama' && mb_strlen(trim($value)) < 3) {
                        $fail('Nama minimal 3 karakter.');
                    }
                },
            ],
        ];
    }
}
