<?php

namespace App\Services\Repositories;

use App\Models\Cart;
use App\Models\Order;
use App\Models\Reservation;
use App\Services\Interfaces\CartRepositoryInterface;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;

class CartRepository implements CartRepositoryInterface
{
    public function get(Request $request)
    {
        $user = Auth::user();
        $query = Cart::where('user_id',$user->id)->orderBy('id', 'DESC');

        if ($request->paginate ?? null) {
            $carts= $query->paginate(50);
        }else{
            $carts=$query->cursor();
        }

        return $carts;
    }

    //
    public function save(Request $request, $product_id)
    {
        $user = Auth::user();
        $data= $request->all();
        unset($data['id']);// for security reasons
        $cart = Cart::where('user_id',$user->id)->where('product_id',$product_id)->first();

        if (!$cart) {
            $cart = Cart::create($data);
        } else {
            $data['count'] = isset($data['plus']) ? $cart->count + $request->count : $request->count;
            $cart->update($data);
        }
        return $this->get($request);
    }

    public function calculate(Request $request)
    {
        $carts = $this->get($request);
        $total_products = 0;
        $total_without_tax = 0; //NEWLY_ADDED
        $total = 0;
        foreach ($carts as $row)
        {
            $total_products += $row->product->price * $row->count;
            $total_without_tax += getAmountDetails($row->product->price)['amount'] * $row->count; //NEWLY_ADDED
        }
        // $tax = $total_products * (settings('tax') / 100) ;
        // $total_without_tax = $total_products - $tax;
        // $total = $total_products;
        $cobon_id = null;
        $cobon = $request->cobon;
        $discount = 0;
        $cobon_msg = '';
        if ($request->cobon)
        {
            $cobon_id = null;
            $cobon = (new CobonRepository())->get($request)->first();

            if($cobon)
            {
                $request->merge(['cobon_id'=>$cobon->id]);
                $count  = Order::whereHas('payment', function($q) use($cobon_id){
                    $q->where('cobon_id', $cobon_id);
                })->count();

                if($cobon->count_user >= $count)
                {
                    if($cobon->discount_type == 'percentage')
                    {
                        // $discount = $total * ($cobon->discount / 100) ;
                        $discount = $total_without_tax * ($cobon->discount / 100) ; //NEWLY_ADDED
                    }
                    else
                    {
                        $discount = $cobon->discount;
                    }
                    $cobon_id = $cobon->id;
                    $cobon_msg = __('web.valid_cobon') .' : '.$discount .' '.__('web.sar');
                }
                else
                {
                    $cobon_msg = __('web.not_valid_cobon');
                }
            }
            else
            {
                $cobon_msg = __('web.not_valid_cobon');
            }
        }
        $total_without_tax -= $discount;
        // $total_without_tax = round($total_without_tax - $discount, 2);
        $reservation_id = Session::get('reservation_id');
        $reservation = Reservation::onlyTrashed()->find($reservation_id);
        $total_reservation = 0;
        // if ($reservation)
        // {
        //     $total_reservation = $reservation->payment->total;
        //     $total += $total_reservation;
        //     $total_without_tax += $total_reservation;
        // }

        $total = $total < 0 ? 0 : $total;
        $tax = $total_without_tax * (settings('tax') / 100) ;
        // dd($total_products, $discount, $tax, $total);
        // $total += $tax;
        //NEWLY_ADDED
        // $total_without_tax = $total_without_tax < 0 ? 0 : $total_without_tax;
        // $applyTaxResult = applyTax($total_without_tax);
        // $total_with_tax = $applyTaxResult['amount'];
        // $tax = $applyTaxResult['tax'];

        return [
            'total_products' => round($total_without_tax + $discount,2),
            // 'total_products' => $total_products, //NEWLY_ADDED
            'total_reservation' => $total_reservation,
            'reservation_id' => $reservation_id,
            'discount' => $discount,
            'cobon' => $cobon,
            'cobon_id' => $cobon_id,
            'cobon_msg' => $cobon_msg,
            'tax' => round($tax,2),
            'total' => round($total_without_tax + $tax ,2),
            'final_total' => round($total_without_tax  + $tax ,2),
            // 'total' => $total_with_tax, //NEWLY_ADDED
            // 'final_total' => $total_with_tax, //NEWLY_ADDED
        ];
    }

    /**
     * Remove the specified client from storage.
     * @param Request $request
     * @param int $id
     * @return get All
     */
    public function destroy(Request $request, int $id)
    {
        $cart = Cart::findOrFail($id);
        $cart->delete();
        return $this->get($request);
    }


}
