/**
 * シルギア 健康保険の給付API v1 の型定義
 * https://api.shirugear.com/kenpo/
 *
 * 出産育児一時金の額（時期ごと・産科医療補償制度の加算の有無）、出産手当金・傷病手当金の計算のしかた
 * （標準報酬月額の平均 ÷ 30 × 2/3・端数・12か月に満たないとき・支給期間・待期）、
 * 標準報酬月額の等級表（健康保険・厚生年金保険。時期ごと）を返します。金額はすべて円の整数です。
 * 保険料率は保険料率API（https://api.shirugear.com/shaho/）にあります。
 *
 * 互換の方針: v1 の中では項目を「足す」ことはあっても、名前や意味は変えません。
 * 知らない項目が増えていても無視してください。
 */

/** 出産育児一時金の1つの時期（/kenpo/v1/birth.json の periods） */
export interface BirthPeriod {
  /** 時期の名前（"2023-04" など） */
  key: string;
  /** この額が使われる最初の出産の日 */
  from: string;
  /** 最後の出産の日。今の時期は null */
  until: string | null;
  /** 産科医療補償制度の対象の出産（加入している医療機関で在胎週数22週以降）の額。1児につき */
  amount: number;
  /** 加算なしの額（制度に加入していない医療機関での出産・在胎週数22週未満の出産） */
  base: number;
  /** 産科医療補償制度の掛金分の加算（amount − base）。制度が無かった時期（2008年12月まで）は null */
  sanka_addition: number | null;
  /** 根拠（政令・資料） */
  basis: string;
}

export interface Birth {
  name: string;
  currency: "JPY";
  /** 古い順（1994年10月から） */
  periods: BirthPeriod[];
  /** 条件・多胎・家族出産育児一時金・直接支払制度など（文で説明） */
  rules: Record<string, string>;
  /** 公布済みで施行日が決まっていない改正（令和8年法律第31号の分娩費・出産時一時金） */
  upcoming: { law: string; change: string; from: string | null; from_note: string };
  data_version: string;
}

/** 期間つきの額（kyoukai_average） */
export interface DatedAmount {
  /** 最初の日。分からないときは null */
  from: string | null;
  /** 最後の日。今の額は null */
  until: string | null;
  amount: number;
}

export interface Allowance {
  name: string;
  currency: "JPY";
  rules: {
    /** 1日あたりの額の式と端数（÷30 は10円未満の四捨五入・×2/3 は1円未満の四捨五入）。months は 12 */
    daily: { rule: string; round_30: string; round_2_3: string; months: number; law: string };
    /** 12か月に満たないとき。kyoukai_average は協会けんぽの上限（支給を始める日で選ぶ） */
    short_tenure: { rule: string; kyoukai_average: DatedAmount[]; kyoukai_note: string; law: string };
    /** 今の計算式が使われる最初の日（"2016-04-01"） */
    since: string;
    before_2016: string;
    /** 出産手当金の日数（産前42日・多胎98日・産後56日） */
    maternity: { before_days: number; before_days_multiple: number; after_days: number; rule: string; late: string; with_sickness: string; law: string };
    /** 傷病手当金の待期（3日）と支給期間（通算1年6か月） */
    sickness: { waiting_days: number; waiting: string; max_period: string; max_period_since: string; max_period_note: string; law: string };
    paid_wage: string;
    continuation: string;
    tax: string;
  };
  data_version: string;
}

/** 等級表の1行。報酬月額が min 以上 max 未満なら amount */
export interface Grade {
  grade: number;
  /** 標準報酬月額 */
  amount: number;
  /** 報酬月額の下限（以上）。第1級は 0 */
  min: number;
  /** 報酬月額の上限（未満）。最高等級は null */
  max: number | null;
}

export interface GradePeriod {
  key: string;
  from: string;
  /** 最後の日。今の時期（または最後の時期）は null */
  until: string | null;
  /** 等級の数 */
  count: number;
  min_amount: number;
  max_amount: number;
  basis: string;
  grades: Grade[];
}

export interface Grades {
  name: string;
  currency: "JPY";
  /** 健康保険（2016年4月から第1〜50級） */
  health: { law: string; periods: GradePeriod[] };
  /** 厚生年金保険（2016年10月から。2020年9月から第1〜32級、2027年9月〜2029年9月に第33〜35級が加わる） */
  pension: { law: string; periods: GradePeriod[] };
  rules: {
    range: string;
    rates: string;
    health_bonus_cap: { amount: number; per: "fiscal_year"; rule: string; since: string; law: string };
    pension_bonus_cap: { amount: number; per: "month"; rule: string; law: string };
    future: string;
  };
  data_version: string;
}

export interface Meta {
  data_version: string;
  checked_at: string;
  files: ("birth" | "allowance" | "grades")[];
  periods: Record<"birth" | "health_grades" | "pension_grades", { 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;
  };
}
