/**
 * シルギア 年金API v1 の型定義
 * https://api.shirugear.com/nenkin/
 *
 * 老齢基礎年金の満額（年度ごと）、国民年金保険料と付加保険料・前納、厚生年金の標準的な年金額（モデル年金）、
 * 繰上げ・繰下げ受給の率、在職老齢年金の支給停止調整額を返します。金額はすべて円の整数です。
 * 年度は4月1日から翌年3月31日までです。
 *
 * 互換の方針: v1 の中では項目を「足す」ことはあっても、名前や意味は変えません。
 * 知らない項目が増えていても無視してください。
 */

export interface FullAmount {
  /** 年額（780,900円 × 改定率 を100円単位にした額） */
  annual: number;
  /** 年額 ÷ 12 の1円未満を切り捨てた額（報道発表の月額と同じ） */
  monthly: number;
  /** 改定率（国民年金法による改定率の改定等に関する政令 第1条）。まだ政令が出ていない年度は null */
  kaitei_ritsu: number | null;
}

export interface BasicYear {
  /** 年度（2026 = 2026年4月〜2027年3月） */
  fiscal_year: number;
  from: string;
  until: string;
  /** 老齢基礎年金の満額（昭和31年4月2日以後生まれ。分かれていない年度は全員） */
  full: FullAmount;
  /** 昭和31年4月1日以前生まれの満額（2023年度から）。分かれていない年度は null */
  full_older: (FullAmount & { born_until: string }) | null;
}

export interface Kiso {
  name: string;
  currency: "JPY";
  /** 古い順（2019年度から） */
  fiscal_years: BasicYear[];
  /** 計算の決まり（文で説明） */
  rules: Record<string, string>;
  /** 老齢厚生年金（報酬比例部分）の乗率（1000分の5.481）と説明 */
  kosei: { hirei_rate_permille: number; hirei: string; law: string };
  data_version: string;
}

export interface PremiumPeriod {
  fiscal_year: number;
  from: string;
  until: string;
  /** 国民年金保険料（月額） */
  monthly: number;
  /** 国民年金法 第87条第3項の表の額（平成16年度水準） */
  statutory: number;
  /** 保険料改定率（照合した年度だけ。ほかは null） */
  kaitei_ritsu: number | null;
  /** 一部免除のときに納める額。half = 半額納付、quarter = 4分の1納付、three_quarter = 4分の3納付。公表前は null */
  partial: { half: number; quarter: number | null; three_quarter: number | null } | null;
}

export interface PrepayAmount { amount: number; discount: number }

export interface Hokenryo {
  name: string;
  currency: "JPY";
  /** 古い順（2005年度から、報道発表で公表された翌年度まで） */
  periods: PremiumPeriod[];
  /** 付加保険料（月額）と付加年金（1か月あたりの年額） */
  fuka: { premium_monthly: number; benefit_per_month: number; rule: string; law: string };
  /** 前納の額（4月から納める場合） */
  prepay: {
    fiscal_year: number;
    monthly: number;
    plans: { key: "2y" | "1y" | "6m" | "early"; label: string; months: number; account_transfer: PrepayAmount; cash_or_card: PrepayAmount | null }[];
    fuka: { key: "2y" | "1y" | "6m"; months: number; account_transfer: PrepayAmount; cash_or_card: PrepayAmount }[];
    note: string;
  };
  rules: Record<string, string>;
  data_version: string;
}

export interface Model {
  name: string;
  currency: "JPY";
  /** 夫婦2人分の老齢基礎年金を含む標準的な年金額（月額）。restated は後の年度の報道発表で前提を改めて示した額 */
  standard: { fiscal_year: number; monthly: number; avg_standard_remuneration: number; years: number; basis: string; source_fiscal_year: number; restated: boolean }[];
  standard_note: string;
  /** ライフコース別の年金額（概算・月額） */
  lifecourse: {
    key: string;
    label: string;
    /** 年度 → 月額 */
    monthly: Record<string, number>;
    avg_kosei_years: number;
    avg_income_monthly: number;
    breakdown_2026: { kiso: number; kosei: number; fuka_included: number };
  }[];
  lifecourse_note: string;
  data_version: string;
}

export interface Jukyu {
  name: string;
  /** 繰上げ。生年月日（born_from 〜 born_until。null は端なし）ごと */
  early: { born_from: string | null; born_until: string | null; max_months: number; rate_per_month: number; max_rate: number; by_age_pct: Record<string, string> }[];
  /** 繰下げ。生年月日ごとの上限の月数・年齢 */
  late: { born_from: string | null; born_until: string | null; max_months: number; max_age: number; note: string | null; rate_per_month: number; max_rate: number; by_age_pct: Record<string, string> }[];
  rules: Record<string, string>;
  data_version: string;
}

export interface Zaishoku {
  name: string;
  currency: "JPY";
  /** 年度ごとの支給停止調整額（limit・円）。2019年度から */
  periods: { fiscal_year: number; from: string; until: string; limit: number; under_65: string }[];
  rules: Record<string, string>;
  data_version: string;
}

export interface Meta {
  data_version: string;
  checked_at: string;
  files: ("kiso" | "hokenryo" | "model" | "jukyu" | "zaishoku")[];
  /** 各ファイルの最初と最後の年度 */
  fiscal_years: Record<string, [number, number]>;
  scope: string;
  next_update: 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;
  };
}
