/**
 * シルギア 児童手当API v1 の型定義
 * https://api.shirugear.com/jidou/
 *
 * 児童手当の月額（年齢の区分・第何子ごと）、支給の対象年齢、第3子以降の数え方、支払の月、
 * 2024年9月分までの所得制限限度額・所得上限限度額と特例給付を、月の分で区切った時期ごと（2012年4月分から）に返します。
 * 金額はすべて円の整数です。
 *
 * 互換の方針: v1 の中では項目を「足す」ことはあっても、名前や意味は変えません。
 * 知らない項目が増えていても無視してください。
 */

/** 区分の最後の月の決め方 */
export interface Ends {
  /** 年齢 */
  age: number;
  /** birth_month = その年齢の誕生日の属する月まで / march31 = その年齢に達する日以後の最初の3月31日（の属する3月）まで */
  at: "birth_month" | "march31";
}

export interface Band {
  /** 区分の名前。今の時期は "under3"・"3to18"、2024年9月分までは "under3"・"3to12"・"12to15" */
  band: "under3" | "3to18" | "3to12" | "12to15";
  /** 区分の名前（日本語） */
  label: string;
  /** この区分の最後の月 */
  ends: Ends;
  /** 第1子・第2子の月額 */
  amount: number;
  /** 第3子以降の月額 */
  third_plus: number;
}

export interface LimitTable {
  /** 扶養親族等と児童がいないときの限度額（所得・円） */
  base: number;
  /** 扶養親族等・児童1人ごとに足す額 */
  per_person: number;
  /** 70歳以上の同一生計配偶者・老人扶養親族1人ごとに足す額 */
  per_elderly: number;
  /** 人数ごとの限度額（老人扶養親族などがいない場合） */
  examples: { persons: number; limit: number }[];
}

export interface Period {
  /** 時期の名前（"2024-10" など。最初の月） */
  key: string;
  /** この時期の最初の月の1日 */
  from: string;
  /** 最後の月の末日。今の時期は null */
  until: string | null;
  label: string;
  /** 支給の対象になる子の年齢（until_age 歳に達する日以後の最初の3月31日まで） */
  target: { until_age: number; rule: string };
  /** 第何子かを数える範囲（until_age 歳に達する日以後の最初の3月31日まで） */
  count: { until_age: number; rule: string };
  /** 年齢の区分ごとの月額（年齢の低い順） */
  amounts: Band[];
  /** 所得制限限度額。所得制限が無い時期は null */
  income_limit: LimitTable | null;
  /** 所得上限限度額（これ以上は特例給付も無し）。無い時期は null */
  income_cap: LimitTable | null;
  /** 特例給付（所得制限限度額以上の人への給付）。無い時期は null */
  special_benefit: { amount: number; rule: string } | null;
  /** 支払の月（それぞれの前月までの分を支払う） */
  payment: { months: number[]; times: number; months_per_payment: number; rule: string };
  note: string;
  law: string;
}

export interface Rates {
  name: string;
  currency: "JPY";
  /** 時期の古い順 */
  periods: Period[];
  /** 第3子以降の数え方・年齢の区切り・所得の判定・支給の始まりと終わり（文で説明） */
  rules: {
    order: { rule: string; condition: string; law: string };
    age: { under3: string; march31: string; law: string };
    income: { rule: string; law: string };
    start: { rule: string; law: string };
    end: string;
    public_servant: string;
    tax: string;
  };
  data_version: string;
}

export interface Meta {
  data_version: string;
  checked_at: string;
  files: "rates"[];
  periods: { key: string; from: string; until: string | null }[];
  scope: string;
  notice: string;
  sources: { label: string; url: string }[];
}

export interface ApiError {
  error: {
    status: number;
    code: "invalid_request" | "not_found" | "region_not_allowed" | "method_not_allowed";
    message: string;
  };
}
