<?php

namespace App\Http\Requests\Admin\Auth;

use Illuminate\Foundation\Http\FormRequest;

class TwoFactorCodeRequest extends FormRequest
{
    protected function prepareForValidation(): void
    {
        $code = $this->input('code');

        if (is_string($code) && preg_match('/^[\d\s]+$/', $code) === 1) {
            $this->merge(['code' => preg_replace('/\s+/', '', $code)]);
        }
    }

    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        return ['code' => ['required', 'string', 'regex:/^(?:[0-9]{6}|[A-Z0-9]{5}-[A-Z0-9]{5})$/i']];
    }

    public function messages(): array
    {
        return [
            'code.required' => __('Enter the 6-digit code from your authenticator app.'),
            'code.regex' => __('Enter a valid 6-digit authentication code.'),
        ];
    }
}
