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

use App\Models\Address;
use App\Models\Cobon;
use App\Models\CobonProduct;
use App\Models\Offer;
use App\Models\ShippingCity;
use App\Services\Auth\User\AccountService;
use App\Services\Currency\CurrencyService;
use App\Services\Product\Cart\CartService;
use App\Services\Product\Tax\TaxService;
use App\Services\Product\Shipping\ShippingService;
use App\Services\Product\Cobon\CobonService;

use Illuminate\Support\Facades\Session;

class OrderService
{
    public function calculate($requestContent)
    {
        $response = [];
        $currency = (new CurrencyService())->getCurrency();
        $cobon_total = 0;
        $total_tax = 0;
        $total = 0;
        $total_shipping = 0;
        $product_costs = 0;
        $total_products = 0;
        $total_products_without_cobon = 0;
        $seller_commission = 0;
        $total_commission = 0;
        $validate_cobon = [];
        $cobon_msg = '';
        $response['cobon'] = null;
        $carts = (new CartService())->carts()['data']['carts'];
        foreach ($carts as $key=>$cart)
        {
            $response['products'][$key]['cart_id'] = $cart->id;
            $response['products'][$key]['product_id'] = $cart->product_id;
            $response['products'][$key]['choice_id'] = $cart->choice_id;
            $response['products'][$key]['count'] = $cart->count;
            $response['products'][$key]['product_name'] = $cart->product->descriptions()->name;
            $response['products'][$key]['product_image'] = asset($cart->product->image);
            $product_cost = round($cart->product->cost * $cart->count ,2);
            
            $response['products'][$key]['product_cost'] =  $product_cost;
            
            $total_products_without_cobon += $product_cost;
            $response['products'][$key]['discount'] = 0;
            $response['cobon_id'] = null;
            $response['cobon_status'] = null;
            $response['products'][$key]['seller_commission'] = null;
            $response['products'][$key]['total_commission'] = 0;
            if(isset($requestContent['cobon']))
            {
                $cobon_calculate = (new CobonService())->calculate([
                        'total' => $product_cost,
                        'cobon' => $requestContent['cobon'],
                        'product_id' => $cart->product_id
                    ]);
                if($cobon_calculate['status'])
                {
                    array_push($validate_cobon,true);
                }
                else
                {
                    array_push($validate_cobon,false);
                }
            }
            // if(isset($requestContent['cobon']))
            // {
            //     $cobon_calculate = (new CobonService())->calculate([
            //             'total' => $product_cost,
            //             'cobon' => $requestContent['cobon'],
            //             'product_id' => $cart->product_id
            //         ]);
            //     if($cobon_calculate['status'])
            //     {
            //         $cobon_msg = __('web.valid_cobon');
            //     }
            //     else
            //     {
            //         $cobon_msg = __('web.invalid_cobon');
            //     }
            //     $response['products'][$key]['discount'] = round($cobon_calculate['discount'],2);
            //     $cobon_total += $cobon_calculate['discount'];
            //     if($cobon_calculate['discount'] != 0)
            //     {
            //         $response['cobon_id'] = $cobon_calculate['cobon'];
            //         $response['cobon_status'] = $cobon_calculate['status'];
            //     }
            //     $response['cobon_dicount'][] = $cobon_calculate['discount'];
            //     $product_cost = round($cobon_calculate['total'],2);
            //     $response['cobon'] = $cobon_calculate['cobon'];
            //     $seller_commission = $cobon_calculate['commission'] != 0 ? settings('seller_percentage') : 0;
            //     $total_commission += $cobon_calculate['commission'];
            //     $response['products'][$key]['seller_commission'] = $seller_commission;
            //     $response['products'][$key]['total_commission'] = round($cobon_calculate['commission'],2);
            //     $response['products'][$key]['product_cost'] =  $product_cost;
            //     $product_costs += $product_cost;
            // }
            // else
            // {
            //     $product_costs += $product_cost;
            // }
            $product_costs += $product_cost;
            $tax = TaxService::tax($product_cost);
            $response['products'][$key]['tax_valid'] =  $tax['valid'];
            $response['products'][$key]['tax'] =  $tax['value'];
            
            $total_tax +=  round($tax['value'],2);
            
            $product_total = $product_cost + $tax['value'];
            
            $response['products'][$key]['product_total'] = round( $product_total ,2);
            $total_products += $product_total;
        }
        
        if(isset($requestContent['cobon']))
        {
            if(in_array(true,$validate_cobon) && !in_array(false,$validate_cobon))
            {
                $cobon_calculate = (new CobonService())->calculate([
                            'total' => $total_products_without_cobon,
                            'cobon' => $requestContent['cobon']
                        ]);
                $cobon_total = $cobon_calculate['discount'];
                $total_products_without_cobon = $cobon_calculate['total'];
                $response['cobon_id'] = $cobon_calculate['cobon'];
                $response['cobon'] = $cobon_calculate['cobon'];
                $response['cobon_status'] = $cobon_calculate['status'];
                $total_commission = round($cobon_calculate['commission'],2);
                $seller_commission = $cobon_calculate['commission'] != 0 ? settings('seller_percentage') : 0;
                $tax = TaxService::tax($total_products_without_cobon);
                if($tax['valid'])
                {
                    $total_tax =  $tax['value'];
                }
                $total_products = $total_tax + $total_products_without_cobon;
                $cobon_msg = __('web.valid_cobon');
            }
            elseif(in_array(true,$validate_cobon) && in_array(true,$validate_cobon))
            {
                $cobon_msg = __('web.invalid_cobon_all_products');
            }
            else
            {
                $cobon_msg = __('web.invalid_cobon');
            }
        }
        
        
        $shipping_id = Session::get('shipping_id');

        $address_id = Session::get('location_id');
        $shipping = new ShippingService();
        $total_shipping = $shipping->calculate([
            'total' => $total_products_without_cobon,
            'address_id' => $address_id,
            'shipping_id' => $shipping_id,
        ]);
        
        $on_hand_payment = 0;
        
        if(settings('on_hand_status') == 'true')
        {
            if(session()->get('order_shipping_status') == 'when_recieving')
            {
                $on_hand_payment = round( settings('on_hand_value')  * $currency->currency,2);
            }
        }
        $response['seller_commission'] =  round($seller_commission,2);
        $response['total_commission'] =  round($total_commission,2);
        $response['product_costs'] = round($total_products_without_cobon,2);
        $response['product_total'] =  round($total_products,2);
        $response['tax_valid'] = settings('tax_valid');
        $response['total_tax'] =  round($total_tax,2);
        $response['cobon_total'] =  round($cobon_total,2);
        $response['shipping_discount'] =  $total_shipping['discount'];
        $response['shipping_status'] =  $total_shipping['status'];
        $response['total_before_shipping'] =  round($total_shipping['total'],2);
        $response['shipping_offer'] =  round($total_shipping['offer_percentage'],2);
        $response['total_shipping'] =  round($total_shipping['shipping_cost'],2);
        $response['on_hand_status'] =  settings('on_hand_status');
        $response['on_hand_payment'] =  round($on_hand_payment,2);
        $response['cobon_msg'] = $cobon_msg;
        $response['total_order'] =  round($total_products + $on_hand_payment + $total_shipping['shipping_cost'] ,2);
        return $response;
    }

    private function checkCobon($cobon,$product_id,$product_total)
    {

        $res =
        [
            'msg' => null,
            'discount' => 0,
            'cobon_id' => null,
            'seller_commission' => 0,
            'total_commission' => 0,
            'check_valid' => false,
        ];

        $cobon = Cobon::where('code',$cobon)->where('count','>',0)->where('start_date','<=',date('Y-m-d'))->where('end_date','>=',date('Y-m-d'))->orderBy('id','DESC')->first();

        if($cobon)
        {
            $check_product_cobon = CobonProduct::where('product_id',$product_id)->where('cobon_id',$cobon->id)->first();

            $currency = (new CurrencyService())->getCurrency();
            if($cobon->status == 'all' || $check_product_cobon)
            {
                $res['cobon_id'] = $cobon->id;

                if($cobon->discount_type == '1')
                {
                    $discount = round( $cobon->discount_amount * $currency->currency,2);

                    $msg = __('web.cobon_discount').' ' . $discount . $currency->abbr;
                }
                else
                {
                    $discount =  round( ($product_total * ($cobon->discount_amount/100)) * $currency->currency,2);
                    $msg = __('web.cobon_discount').' ' . $discount . $currency->symbol;
                }
                if($cobon->seller_id)
                {
                    $res['seller_commission'] = settings('seller_percentage');
                    $total_commission = (settings('seller_percentage') / 100) * ($product_total - $discount);
                    $res['total_commission'] = round($total_commission,2);

                }

                 $res['msg'] = $msg;
                $res['discount'] = $discount;
                $res['check_valid'] = true;
            }
        }
        return $res;
    }


}
