<?php

namespace App\Http\Requests\Shop;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\ValidationException;
use Sebastienheyd\Boilerplate\Rules\Password;

class RegisterCustomer extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'last_name' => 'required|max:255',
            'first_name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:shop_customers,email,NULL,id,deleted_at,NULL',
            'phone' => 'required|max:30',
            'password' => ['required', 'confirmed', new Password()],
        ];
    }

    protected function failedValidation(Validator $validator)
    {
        $response = redirect()
            ->back()
            ->withInput()
            ->withErrors($validator->errors(), 'registration');

        throw new ValidationException($validator, $response);
    }
}
