<?php

namespace Modules\Order\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Services\Response\ResponseService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Modules\Order\Models\Cart;
use Modules\Coupon\Enums\DiscountStatusEnum;
use Modules\Coupon\Services\CouponService;
use Modules\Order\Requests\Web\AddToCartRequest;
use Modules\Order\Requests\Web\CartRequest;
use Modules\Order\Requests\Web\UpdateCartRequest;
use Modules\Order\Services\CartService;
use Modules\Product\Services\ProductService;
use Modules\User\Models\Address;
use Modules\User\Requests\Web\AddressRequest;
use Modules\User\Services\AccountService;

class CartController extends Controller
{
    private $view = 'web.pages.cart.';

    public function __construct(
        private CartService $cartService,
        private AccountService $accountService,
        private CouponService $couponService,
        private ProductService $productService,
    ){}

    public function index(Request $request)
    {
        $title = __('web.cart');
        return view($this->view.'index',get_defined_vars());
    }

    public function cartCount(Request $request)
    {
        $account = $this->accountService->getAccount();
        $request->merge(['user_id'=>$account['user']->id,'status'=>$account['status']]);
        $count = $this->cartService->get($request);
        return json_encode($count);
    }

    public function store(CartRequest $request)
    {
        $account = $this->accountService->getAccount();
        $user = $account['user'];
        $data = $request->validated();
        $data['user_status'] = $account['status'];
        $data['user_id'] = $user->id;
        $data['warehouse_id'] = $user->address->city->warehouses->first()->warehouse_id;

        $this->cartService->store($data);
        $response = [
            'status' => 200,
            'message' => __('web.cart_success'),
            'data' => ''
        ];
        return ResponseService::response($response);
    }
    public function addToCart(AddToCartRequest $request)
    {
        $account = $this->accountService->getAccount();
        $user = $account['user'];
        $data = $request->validated();
        $data['user_status'] = $account['status'];
        $data['user_id'] = $user->id;
        $data['warehouse_id'] = $user->address->city->warehouses->first()->warehouse_id;
        $product = $this->productService->getItem($request->product_id);
        if (count($product->productOptions))
        {
            $data['option_id'] = $product->productOptions->first()->option_id;
        }
        $this->cartService->store($data);
        $response = [
            'status' => 200,
            'message' => __('web.cart_success'),
            'data' => ''
        ];
        return ResponseService::response($response);
    }

    public function calculate(Request $request)
    {
        $url = url()->previous();

        $route = last(explode('/',$url));
        $account = $this->accountService->getAccount();
        $data =
        [
            'user'=>$account['user'],
            'user_id'=>$account['user']->id,
            'status'=>$account['status'],
            'coupon'=>$request->coupon ?? null,
            'payment_way_id'=>$request->payment_way_id ?? null,
        ];
        $calculate = $this->cartService->calculate($data);
        $carts = $calculate['carts'];
        $total_tax = $calculate['total_tax'];
        $total_products = $calculate['total_products'];
        $total_products_tax = $calculate['total_products_tax'];
        $shipping_cost = $calculate['shipping_cost'];
        $on_hand_cost = $calculate['on_hand_cost'];
        $discount = $calculate['discount'];
        $total = $calculate['total'];
        $products_count = $calculate['products_count'];
        $product_costs = $calculate['product_costs'];
        $discount_offer = $calculate['discount_offer'];
        $calculate_view = view($this->view.'calculate',get_defined_vars())->render();
        $products_view = view($this->view.'products',get_defined_vars())->render();
        return [
            'calculate'=>$calculate_view,
            'products'=>$products_view,
            'total'=>$total
        ];
    }

    public function update(UpdateCartRequest $request,$id)
    {
        $account = $this->accountService->getAccount();
        $this->cartService->update($request->all(),$id);
        $response = [
            'status' => 200,
            'message' => __('web.success'),
            'data' => url()->previous()
        ];
        return ResponseService::response($response);
    }

    public function destroy($id)
    {
        $this->cartService->destroy($id);
        $response = [
            'status' => 200,
            'message' => __('web.success'),
            'data' => url()->previous()
        ];
        return ResponseService::response($response);
    }

    public function checkCoupon(Request $request)
    {
        $color = '';
        $data = '';
        $account = $this->accountService->getAccount();
        $request->merge(['user_id'=>$account['user']->id,'status'=>$account['status'],'user'=>$account['user']]);
        $calculate = $this->cartService->calculate($request->all());

        if($request->coupon)
        {
            $coupon = $this->couponService->getItemByCode($request->coupon);

            if($coupon)
            {
                if($calculate['discount'] != 0)
                {
                    $color = 'green';
                    $data = __('web.discount_value'). ' '. $coupon->discount . ' '. ($coupon->status_discount == DiscountStatusEnum::PERCENTAGE->value ? ' %' : ' '.__('web.sar'));
                }
                else
                {
                    $color = 'red';
                    $data = __('web.invalid_coupon');
                }
            }
            else
            {
                $color = 'red';
                $data = __('web.invalid_coupon');
            }
        }
        return [
            'color'=>$color,
            'data'=>$data
        ];
    }
}
