<?php

declare(strict_types=1);

namespace App\Http\Requests\Auth;

use Illuminate\Foundation\Http\FormRequest;

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

    /** @return array<string, mixed> */
    public function rules(): array
    {
        return [
            'email' => ['required', 'string', 'email', 'max:255'],
            'password' => ['required', 'string'],
            'remember' => ['nullable', 'boolean'],

            /*
             * Present for token-based clients (the mobile app). When given, the
             * response carries a bearer token instead of establishing a
             * cookie session.
             */
            'device_name' => ['nullable', 'string', 'max:120'],
            'device_platform' => ['nullable', 'string', 'in:ios,android,web,desktop,other'],

            // Supplied on the second leg of a two-factor login.
            'two_factor_code' => ['nullable', 'string', 'max:12'],
            'recovery_code' => ['nullable', 'string', 'max:64'],
        ];
    }

    protected function prepareForValidation(): void
    {
        $this->merge([
            'email' => is_string($this->input('email'))
                ? mb_strtolower(trim($this->input('email')))
                : $this->input('email'),
        ]);
    }

    public function wantsToken(): bool
    {
        return $this->filled('device_name');
    }
}
