<?php

namespace TPS\API\Http\Controllers;

use Illuminate\Http\Request;
use TPS\API\Http\Resources\Customer\WishListResource;
use Webkul\RestApi\Http\Controllers\V1\Shop\Customer\WishlistController;
use Webkul\RestApi\Http\Resources\V1\Shop\Customer\CustomerWishlistResource;

class Wishlists extends WishlistController
{
    /**
     * Get customer wishlist.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $customer = $request->user();

        return response([
            'data' => WishListResource::collection($customer->wishlist_items()->get()),
        ]);
    }
    /**
     * Add or remote item from wishlist.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function addOrRemove(Request $request, $id)
    {
        $customer = $request->user();

        $wishlistItem = $this->wishlistRepository->findOneWhere([
            'channel_id'  => core()->getCurrentChannel()->id,
            'product_id'  => $id,
            'customer_id' => $customer->id,
        ]);

        if ($wishlistItem) {
            $this->wishlistRepository->delete($wishlistItem->id);

            return response([
                'data'    => null,
                'message' => __('shop::app.response.remove-success', ['name' => trans('shop::app.header.wishlist')]),
            ]);
        }

        $wishlistItem = $this->wishlistRepository->create([
            'channel_id'  => core()->getCurrentChannel()->id,
            'product_id'  => $id,
            'customer_id' => $customer->id,
        ]);

        return response([
            'data'    => new WishListResource($wishlistItem),
            'message' => __('shop::app.response.add-success', ['name' => trans('shop::app.header.wishlist')]), //todo translate
        ]);
    }
}