import { useEffect } from 'react'
import { usePage } from '@inertiajs/react'
import { useStore } from '@/store'
import { useTradingStore } from '@/store/tradingStore'

export function AppInit() {
  const { auth } = usePage().props
  const { loadBalance, syncFromBackend } = useStore()
  const { fetchAssetSettings } = useTradingStore()

  useEffect(() => {
    // Public endpoint — no auth required
    fetchAssetSettings()
  // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [])

  useEffect(() => {
    if (auth?.user) {
      loadBalance()
      syncFromBackend()
    }
  // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [auth?.user?.id])

  return null
}
