import axios from 'axios'

const baseURL = import.meta.env.VITE_API_BASE_URL || '/api'

export const api = axios.create({
  baseURL,
  headers: { 'Content-Type': 'application/json' },
})

export type MemoryFeatureType =
  | 'heart_window'
  | 'fissure_page'
  | 'standard_nepal'
  | 'first_meet'
  | 'first_meet_ju'
  | 'first_meet_rui'
  | 'first_meet_train'
  | 'first_meet_taix'
  | 'first_meet_quanji'
  | 'make_love'

export type NepalArtStyle =
  | 'none'
  | 'mandala_pink'
  | 'mandala_blue'
  | 'spiral_rose'
  | 'spiral_cyan'
  | 'dawn_meet'
  | 'blush_argue'
  | 'mint_reconcile'
  | 'saffron_home'
  | 'indigo_ikea'
  | 'lotus_petal'
  | 'lokta_gold'
  | 'ocean_prayer'
  | 'desert_rose'
  | 'night_star'
  | 'terra_torn'

export interface Memory {
  id: number
  story_order: number
  date: string
  event_title: string
  main_text: string
  unique_feature_type: MemoryFeatureType
  nepal_art_style: NepalArtStyle
  /** 首张图 URL，与 `images[0]` 一致 */
  image: string | null
  /** 主图 + 附加图（按后台顺序）；仅一张时不轮播 */
  images: string[]
  is_finale: boolean
}

export interface StatusPayload {
  server_time: string
  unlocked: boolean
  unlock_at: string | null
  together_since: string
  site_title: string
}

export interface GuestbookMessage {
  id: number
  nickname: string
  message: string
  created_at: string
}

export async function fetchMemories(): Promise<Memory[]> {
  const { data } = await api.get<Memory[]>('/memories/')
  return data
}

export async function fetchStatus(): Promise<StatusPayload> {
  const { data } = await api.get<StatusPayload>('/status/')
  return data
}

export async function postMessage(payload: {
  nickname: string
  message: string
}): Promise<GuestbookMessage> {
  const { data } = await api.post<GuestbookMessage>('/messages/', payload)
  return data
}
