<?php

namespace App\Http\Controllers;

use App\Models\ProfitCenter;

class ProductController extends Controller
{
    //
    public function toggleFavorites($productId)
    {
        // $productId=878;
        $profitCenterId = auth()->user()->profitCenters()->first()->id;
        debug([$productId, $profitCenterId]);
        $ProfitCenter = ProfitCenter::find($profitCenterId);
        $favorites = $ProfitCenter->favorites()->where('product_id', '=', $productId)->get()->pluck('id')->toArray();
        if (! in_array($productId, $favorites)) {
            $currentStatus = false;
            debug('berakjuk');
            $ProfitCenter->favorites()->attach($productId);
            $currentStatus = true;
        } else {
            debug('kivesszuk');
            $currentStatus = true;
            $ProfitCenter->favorites()->detach($productId);
            $currentStatus = false;

        }

        return \response()->json([
            'success' => true,
            'status' => $currentStatus,
        ]);

    }
}
