<?php

namespace App\Http\Controllers\Web;

use App\City;
use App\Consulting;
use App\Coupon;
use App\Documentation;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class CouponController extends Controller
{

    /**
     * Check coupon code
     * @param Request $request
     * @return array
     */
    public static function checkCoupon(Request $request)
    {
        if (!$request->coupon_code){
            return ['errors' => __('coupons.type_coupon_code')];
        }
        if ($request->service_type == 'consulting'){
            if (!$request->consulting_id){
                return ['errors' => __('consultings.select_consulting_date_time')];
            }

            $consulting = Consulting::where('type', $request->consulting_type)
                ->where('id', $request->consulting_id)->first();
            $price = $consulting->price;

            $coupon = Coupon::checkCoupon($request->coupon_code, $price, 'consulting');
            return $coupon;

        }
        if ($request->service_type == 'documentation'){
            if (!$request->type){
                return ['errors' => __('documentations.select_documentation_type')];
            }

            if (!in_array($request->type, ['inside_office', 'outside_office'])){
                return ['errors' => __('documentations.documentation_no_found')];
            }

//            if (!$request->documentation_id){
//                return ['errors' => __('documentations.select_documentation_date')];
//            }
//            $documentation = Documentation::where('type', $request->type)
//                ->where('id', $request->documentation_id)->first();

            $price = 0;
            if ($request->type == 'outside_office'){
                if (!$request->city_id){
                    return ['errors' => __('documentations.select_city')];
                }

                $city = City::withDescription($request->city_id);
                if (!$city){
                    return ['errors' => __('documentations.select_city_not_found')];
                }

                $price = $city->price;
            }
            if ($request->type == 'inside_office'){
                $price = settings('documentation_fees');
            }

            $coupon = Coupon::checkCoupon($request->coupon_code, $price, 'documentation');
            return $coupon;
        }
    }

}
