'use client'

import { useEffect, useState } from 'react'
import ComboBox from '@/app/components/ComboBox'

export default function TeamSelector() {
  const [teams, setTeams] = useState<string[]>([])
  const [selectedTeam, setSelectedTeam] = useState('')

  useEffect(() => {
    const fetchTeams = async () => {
      try {
        const res = await fetch('/api/team/list')
        const data = await res.json()
        setTeams(data.teams ?? []) // Fallback zu leerem Array
      } catch (err) {
        console.error('Fehler beim Laden der Teams:', err)
      }
    }

    fetchTeams()
  }, [])

  return (
    <ComboBox
      value={selectedTeam}
      items={teams}
      onSelect={setSelectedTeam}
    />
  )
}
