import type { Expert } from '@/types'
import { mapBackendExpert, type BackendExpert, type BackendExpertSlot } from '@/lib/backend'
import { serverFetchJson } from '@/lib/server-api'

async function fetchJson<T>(path: string): Promise<T | null> {
  return serverFetchJson<T>(path)
}

export async function getExperts(): Promise<Expert[]> {
  const data = await fetchJson<{ items: BackendExpert[] }>('/experts')
  return (data?.items || []).map(mapBackendExpert)
}

export async function getExpert(slug: string): Promise<Expert | null> {
  const data = await fetchJson<BackendExpert>(`/experts/${encodeURIComponent(slug)}`)
  return data ? mapBackendExpert(data) : null
}

export async function getExpertSlots(slug: string): Promise<BackendExpertSlot[]> {
  const data = await fetchJson<{ items: BackendExpertSlot[] }>(`/experts/${encodeURIComponent(slug)}/slots?status=open`)
  return data?.items || []
}
