<?php

namespace Modules\Order\Services;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use Modules\Coupon\Services\CouponService;
use Modules\Order\Interfaces\CartRepositoryInterface;
use Modules\Order\Models\Cart;
use Modules\Product\Models\ProductAttribute;
use Modules\Setting\Enums\SettingOnHandCostValidEnum;
use Modules\Setting\Enums\SettingTaxValidEnum;
use Modules\Shipping\Interfaces\ShippingRepositoryInterface;
use Modules\Shipping\Models\ShippingCity;
use Modules\Shipping\Services\ShippingService;
use Modules\ShippingWay\Services\ShippingWayService;

class CartService
{
    private CartRepositoryInterface $cartRepository ;
    private ShippingWayService $shippingWayService ;
    private CouponService $couponService ;

    public function __construct(CartRepositoryInterface $cartRepository,CouponService $couponService,ShippingWayService $shippingWayService)
    {
        $this->cartRepository = $cartRepository;
        $this->shippingWayService = $shippingWayService;
        $this->couponService = $couponService;
    }

    public function get(Request $request)
    {
        $carts= $this->cartRepository->get($request);
        return $carts;
    }
    public function getItem(Cart $cart)
    {
        $cart = $this->cartRepository->getItem($cart);
        return $cart;
    }
    public function getCart(Request $request)
    {
        $cart = $this->cartRepository->getCart($request);
        return $cart;
    }

    public function store(Request $request): Cart
    {
        $size = ProductAttribute::find($request->size_id);
        $color = ProductAttribute::find($request->color_id);
        $request->merge(['main_attribute_id'=>($size->attribute_id ?? null),'sub_attribute_id'=>($color->attribute_id ?? null)]);
        $cart = $this->getCart($request);

        if ($cart)
        {
            $request->merge(['count'=>($cart->count + $request->count)]);
            $cart = $this->cartRepository->update($request,$cart);
        }
        else
        {
            $cart = $this->cartRepository->store($request);
        }
        return $cart;
    }
    public function update(Request $request, Cart $cart): Cart
    {
        $cart = $this->cartRepository->update($request,$cart);
        return $cart;
    }
    public function destroy(Request $request, int $id): array
    {
        $response = $this->cartRepository->destroy($request,$id);
        return $response;
    }
    public function destroyAll(Request $request): void
    {
        $this->cartRepository->destroyAll($request);
    }
    public function calculate(Request $request)
    {
        $response = [];
        $carts = [];
        $total_tax = 0;
        $total_products = 0;
        $total_products_tax = 0;
        $shipping_cost = 0;
        $on_hand_cost = 0;
        $discount = 0;
        foreach ($this->get($request) as $key=>$row)
        {
            $carts[$key]['id'] = $row->id;
            $carts[$key]['product_image'] = asset($row->product->image);
            $product_name = $row->product->currentDescription->title;
            if ($row->main_attribute_id)
            {
                $product_name .= ' ( '.$row->mainAttr->currentDescription->title;
            }
            if ($row->sub_attribute_id)
            {
                $product_name .= ' - '.$row->subAttr->currentDescription->title . ' ) ';
            }
            $carts[$key]['product_name'] = $product_name;
            $carts[$key]['product_code'] = $row->product->code;
            $carts[$key]['count'] = $row->count;
            $product_cost = $row->count * $row->product->price;
            $carts[$key]['product_cost'] = $product_cost;
            $total_products += $product_cost;
            $carts[$key]['tax_cost'] = 0;
            $tax_value = 0;
            if (settings('tax_valid') == SettingTaxValidEnum::TRUE->value)
            {
                $tax_value = $product_cost * (settings('tax_value') / 100);
                $carts[$key]['tax_cost'] = $tax_value;
                $total_tax += $tax_value;
            }
            $total_products_tax += ($product_cost + $tax_value);
        }
//        $shipping_city_id = Session::get('shipping_city_id');
//        if ($shipping_city_id)
//        {
//            $shipping_city = ShippingCity::find($shipping_city_id);
//            $shipping_city = $this->shippingWayService->getCity($shipping_city);
//            $shipping_cost = $shipping_city->cost;
//        }
        $shipping_cost = settings('shipping_cost');
        $way_id = Session::get('way_id');
        $on_hand_cost_valid = settings('on_hand_cost_valid');
        if ($way_id == 2 && $on_hand_cost_valid == SettingOnHandCostValidEnum::TRUE->value)
        {
            $on_hand_cost = settings('on_hand_cost');
        }
        if (isset($request->coupon))
        {
            $coupon_total = $total_products ;
            $discount = $this->couponService->discount($request->coupon,$coupon_total);
            if ($discount != 0)
            {
                $total_tax = ($total_products - $discount) * (settings('tax_value') / 100);
            }
        }
        $response['carts'] = $carts;
        $response['total_tax'] = number_format($total_tax,2);
        $response['shipping_cost'] = number_format($shipping_cost,2);
        $response['on_hand_cost'] = number_format($on_hand_cost,2);
        $response['discount'] = number_format($discount,2);
        $response['total_products'] = number_format($total_products,2);
        $response['total_products_tax'] = number_format($total_products_tax,2);
        $response['total'] = number_format($total_products + $total_tax + $shipping_cost + $on_hand_cost - $discount ,2);

        return $response;
    }
}
