<?php

namespace App\Http\Requests\Front;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;

class RegisterRequest 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 [
            'name' => 'required|string|max:20',
            'email' => 'required|email|unique:users',
            'phone' => ['required','unique:users','regex:/^((\+|00)9665|0?5)([013-9][0-9]{7})$/'],
            'password' => 'required|min:8',
            'type' => 'in:marketed,user,owner',
            'realEstateAdvertiserNumber' => 'required_if:type,==,marketed,owner',
            'facilityName' => 'required_if:type,==,marketed,owner',
            'area_id' => 'required|exists:areas,id'
        ];
    }

    public function messages()
    {
        return [
            'fullName.required' => 'FullName is required!',
            'email.required' => 'Email is required!',
            'phone.required' => 'phone is required!',
            'password.required' => 'Password is required!',
            'overview.required' => 'Overview is required!',
            'type.required' => 'type is required!'
        ];
    }

    protected function failedValidation(Validator $validator)
    {
        $response = [
            'status' => false,
            'message' => $validator->errors()->first(),
            'data' => $validator->errors()
        ];
        throw new HttpResponseException(response()->json($response, 422));
    }
}
