'use client'

import { useState, useCallback, useEffect, useRef } from 'react'
import { Language } from '@/lib/types'
import { useAuth } from '@/lib/hooks/useAuth'
import PitchDeck from '@/components/PitchDeck'

export default function Home() {
  const { investor, loading, logout } = useAuth()
  const [lang, setLang] = useState<Language>('de')
  const [langReady, setLangReady] = useState(false)
  const synced = useRef(false)

  useEffect(() => {
    if (!loading && !synced.current) {
      synced.current = true
      if (investor?.preferred_lang === 'en') setLang('en')
      setLangReady(true)
    }
  }, [loading, investor])

  const toggleLanguage = useCallback(() => {
    setLang(prev => prev === 'de' ? 'en' : 'de')
  }, [])

  if (loading || !langReady) {
    return (
      <div className="h-screen flex items-center justify-center">
        <div className="w-12 h-12 border-2 border-indigo-500 border-t-transparent rounded-full animate-spin" />
      </div>
    )
  }

  return <PitchDeck lang={lang} onToggleLanguage={toggleLanguage} investor={investor} onLogout={logout} />
}
