<?php

namespace App\Http\Requests\Web;

use App\Services\Auth\User\AccountService;
use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\Auth;
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 video_type
 * @property mixed video_url
 * @property mixed images
 */
class LocationOrderRequest 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()
    {
        $rules =
        [
            'status' => 'required|in:previous,new',

        ];

        $user = (new AccountService())->getAccount();
        $status = $user['status'];
        $user = $user['user'];

        if(request('status'))
        {
            if(request('status') == 'new')
            {
                if($status == 'visitor')
                {
                    $rule['name'] = 'required';
                    $rule['email'] = 'required|email';
                    $rule['phone'] = 'required|numeric|min_digits:9';
                $rules= array_merge($rules, $rule);
                }
                $rule =
                    [
                    'country_id'=>[
                        "required",
                        Rule::exists('countries', 'id')->whereNull('deleted_at')
                    ],
                    'city_id'=>[
                        "required",
                        Rule::exists('cities', 'id')->whereNull('deleted_at')
                    ],
                    'address'=>'required'
                ];
                $rules= array_merge($rules, $rule);
            }
            else
            {
                $rule =
                [
                    'location_id'=>[
                        "required",
                        Rule::exists('addresses', 'id')->where('status',$status)->where('user_id',$user->id)
                    ],
                ];
                $rules= array_merge($rules, $rule);
            }
        }

        return $rules;
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {
        $attributes=
            [
                'country_id' => __('web.country'),
                'city_id' => __('web.city'),
                'address' => __('web.address'),
                'status' => __('web.status'),
                'name' => __('web.name'),
                'phone' => __('web.phone'),
                'email' => __('web.email'),
            ];

        return $attributes;
    }


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