<?php

namespace App\Http\Requests;

use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;

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

    /**
     * @return array<string, ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            'anagram' => ['required', 'string', 'alpha:ascii', 'min:2', 'max:24'],
        ];
    }

    public function messages(): array
    {
        return [
            'anagram.alpha' => 'Letters only — no digits, spaces or punctuation.',
            'anagram.max' => 'That is longer than any word in the dictionary.',
        ];
    }

    // The dictionary is lowercase and Matcher::check compares raw characters,
    // so an uppercase anagram would silently return nothing.
    protected function prepareForValidation(): void
    {
        $this->merge([
            'anagram' => strtolower((string) $this->input('anagram', '')),
        ]);
    }
}
