/**
 * Japan Post API - Mapper
 *
 * Transforms Japan Post API responses to domain types.
 * Single transformation point: Raw API -> Domain type
 */

import type { JapanPostAddressRecord, JapanPostSearchResponse } from "./raw.types.js";
import { japanPostSearchResponseSchema } from "./raw.types.js";
import type { JapanPostAddress, AddressLookupResult } from "../../schema.js";

/**
 * Transform a single Japan Post address record to domain type
 */
export function transformJapanPostAddress(raw: JapanPostAddressRecord): JapanPostAddress {
  // Get ZIP code from either field name
  const zipCode = raw.zipcode || raw.zip_code || "";

  return {
    zipCode,
    // Japanese
    prefecture: raw.pref_name || "",
    prefectureKana: raw.pref_kana ?? undefined,
    city: raw.city_name || "",
    cityKana: raw.city_kana ?? undefined,
    town: raw.town_name || "",
    townKana: raw.town_kana ?? undefined,
    // Romanized
    prefectureRoma: raw.pref_roma || "",
    cityRoma: raw.city_roma || "",
    townRoma: raw.town_roma || "",
  };
}

/**
 * Transform Japan Post search response to domain AddressLookupResult
 */
export function transformJapanPostSearchResponse(raw: unknown): AddressLookupResult {
  const parsed = japanPostSearchResponseSchema.parse(raw);

  // Get ZIP code from first address or empty string
  const firstAddress = parsed.addresses[0];
  const zipCode = firstAddress?.zipcode || firstAddress?.zip_code || "";

  return {
    zipCode,
    addresses: parsed.addresses.map(transformJapanPostAddress),
    count: parsed.count,
  };
}

/**
 * Parse and validate raw Japan Post API response
 */
export function parseJapanPostSearchResponse(raw: unknown): JapanPostSearchResponse {
  return japanPostSearchResponseSchema.parse(raw);
}
