export type NarratorContext =
  | 'intro'
  | 'transition_projects'
  | 'transition_skills'
  | 'transition_testimonials'
  | 'transition_journey'
  | 'hint'
  | 'encouragement_25'
  | 'encouragement_50'
  | 'encouragement_75'
  | 'contact_unlocked'
  | 'welcome_back'

export type HeroType = 'recruteur' | 'client' | 'dev'

export interface NarratorTextData {
  context: string
  text: string
  variant: number
  hero_type: HeroType | null
}

interface NarratorTextResponse {
  data: NarratorTextData
  meta: { lang: string }
}

export function useFetchNarratorText() {
  const config = useRuntimeConfig()
  const { locale } = useI18n()

  async function fetchText(context: NarratorContext, heroType?: HeroType): Promise<NarratorTextData | null> {
    try {
      const url = heroType
        ? `/narrator/${context}?hero=${heroType}`
        : `/narrator/${context}`

      const response = await $fetch<NarratorTextResponse>(url, {
        baseURL: config.public.apiUrl as string,
        headers: {
          'X-API-Key': config.public.apiKey as string,
          'Accept-Language': locale.value,
        },
      })

      return response.data
    } catch {
      return null
    }
  }

  return { fetchText }
}
