/**
 * シルギア 郵便料金API v1 の型定義
 * https://api.shirugear.com/postage/
 *
 * 日本国内あての郵便物の基本料金（税込）だけを返します（速達・書留などのオプションは含みません）。
 * 料金は日本郵便の料金表と照らし合わせた値で、照合した日を data_version で返します。
 *
 * 互換の方針: v1 の中では項目や種類を「足す」ことはあっても、名前や意味は変えません。
 * 知らない項目・種類が増えていても無視してください。
 */

/** 種類 */
export type PostageKey =
  | "teikei"
  | "teikeigai_in"
  | "teikeigai_out"
  | "hagaki"
  | "ofuku"
  | "minileter"
  | "smartletter"
  | "lp_light"
  | "lp_plus"
  | "yu_packet";

/** 重さの区分。重さが max_g 以下なら price */
export interface PostageTier {
  /** その重さまで（g） */
  max_g: number;
  /** 料金（円・税込） */
  price: number;
}

/** サイズ・重さの条件（mm・g）。決まりが無い項目は null */
export interface PostageLimits {
  long_min_mm: number | null;
  long_max_mm: number | null;
  short_min_mm: number | null;
  short_max_mm: number | null;
  thickness_max_mm: number | null;
  /** 長辺＋短辺＋厚さの合計の上限 */
  sum_max_mm: number | null;
  weight_max_g: number | null;
}

export interface PostageSource {
  label: string;
  url: string;
}

/** 1種類（rates.json の services の1件） */
export interface PostageService {
  key: PostageKey;
  /** 日本郵便の呼び名 */
  name: string;
  group: "letter" | "postcard" | "envelope" | "parcel";
  /** by_weight＝tiers を見る、flat＝price を見る */
  pricing: "by_weight" | "flat";
  /** 決まった料金（円・税込）。by_weight のときは null */
  price: number | null;
  /** 重さの区分（軽い順）。flat のときは null */
  tiers: PostageTier[] | null;
  /** 送れる重さの上限（g）。記載が無いもの（はがき）は null */
  weight_max_g: number | null;
  /** サイズ・重さの条件。はがきは null */
  limits: PostageLimits | null;
  note: string;
  /** 照らし合わせた日本郵便のページ */
  source: PostageSource;
}

interface Common {
  currency: "JPY";
  tax: "included";
  /** 日本郵便のページと照らし合わせた日 "YYYY-MM-DD" */
  data_version: string;
}

/** GET /postage/v1/{key}.json */
export type PostageServiceResponse = PostageService & Common;

/** GET /postage/v1/rates.json */
export interface PostageRatesResponse extends Common {
  services: PostageService[];
}

/** GET /postage/v1/meta.json */
export interface PostageMeta {
  data_version: string;
  checked_at: string;
  scope: string;
  services: PostageKey[];
  counts: { services: number };
  sources: PostageSource[];
}

/** エラー（400・403・404・405） */
export interface PostageError {
  error: {
    status: number;
    code: "invalid_request" | "region_not_allowed" | "not_found" | "method_not_allowed";
    message: string;
  };
}
