<?php

namespace App\Http\Requests;

use App\Rules\CheckCharacterSingleByte;
use App\Rules\IsCurrentPasswordCorrect;
use App\Rules\IsPasswordValid;
use Illuminate\Foundation\Http\FormRequest;

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

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'current_password' => [
                'bail',
                'required',
                'min:8',
                'max:20',
                new CheckCharacterSingleByte(__('validation.change_password.current_password.format_error')),
                new IsCurrentPasswordCorrect()
            ],
            'new_password' => ['bail', 'required', 'min:8', 'max:20',
                new CheckCharacterSingleByte(__('validation.change_password.new_password.format_error')),
                new IsPasswordValid(__('validation.change_password.new_password.format_error'))
            ],
            'new_password_confirm' => ['bail', 'required', 'min:8', 'max:20',
                new CheckCharacterSingleByte(__('validation.change_password.new_password_confirm.format_error')),
                new IsPasswordValid(__('validation.change_password.new_password_confirm.format_error')),
                'same:new_password'
            ]
        ];
    }

    /**
     * @return array
     */
    public function messages(): array
    {
        return [
            'current_password.required' => __('validation.change_password.current_password.required'),
            'current_password.max' => __('validation.change_password.current_password.max'),
            'current_password.regex' => __('validation.change_password.current_password.format_error'),
            'current_password.min' => __('validation.change_password.current_password.min'),
            'new_password.required' => __('validation.change_password.new_password.required'),
            'new_password.min' => __('validation.change_password.new_password.min'),
            'new_password.max' => __('validation.change_password.new_password.max'),
            'new_password.regex' => __('validation.change_password.new_password.format_error'),
            'new_password_confirm.required' => __('validation.change_password.new_password_confirm.required'),
            'new_password_confirm.max' => __('validation.change_password.new_password_confirm.max'),
            'new_password_confirm.same' => __('validation.change_password.new_password_confirm.same'),
            'new_password_confirm.min' => __('validation.change_password.new_password_confirm.min'),
            'new_password_confirm.regex' => __('validation.change_password.new_password_confirm.format_error'),
        ];
    }
}
