import { clientFetch } from '@/lib/api/client';
import { unwrap, type ApiEnvelope } from '@/lib/api/types';
import type { City, District, GeographyApi, Province } from '../types';

const BASE = '/api/v1/geo';

/**
 * Real HTTP implementation of the GeographyApi seam (b4 public `geo/*` lookups, no auth). The
 * server returns active-only, `sortOrder`-ordered rows, so the client does no re-filtering or
 * re-sorting beyond what the contract guarantees. Query params are snake_case
 * (`province_id`/`city_id`); the response bodies are camelCase. Selected once USE_GEOGRAPHY_MOCK
 * is false.
 */
export const geographyClientApi: GeographyApi = {
  listProvinces: async () => unwrap(await clientFetch<ApiEnvelope<Province[]>>(`${BASE}/provinces`)),

  listCities: async (provinceId: number) =>
    unwrap(await clientFetch<ApiEnvelope<City[]>>(`${BASE}/cities?province_id=${provinceId}`)),

  listDistricts: async (cityId: number) =>
    unwrap(await clientFetch<ApiEnvelope<District[]>>(`${BASE}/districts?city_id=${cityId}`)),
};
