/**
 * シルギア 健康保険・厚生年金の保険料率API v1 の型定義
 * https://api.shirugear.com/shaho/
 *
 * 協会けんぽ・日本年金機構・厚生労働省・こども家庭庁の公表値をもとにアナライズギア株式会社が作成。
 * 公式の API ではありません。
 *
 * 率の単位はすべて %（9.85 は 9.85%）。
 * 年度の境目は項目ごとに違います:
 *   健康保険・介護保険 … health.effective_from の月の分から（ふつう3月分＝4月納付分）
 *   子ども・子育て支援金・拠出金・厚生年金 … 4月始まりの年度
 *   雇用保険 … employment.effective_from 〜 effective_to（4月1日〜翌年3月31日）
 *
 * 互換の方針: v1 の中では項目を「足す」ことはあっても、名前や意味は変えません。
 * 知らない項目が増えていても無視してください。
 */

/** 都道府県コード2桁（JIS。"01" 北海道 〜 "47" 沖縄県） */
export type PrefCode = string;

/** "YYYY-MM"（その月の分の保険料から） */
export type YearMonth = string;

/** 全国一律の率と、使われはじめた月 */
export interface RateSince {
  /** 率（%） */
  rate: number;
  /** この率が使われはじめた月 "YYYY-MM" */
  effective_from: YearMonth;
}

export interface HealthPrefecture {
  pref_code: PrefCode;
  /** 都道府県名（"東京都" など） */
  pref: string;
  /** 協会けんぽの健康保険料率（%・介護保険を含まない） */
  rate: number;
  /** rate ＋ 介護保険料率（40〜64歳＝介護保険第2号被保険者） */
  rate_with_care: number;
  /** 前の年度との差（ポイント）。最初の年度（2012）は null */
  change: number | null;
}

export interface HealthHead {
  /** この年度の健康保険料率・介護保険料率を使いはじめる月（ふつう3月分。2015年度だけ4月分） */
  effective_from: YearMonth;
  /** false=47都道府県とも前の年度と同じ（据え置き） */
  revised: boolean;
  /** 協会けんぽの平均保険料率（公表値）。確かめられた年度だけ。ほかは null */
  average_rate: number | null;
  /** その年度の料率を確かめた協会けんぽのページ */
  source_url: string;
}

export interface Health extends HealthHead {
  /** 都道府県コードの順 */
  prefectures: HealthPrefecture[];
}

export type EmploymentType = "general" | "agri_sake" | "construction";

export interface EmploymentRate {
  /** general=一般の事業 / agri_sake=農林水産・清酒製造の事業 / construction=建設の事業 */
  type: EmploymentType;
  label: string;
  /** 労働者負担（%） */
  worker: number;
  /** 事業主負担（%）= employer_benefits + employer_two_projects */
  employer: number;
  /** 事業主負担のうち失業等給付・育児休業給付の分（%） */
  employer_benefits: number;
  /** 事業主負担のうち雇用保険二事業の分（%） */
  employer_two_projects: number;
  /** 雇用保険料率（%）= worker + employer */
  total: number;
  /** 「1,000分の◯」の値（5 は 5/1,000＝0.5%） */
  per_mille: { worker: number; employer: number; total: number };
}

export interface Employment {
  /** "YYYY-04-01" */
  effective_from: string;
  /** "YYYY-03-31"（翌年） */
  effective_to: string;
  types: EmploymentRate[];
}

/** 全国一律の率（年度の応答と all.json の years に共通） */
export interface YearRates {
  fiscal_year: number;
  /** 和暦の年度（"令和8年度"。2019年度は "令和元年度"） */
  fiscal_year_label: string;
  unit: "%";
  /** 介護保険料率（協会けんぽ・全国一律） */
  care: RateSince;
  /** 子ども・子育て支援金率（2026年4月分から）。始まる前の年度は null */
  child_support: RateSince | null;
  /** 厚生年金保険料率（2017年9月分から18.3%）。2017年度より前は null */
  pension: RateSince | null;
  /** 子ども・子育て拠出金率（事業主のみ負担）。2019年度より前は null */
  child_contribution: RateSince | null;
  /** 雇用保険料率。2023年度より前は null */
  employment: Employment | null;
}

/** GET /shaho/v1/{年度4桁}.json（2012〜最新の年度） */
export interface ShahoFiscalYearResponse extends YearRates {
  health: Health;
  data_version: string;
}

export interface ShahoPrefectureHistoryEntry {
  fiscal_year: number;
  fiscal_year_label: string;
  /** この年度の率を使いはじめる月 */
  effective_from: YearMonth;
  /** 健康保険料率（%・介護保険を含まない） */
  rate: number;
  /** その年度の介護保険料率（%） */
  care_rate: number;
  rate_with_care: number;
  change: number | null;
}

/** GET /shaho/v1/{都道府県コード2桁}.json（01〜47） */
export interface ShahoPrefectureResponse {
  pref_code: PrefCode;
  pref: string;
  unit: "%";
  insurer: "全国健康保険協会（協会けんぽ）";
  /** 新しい年度から順 */
  history: ShahoPrefectureHistoryEntry[];
  data_version: string;
}

/** GET /shaho/v1/all.json */
export interface ShahoAllResponse {
  from: number;
  to: number;
  unit: "%";
  /** 古い年度から順。年度の応答から health.prefectures を除いたもの */
  years: (YearRates & { health: HealthHead })[];
  prefectures: {
    pref_code: PrefCode;
    pref: string;
    /** 古い年度から順。項目は2つだけ */
    history: { fiscal_year: number; rate: number }[];
  }[];
  data_version: string;
}

/** GET /shaho/v1/meta.json */
export interface ShahoMetaResponse {
  data_version: string;
  /** データを作った日時（ISO 8601・UTC） */
  built_at: string;
  from: number;
  to: number;
  /** 作成者と、公式の API ではないことの表示 */
  notice: string;
  source: string;
  /** 出典ごとの書き方 */
  attribution: string[];
  /** 対象の範囲（協会けんぽの一般被保険者だけ） */
  scope: string;
  /** 項目ごとに入っている年度。average_rate だけは年度の一覧、ほかは [最初, 最後] */
  coverage: {
    health: [number, number];
    care: [number, number];
    average_rate: number[];
    child_support: [number, number];
    pension: [number, number];
    child_contribution: [number, number];
    employment: [number, number];
  };
  /** いちばん新しい年度を協会けんぽの保険料額表（Excel）と突き合わせた結果 */
  excel_checked: { fiscal_year: number; url: string; prefectures: number; mismatches: number };
  sources: {
    name: string;
    publisher: string;
    url: string;
    last_modified: string | null;
    /** 取得した日時（ISO 8601・UTC） */
    fetched_at: string;
  }[];
  counts: { prefectures: number; fiscal_years: number; records: number };
}

/** エラーのときの本文 */
export interface ErrorResponse {
  error: {
    status: 400 | 403 | 404 | 405;
    code: "invalid_request" | "not_found" | "region_not_allowed" | "method_not_allowed";
    message: string;
  };
}
