<?php

namespace App\Http\Requests\Web;

use App\Models\Address;
use App\Models\ShippingCity;
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 OrderCheckOutMethodRequest 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:when_recieving,online,installment'
            ];
        if(request('status'))
        {
            if(request('status') == 'installment')
            {
                $rules['installment_id'] = [
                    "required",
                    Rule::exists('ways', 'id')->whereNull('deleted_at')
                ];
            }
        }

        $user = (new AccountService())->getAccount();
        $status = $user['status'];
        $user = $user['user'];
        if($user->role == 'company')
        {
            $rules['shipping_status'] = 'required|in:shipping_company,shipping_city_bike_driver,shipping_driver';
            if(request('shipping_status'))
            {
                if(request('shipping_status') == 'shipping_company')
                {
                    $rules['policy_number'] = 'required';
                }
                if(request('shipping_status') == 'shipping_driver')
                {
                    $rules['representative_name'] = 'required';
                }
            }
        }
        else
        {
            $rules['shipping_id'] = [
                "required",
                Rule::exists('shippings', 'id')->whereNull('deleted_at')
            ];
            if(request('shipping_id'))
            {
                $location_id = session()->get('location_id');
                $city_id = Address::withTrashed()->find($location_id)->city_id;
                $shipping = ShippingCity::where('shipping_id',request('shipping_id'))->where('city_id',$city_id)->first();
                if(!$shipping)
                {
                    return ['not_delivery_there'=>'required'];
                }
            }
        }
        return $rules;
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function messages()
    {
        return [
            'not_delivery_there.required'=>__('web.not_delivery_there')
        ];
    }
    public function attributes()
    {
        $attributes=
            [
                'installment_id' => __('web.installment'),
                'shipping_status' => __('web.shipping_status'),
                'policy_number' => __('web.policy_number'),
                'status' => __('web.payment_method'),
                'shipping_id' => __('web.shipping_id'),
                'representative_name' => __('web.representative_name'),
            ];

        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);
        }
    }
}
