<?php

namespace TPS\Shop\Repositories;
use Webkul\Customer\Repositories\WishlistRepository as WishlistRepo;
class WishlistRepository extends WishlistRepo
{
    /**
     * Get customer wishlist items.
     *
     * @return \Illuminate\Support\Collection
     */
    public function getCustomerWishlist()
    {
        /* due to ambigious ids only selecting from wishlist table */
        $query = $this->model->select('wishlist.*');

        /* don't add product repository method as that one will need a product flat table */
        if (! core()->getConfigData('catalog.products.homepage.out_of_stock_items')) {
            $query = $query
                ->leftJoin('products as ps', 'wishlist.product_id', '=', 'ps.id')
                ->leftJoin('product_inventories as pv', 'ps.id', '=', 'pv.product_id')
                ->where(function ($qb) {
                    $qb
                        ->WhereIn('ps.type', ['configurable', 'grouped', 'downloadable', 'bundle', 'booking'])
                        ->orwhereIn('ps.type', ['simple', 'virtual'])->where('pv.qty', '>', 0);
                });
        }

        /* main check to determine the wishlist */
        return $query->where([
            'channel_id'  => core()->getCurrentChannel()->id,
            'customer_id' => auth()->guard('customer')->user()->id,
        ])->paginate(8);
    }
}