<?php

namespace App\Http\Requests\Web;

use App\Models\Cart;
use App\Models\WorkDay;
use App\Services\Repositories\CartRepository;
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 OrderRequest 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()
    {
        $work_day = WorkDay::where('day',date('l'))->first();
        $start_time = date('H',strtotime($work_day->start_time));
        [$startTimes, $endTimes, $times, $fullTimes]= $work_day->time_slots;
        $rules =
            [
                'accept_on'=>'required',
                'status' => 'required|in:room,table,pickup',
                'time' => 'sometimes|nullable',
                // 'table_id' => 'sometimes|nullable|required_if:status,table|exists:table_numbers,foodics_id',
                'room_id' => 'sometimes|nullable|required_if:status,room|exists:rooms,id',
                
            ];
        if(request('status') == 'table')
        {
            $rules['table_id'] = 'required|exists:table_numbers,foodics_id';
        }
        if(request('status') == 'pickup')
        {
            $rules['time'] = 'required|in:'.'"'.implode('","', $fullTimes).'"|after:now';
        }
        $carts = Cart::where('user_id',Auth::user()->id)->count();
        if ($carts == 0)
        {
            return ['min_1' => 'required'];
        }
        // dd($rules);
        return $rules;
    }

    public function messages()
    {
        return [
            'min_1.required'=>__('web.min_1')
        ];
    }
    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {
        $attributes=
            [
                'accept_on'=>__('web.accept_on'),
                'status' => __('orders.status'),
                'time' => __('orders.time'),
                'table_id' => __('web.table'),
                'room_id' => __('web.room'),
            ];

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