export interface RadioPlace {
  id: string
  title: string
  country: string
  lat: number
  lng: number
  size: number
}

export interface RadioChannel {
  id: string
  title: string
  placeTitle: string
  country: string
  website?: string
}

export interface RadioSearchResult {
  stations: Array<{
    id: string
    title: string
    placeId: string
    placeTitle: string
    country: string
  }>
  places: Array<{
    id: string
    title: string
    country: string
  }>
}

export async function getPlaces(): Promise<RadioPlace[]> {
  const res = await fetch('/api/radio/places')
  if (!res.ok) throw new Error('Failed to load radio places')
  return res.json()
}

export async function getChannels(placeId: string): Promise<RadioChannel[]> {
  const res = await fetch(`/api/radio/channels/${placeId}`)
  if (!res.ok) throw new Error('Failed to load channels')
  return res.json()
}

export async function resolveStreamUrl(channelId: string): Promise<string> {
  const res = await fetch(`/api/radio/stream/${channelId}`)
  if (!res.ok) throw new Error('Failed to resolve stream')
  const data = await res.json()
  return data.url
}

export async function searchRadio(q: string): Promise<RadioSearchResult> {
  const res = await fetch(`/api/radio/search?q=${encodeURIComponent(q)}`)
  if (!res.ok) throw new Error('Search failed')
  return res.json()
}
