<?php
namespace App\Services\Product\Cart;

use App\Models\Cart;
use App\Models\Product;
use App\Models\ProductAttributeChoice;
use App\Services\Auth\User\AccountService;
use App\Services\Currency\CurrencyService;
use App\Services\Product\ProductService;
use App\Services\Product\Tax\TaxService;
use App\Services\Response\ResponseService;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;

class CartService
{
    public function carts($message = ''):array
    {
        $user = (new AccountService())->getAccount();
        $status = $user['status'];
        $user = $user['user'];
        $this->checkRemove($status,$user);
        $carts = Cart::where('status',$status)->where('user_id',$user->id)->orderBy('id','DESC')->get();
        $count = Cart::where('status',$status)->where('user_id',$user->id)->orderBy('id','DESC')->sum('count');
        
        $status = 200;
        $content =
        [
            'message' => $message,
            'status' => $status,
            'data' => [
                'carts'=>$carts,
                'count'=>$count,
                ],
        ];

        return $content;
    }
    private function checkRemove($status,$user)
    {
        $carts = Cart::where('status',$status)->where('user_id',$user->id)->orderBy('id','DESC')->get();
        
        foreach($carts as $cart)
        {
            $product= Product::onlyTrashed()->find($cart->product_id);
            if($product)
            {
                $cart->delete();
            }
        }
    }
    public function createCart($requestContent)
    {
        $user = (new AccountService())->getAccount();
        $status = $user['status'];
        $user = $user['user'];
        $requestContent['user_id'] = $user->id;
        $requestContent['status'] = $status;
        $product = Product::find($requestContent['product_id']);
        
        $checkCount = (new ProductService($product))->checkAvailable($requestContent['count'],$requestContent['choice_id']);

        if($checkCount['status'] != 200)
        {
            return ResponseService::responseFail($checkCount);
        }

        $cart = Cart::where('product_id',$requestContent['product_id'])->where('choice_id',$requestContent['choice_id'])->where('status',$status)->where('user_id',$user->id)->first();
        if(!$cart)
        {
            $checkAvailable = (new ProductService($product))->checkAvailable($requestContent['count'],$requestContent['choice_id']);
            if($checkAvailable['status'] != 200)
            {
                return ResponseService::responseFail($checkAvailable);
            }
            Cart::create($requestContent);
        }
        else
        {
            $count = $cart->count + $requestContent['count'];
            $checkAvailable = (new ProductService($product))->checkAvailable($count,$requestContent['choice_id']);
            if($checkAvailable['status'] != 200)
            {
                return ResponseService::responseFail($checkAvailable);
            }
            $cart->update(['count'=>$count]);
        }
        $message = __('web.success');
        $response = $this->carts($message);
        return ResponseService::response($response);
    }


    public function editCart($requestContent,$cart_id)
    {
        $user = (new AccountService())->getAccount();
        $status = $user['status'];
        $user = $user['user'];
        $cart = Cart::where('status',$status)->where('user_id',$user->id)->where('id',$cart_id)->first();
        if(!$cart)
        {
            return ResponseService::responseFail([__('web.not_found')]);
        }


        $product = Product::find($cart->product_id);
        if($requestContent['status'] == 'plus')
        {
            $checkCount = (new ProductService($product))->productCount($cart->choice_id);
            if ($checkCount['status'] != 200) {
                return ResponseService::responseFail($checkCount);
            }
        }

        $count =  $requestContent['count'];
        if($requestContent['status'] == 'plus')
        {
            $checkAvailable =  (new ProductService($product))->checkAvailable($count,$cart->choice_id);

            if($checkAvailable['status'] != 200)
            {
                return ResponseService::responseFail($checkAvailable);
            }
        }
        $cart->update(['count'=>$requestContent['count']]);

        $message = __('web.success');

        $product_cost = $cart->product->cost * $cart->count;

        $tax = TaxService::tax();
        $total = $this->totalCart();
        $cart_count = Cart::where('status',$status)->where('user_id',$user->id)->sum('count');
        $response =
            [
                'status' => 200,
                'message' => $message,
                'data'=>
                [
                    'cart_count' => $cart_count,
                    'total_order' => $total,
                    'product_cost' => $product_cost,
                    'tax_value' => $tax['value'] * $cart->count,
                    'product_total' => ($cart->product->cost + $tax['value']) * $cart->count,
                ]
            ];
        return ResponseService::response($response);
    }
    public function totalCart()
    {
        $user = (new AccountService())->getAccount();
        $status = $user['status'];
        $user = $user['user'];
        $carts = Cart::where('status',$status)->where('user_id',$user->id)->get();
        $total = 0;
        $tax = TaxService::tax();
        foreach ($carts as $cart)
        {
            $total += ($cart->product->cost + $tax['value']) * $cart->count ;
        }
        return $total;

    }

    public function deleteAllCarts()
    {
        $user = (new AccountService())->getAccount();
        $status = $user['status'];
        $user = $user['user'];
        Cart::where('status',$status)->where('user_id',$user->id)->delete();
        $response =
            [
                'status' => 200,
                'message' => __('web.success'),
                'data' => '',
            ];
        return ResponseService::response($response);
    }

    public function deleteCart($id)
    {
        $user = (new AccountService())->getAccount();
        $status = $user['status'];
        $user = $user['user'];
        $cart = Cart::where('status',$status)->where('user_id',$user->id)->where('id',$id)->first();
        if(!$cart)
        {
            return ResponseService::responseFail([__('web.not_found')]);
        }
        $cart->delete();
        $cart_count = Cart::where('status',$status)->where('user_id',$user->id)->sum('count');
        $response =
        [
            'status' => 200,
            'message' => __('web.success'),
            'data' => [
                'count' => $cart_count,
                'data'=>$this->totalCart()
            ],
        ];
        return ResponseService::response($response);
    }
    public function checkAvailable()
    {
        $response = null;
        $user = (new AccountService())->getAccount();
        $status = $user['status'];
        $user = $user['user'];
        $carts = $this->carts()['data']['carts'];
        
        if(count($carts) == 0)
        {
            return __('web.cart_empty');
        }
        else
        {
            foreach ($carts as $cart)
            {
                if ($cart->choice_id)
                {
                    $choice = ProductAttributeChoice::find($cart->choice_id);
                }
                else
                {
                    $choice = Product::find($cart->product_id);
                }
                
                if($choice->count < $cart->count)
                {
                    return __('web.count_less'). ' '. $cart->product->currentDescription->name;
                }
            }
        }
        return $response;
    }
}
