/** FEAT-NAMEGEN: Namensvorschläge — Schnittstelle zum Backend /names/suggest. */
import { api } from './client'

export interface NameSuggestion {
  name: string
  meaning: string
  origin: string
}

export const NAMEGEN_USAGES = [
  { code: 'norn', label: 'Nordisch' },
  { code: 'japa', label: 'Japanisch' },
  { code: 'mythg', label: 'Griech. Mythologie' },
  { code: 'ger', label: 'Deutsch' },
  { code: 'arb', label: 'Arabisch' },
] as const

export type NamegenUsageCode = (typeof NAMEGEN_USAGES)[number]['code']

export interface SuggestNamesParams {
  letter?: string
  gender?: string
  usages: NamegenUsageCode[]
  count?: number
}

/** Exported for unit tests — builds the query path without a network call. */
export function buildSuggestPath(params: SuggestNamesParams): string {
  const parts: string[] = []
  const letter = params.letter?.trim().toUpperCase()
  if (letter) parts.push(`letter=${encodeURIComponent(letter)}`)
  if (params.gender && params.gender !== '') parts.push(`gender=${encodeURIComponent(params.gender)}`)
  if (params.usages.length > 0) parts.push(`usages=${params.usages.join(',')}`)
  parts.push(`count=${params.count ?? 6}`)
  return `/names/suggest?${parts.join('&')}`
}

export function suggestNames(params: SuggestNamesParams): Promise<NameSuggestion[]> {
  return api.get<NameSuggestion[]>(buildSuggestPath(params))
}
