export type PlaceItem = { code: string name: string } export type CreateBirthProfileRequest = { name: string birth_date: string birth_time?: string | null time_confidence: 'exact' | 'approximate' | 'unknown' | 'rectified' birth_place_adcode?: string | null birth_place?: string | null } export type CreateBirthProfileResponse = { profile_id: string resolved_location: { lat: number; lng: number; timezone: string } } export type CreateNatalChartRequest = { profile_id: string calculation_profile: { ephemeris: 'DE440' | 'DE441' zodiac: 'tropical' house_system: 'whole_sign' | 'equal' | 'porphyry' | 'placidus' orb_profile: string position_type: 'apparent' | 'astrometric' ecliptic_epoch: 'date' | 'J2000' } } export type CreateChartResponse = { chart_id: string chart_json: Record } export type HealthResponse = { status: string } export function apiBase(): string { return process.env.NEXT_PUBLIC_API_BASE_URL ?? '' } async function apiGet(path: string, init?: RequestInit): Promise { const base = apiBase() const url = base ? `${base}${path}` : `/api${path}` const res = await fetch(url, { cache: 'no-store', ...init }) if (!res.ok) throw new Error((await res.text()) || `http_${res.status}`) return (await res.json()) as T } async function apiPost(path: string, body: unknown): Promise { const base = apiBase() const url = base ? `${base}${path}` : `/api${path}` const res = await fetch(url, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body), cache: 'no-store', }) if (!res.ok) throw new Error((await res.text()) || `http_${res.status}`) return (await res.json()) as T } export function getCnProvinces(): Promise { return apiGet('/places/cn/provinces') } export function getCnCities(provinceCode: string): Promise { return apiGet(`/places/cn/cities?province_code=${encodeURIComponent(provinceCode)}`) } export function getCnCounties(cityCode: string): Promise { return apiGet(`/places/cn/counties?city_code=${encodeURIComponent(cityCode)}`) } export function createBirthProfile(req: CreateBirthProfileRequest): Promise { return apiPost('/birth-profiles', req) } export function createNatalChart(req: CreateNatalChartRequest): Promise { return apiPost('/charts/natal', req) } export function getHealth(): Promise { return apiGet('/health') }