<?php

namespace Modules\Order\Requests\Web;

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\Order\Models\Cart;
use Modules\Order\Models\OrderProduct;
use Modules\Order\Services\CartService;
use Modules\Product\Models\ProductAttribute;
use Modules\User\Services\AccountService;

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()
    {
        $account = (new AccountService())->getAccount();
        $products = Cart::where('user_id',$account['user']->id)->where('status',$account['status'])->get();
        $rules =[];
        $over = null;
        if (count($products) == 0)
        {
            $rules['cart_empty'] = 'required';
        }
        foreach ($products as $row)
        {
            if ($row->sub_attribute_id)
            {
                $attr = ProductAttribute::where('product_id',$row->product_id)->where('attribute_id',$row->sub_attribute_id)->where('main_attribute_id','!=',null)->first();
                if ($attr)
                {
                    $product_count = $attr->count;
                }
                else
                {
                    $product_count = 0;
                }
            }
            else
            {
                $product_count = $row->product->count;
            }
            if ($row->count > $product_count)
            {
                $over = true;
            }
        }
        if ($over)
        {
            $rules['over'] = 'required';
        }
        return $rules;
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {
        $attributes= [
            'cart_empty'=>__('web.cart_empty')
        ];
        $account = (new AccountService())->getAccount();
        $products = Cart::where('user_id',$account['user']->id)->where('status',$account['status'])->get();
        foreach ($products as $row)
        {
            if ($row->count > $row->product->count)
            {
                $attributes['over'] = __('web.product_over_count'). ' : '.$row->product->currentDescription->title ;
            }
        }
        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);
        }
    }
}
