'use client'

import { useEffect } from 'react'
import { useProductStore } from '@/stores/product-store'
import { useProductWorkspaceStore } from '@/stores/product-workspace/store'
import { debugProps } from '@/lib/debug'

// PBI-74 / T-847: zet zowel oude useProductStore.setCurrentProduct als de
// nieuwe workspace-store.setActiveProduct. setActiveProduct triggert
// ensureProductLoaded met een requestId-guard; de fetch-stub levert tijdens
// Story 2 nog geen echte data — echte LIST-endpoints komen in Story 7
// (T-870). Restore-hint flow volgt in Story 4 (T-857).
export function SetCurrentProduct({ id, name }: { id: string; name: string }) {
  const { setCurrentProduct, clearCurrentProduct } = useProductStore()

  useEffect(() => {
    setCurrentProduct(id, name)
    useProductWorkspaceStore.getState().setActiveProduct({ id, name })
    return () => {
      clearCurrentProduct()
      useProductWorkspaceStore.getState().setActiveProduct(null)
    }
  }, [id, name, setCurrentProduct, clearCurrentProduct])

  return <span {...debugProps('set-current-product')} hidden />
}
