<?php

namespace App\Repositories\Member\Cart;

use App\Models\Cart;
use App\Models\ItemReference;
use Illuminate\Support\Facades\DB;

class MemberCartRepository
{
    static public function getCount($locationId = null)
    {
        $location = $locationId ?? session('location_id', 22);
        return Cart::where('user_id', auth()->user()->id)
            ->where('location_id', $location)
            ->sum('qty');
    }

    static public function getSubtotal($locationId = null)
    {
        $location = $locationId ?? session('location_id', 22);
        return Cart::where('user_id', auth()->user()->id)
            ->where('location_id', $location)
            ->get()
            ->map(function($row){
                return $row->qty * $row->display_price;
            })
            ->sum();
    }

    static public function clearAll($locationId = null)
    {
        $location = $locationId ?? session('location_id', 22);
        return Cart::where('user_id', auth()->user()->id)
            ->where('location_id', $location)
            ->delete();
    }



    public function getList($request)
    {
        $location = (int) $request->input("location_id");
        $cart = Cart::where('user_id', auth()->user()->id)
            ->where("location_id", $location)
            ->orderBy('created_at','desc')
            ->paginate($request->limit ?? 1000);

        return $cart;
    }

    public function create($data)
    {
        $model = DB::transaction(function () use ($data) {
            $location = (int) @$data["location_id"];
            $itemreference = ItemReference::findOrfail($data['item_reference_id']);

            $model = Cart::where('item_reference_id', $data['item_reference_id'])
                ->where('location_id', $location)
                ->where('user_id', auth()->user()->id)->first();
            if ($model) {
                $model->qty = $model->qty + $data['qty'];
                $model->save();
            } else {
                $model = Cart::create([
                    'item_id' => $itemreference->item_id,
                    'item_variant_id' => $itemreference->item_variant_id,
                    'location_id' => $data['location_id'],
                    'item_reference_id' => $data['item_reference_id'],
                    'user_id' => auth()->user()->id,
                    'qty' => $data['qty']
                ]);
            }

            return $model;
        });

        return $model;
    }

    public function update($cartId, $data)
    {
        $cart = DB::transaction(function () use ($cartId, $data) {
            $cart = Cart::find($cartId);
            
            if (!$cart) {
                throw new \Exception('Cart item not found');
            }

            $cart->qty = $data['qty'] ?? $cart->qty;
            $cart->save();

            return $cart;
        });

        return $cart;
    }

    public function delete($cartId)
    {
        $cart = DB::transaction(function () use ($cartId) {
            $cart = Cart::find($cartId);
            $cart->delete();
            return $cart;
        });

        return $cart;
    }

}
