/**
 * シルギア 源泉徴収税額表API v1 の型定義
 * https://api.shirugear.com/gensen/
 *
 * 国税庁「源泉徴収税額表」の月額表・日額表・賞与に対する源泉徴収税額の算出率の表を
 * 年分ごとに返します（国税庁ホームページの Excel を加工して作成。国税庁が作成・保証するものではありません）。
 * 表そのものです。電子計算機等の特例（月額表の甲欄を式で出す方法）の値とは最大100円ほど違うことがあります。
 *
 * 年分は「給与・賞与を支払う日」の年で選びます（meta.json の years の payment_from〜payment_to）。
 * 金額はすべて円の整数。率は rate（％）と rate_x1000（％を1000倍した整数）の両方を入れています。
 * 率を掛ける計算は Math.floor(金額 * rate_x1000 / 100000)（1円未満は切り捨て）にすると、小数の誤差が出ません。
 *
 * 互換の方針: v1 の中では項目を「足す」ことはあっても、名前や意味は変えません。
 * 知らない項目が増えていても無視してください。
 */

/** 率（％）。rate_x1000 は rate を1000倍した整数（20.42％ → 20420） */
export interface Rate {
  rate: number;
  rate_x1000: number;
}

/** 年分の共通の項目 */
export interface GensenYearHead {
  /** 年分（西暦）。令和9年分なら 2027 */
  year: number;
  /** "令和8年分" など */
  year_label: string;
  /** この表を使う支払日の範囲 "YYYY-MM-DD"（その年の1月1日〜12月31日） */
  payment_from: string;
  payment_to: string;
  /** この表で源泉徴収する税（2026: "所得税及び復興特別所得税"、2027: "所得税、防衛特別所得税及び復興特別所得税"） */
  taxes: string;
}

/** 表の1行（「以上」from を含み、「未満」to を含まない） */
export interface WageRow {
  from: number;
  to: number;
  /** 甲欄。kou[0]〜kou[7] が扶養親族等の数0人〜7人の税額 */
  kou: number[];
  /** 乙欄の税額 */
  otsu: number;
  /** 丙欄の税額（日額表だけ） */
  hei?: number;
}

/** 最後の行を超える金額の式の1区間: from 以上・次の区間の from 未満は tax ＋ (金額 − from) × 率 */
export interface TopSegment<T> extends Rate {
  from: number;
  /** from ちょうどの税額（甲欄は扶養0〜7人の8つ） */
  tax: T;
}

/** GET /gensen/v1/{年}/monthly.json ・ /gensen/v1/{年}/daily.json */
export interface GensenWageTable extends GensenYearHead {
  table: "monthly" | "daily";
  table_label: "月額表" | "日額表";
  /** 根拠の財務省告示 */
  notice: string;
  unit: "円";
  /** 表を引く金額（"その月の社会保険料等控除後の給与等の金額" など） */
  amount_basis: string;
  columns: Record<string, string>;
  /** 表の最下段（「under 円未満」）。甲欄・丙欄は表の額、乙欄は金額 × otsu.rate */
  bottom: { under: number; kou: number[]; otsu: Rate; hei?: number };
  /** 金額の小さい順。最初の from は bottom.under、最後の to は top.from */
  rows: WageRow[];
  /** 最後の行を超える金額（top.from 以上）の式。区間は from の小さい順 */
  top: {
    from: number;
    kou: TopSegment<number[]>[];
    otsu: TopSegment<number>[];
    hei?: TopSegment<number>[];
  };
  dependents: {
    /** 甲欄の表にある扶養親族等の数の上限（7） */
    kou_max_in_table: 7;
    /** 7人を超える1人ごとに、7人の税額から引く額（月額表 1610・日額表 50） */
    kou_over7_deduction_per_person: number;
    /** 乙欄で「従たる給与についての扶養控除等申告書」の扶養親族等1人ごとに引く額（月額表 1610・日額表 50） */
    otsu_deduction_per_person: number;
  };
  /** 端数の扱いの説明 */
  rounding: string;
  /** データの版（国税庁の Excel を取り込んだ日 "YYYY-MM-DD"） */
  data_version: string;
  /** 出典の書き方 */
  source: string;
  note: string;
}

export interface BonusBand extends Rate {
  /** 前月の社会保険料等控除後の給与等の金額の「以上」（円） */
  from: number;
  /** 「未満」（円）。いちばん上の段は null（上限なし） */
  to: number | null;
}

/** GET /gensen/v1/{年}/bonus.json */
export interface GensenBonusTable extends GensenYearHead {
  table: "bonus";
  table_label: "賞与に対する源泉徴収税額の算出率の表";
  notice: string;
  amount_basis: string;
  /** 甲欄。dependents 0〜7（7 は「7人以上」）。bands は率の低い順の21段 */
  kou: { dependents: number; bands: BonusBand[] }[];
  /** 乙欄（5段） */
  otsu: BonusBand[];
  dependents: { kou_max_in_table: 7; kou_max_label: "7人以上" };
  /** この表を使わない場合（前月の給与が無い・賞与が前月の給与の multiplier 倍を超える など） */
  table_not_used: { multiplier: number; when: string; then: string };
  rounding: string;
  data_version: string;
  source: string;
  note: string;
}

/** GET /gensen/v1/meta.json */
export interface GensenMetaResponse {
  data_version: string;
  /** データを作った日時（ISO 8601・UTC） */
  built_at: string;
  years: (GensenYearHead & {
    notice: { monthly: string; daily: string; bonus: string };
    /** 国税庁のページ */
    page: string;
    /** 国税庁の Excel の更新日 "YYYY-MM-DD" */
    source_date: string | null;
    files: { monthly: string; daily: string; bonus: string };
    counts: { monthly_rows: number; daily_rows: number; bonus_bands: number };
  })[];
  /** 年分の選び方の説明 */
  how_to_choose: string;
  source: string;
  sources: {
    name: string;
    page: string;
    files: Record<"monthly" | "daily" | "bonus", { url: string; sha256: string; bytes: number; last_modified: string | null }>;
  }[];
  /** 国税庁の Excel を取得した日時（ISO 8601・UTC） */
  fetched_at: string;
  note: string;
}

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