<?php

namespace Modules\Auth\Http\Requests\Password;

use Illuminate\Foundation\Http\FormRequest;
use Modules\Core\Traits\FailedValidation;

class ResetPasswordRequest extends FormRequest
{
    use FailedValidation;

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        return [
            'email' => ['required','email','exists:users,email'],
            'token' => ['required'],
            'password' => [
                'required',
                'string',
                'min:6',
                'required_with:password_confirmation',
                function ($attribute, $value, $fail) {
                    if (request('password_confirmation') !== null && $value !== request('password_confirmation')) {
                        $fail('Passwords do not match.');
                    }
                }
            ],
            'password_confirmation' => ['nullable', 'string', 'min:6', 'required_with:password']
        ];
    }

    public function messages(): array
    {
        return [
            'email.required' => 'The email field is required.',
            'email.email' => 'Enter a valid email address.',
            'email.exists' => 'No user found with this email address.',
            'token.required' => 'The token is required.',
        ];
    }

    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }
}
