<?php

namespace App\Http\Requests\API;

use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\Request;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\ValidationException;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;

/**
 * @property mixed password
 * @property mixed email
 * @property mixed photo
 */
class AddHotelReservationRequest 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()
    {
        $phonePattern = '/^(05){1}([0-9]{8})$/';

        $rules = [
            'room_type'=> 'required|exists:rooms,id',
            'days_count'=> 'required|numeric|gt:0',
            'person_count'=> 'nullable|numeric|gt:0',
            'going_date'=> 'required|date|after_or_equal:'.date('Y-m-d'),
            'return_date'=> 'required|date|after_or_equal:going_date',
//            'pay_type'=> 'required|in:online,pay_in_advance',
//            'added_service'=> 'required|array|in:breakfast,half_residence,full_residence',
        ];

        if (request('guest')){
            $rules['name'] = 'required|min:3|max:100';
            $rules['email'] = 'required|email';
            $rules['code'] = 'required|exists:countries,code';
            $rules['phone'] = 'required|numeric|regex:'.$phonePattern;
        }

        return $rules;
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {
        $attributes= [
            'room_type'=> __('hotel_reservations.room_type'),
            'days_count'=> __('hotel_reservations.days_count'),
            'person_count'=> __('hotel_reservations.person_count'),
            'going_date'=> __('hotel_reservations.going_date'),
            'return_date'=> __('hotel_reservations.return_date'),
            'added_service'=> __('hotel_reservations.added_services'),
            'name'=> __('hotel_reservations.name'),
            'email'=> __('hotel_reservations.user_email'),
            'code'=> __('hotel_reservations.code'),
            'phone'=> __('hotel_reservations.user_phone'),
//            'pay_type'=> __('hotel_reservations.pay_type'),
        ];
        return $attributes;
    }

    public function messages()
    {
        return [
            'days_count.gt' => __('hotel_reservations.days_count_gt'),
            'person_count.gt' => __('hotel_reservations.person_count_gt'),
        ];
    }


    /**
     * Handle a failed validation attempt.
     * Override the parent method action if the request is for AJAX
     * @param Validator $validator
     * @return void
     */
    protected function failedValidation(Validator $validator) :void
    {
        // Change response attitude if the request done via Ajax requests like API requests
        $errors = (new ValidationException($validator))->errors();
        $message = $validator->errors()->first();
        throw new HttpResponseException(response()->json(['message'=> $message, 'errors' => $errors],  400));
    }
}
