<?php

namespace Modules\Store\Requests\Admin;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Request;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
use Modules\Coupon\Enums\CouponDiscountStatusEnum;
use Modules\Coupon\Enums\CouponStatusEnum;
use Modules\Store\Enums\StoreStatusAccountEnum;

class BranchRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        $role = ['super_admin', 'admin'];
        if (! Auth::check()) {
            return false;
        }
        if(!in_array(Auth::user()->role, $role))
        {
            return false;
        }
        if(Auth::user()->hasModule('branches'))
        {
            return true;
        }
        return Auth::check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules=[
            'name'=> 'required|min:3|max:100',
            'address'=> 'required|min:3|max:500',
            'cost_delivery'=> [
                "required",
                "numeric",
            ],
            'time_delivery'=> [
                'required',
                'numeric',
            ],
            'time_prepare'=>[
                'required',
                'numeric'
            ],
            'latitude'=>[
                'required',
            ],
            'longitude'=>[
                'required',
            ],
            'city_id'=>[
                'required',
                'exists:cities,id'
            ],
            'area_id'=>[
                'required',
                'exists:areas,id'
            ],
            'store_id'=>[
                'required',
                'exists:stores,id'
            ],

        ];

        return $rules;
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {

        $attributes =
            [
                'name' => __('branches.name'),
                'address' => __('branches.address'),
                'cost_delivery' => __('branches.cost_delivery'),
                'time_delivery' => __('branches.time_delivery'),
                'time_prepare' => __('branches.time_prepare'),
                'latitude' => __('branches.map'),
                'longitude' => __('branches.map'),
                'city_id' => __('branches.city_id'),
                'area_id' => __('branches.area_id'),
                'store_id'=>__('branches.store_id')
            ];

        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 {
            // Do the original action of the Form request class
            parent::failedValidation($validator);
        }
    }
}
