/**
 * シルギア 賃金の統計API v1 の型定義
 * https://api.shirugear.com/chingin/
 *
 * 厚生労働省「賃金構造基本統計調査」の一般労働者（短時間労働者を除く常用労働者）・民営事業所・企業規模10人以上・
 * 産業計の値を、男女別・企業規模別・年齢階級別に、全国と47都道府県で返します（全国は学歴別も）。
 * 金額の単位は千円、時間は月の時間数、人数は十人（どれも統計表のまま）。
 *
 * 互換の方針: v1 の中では項目を「足す」ことはあっても、名前や意味は変えません。
 * 知らない項目が増えていても無視してください。
 */

export type SexKey = "total" | "male" | "female";
export type SizeKey = "all" | "1000+" | "100-999" | "10-99";
export type AgeKey = "all" | "~19" | "20-24" | "25-29" | "30-34" | "35-39" | "40-44" | "45-49" | "50-54" | "55-59" | "60-64" | "65-69" | "70~";
export type EducationKey = "all" | "junior_high" | "high_school" | "vocational" | "junior_college" | "university" | "graduate" | "unknown";

/** 1つの区分の値。統計表で「-」の項目は null */
export interface Cell {
  /** 平均年齢（歳） */
  age: number | null;
  /** 平均勤続年数（年） */
  tenure: number | null;
  /** 所定内実労働時間数（6月・時間） */
  scheduled_hours: number | null;
  /** 超過実労働時間数（6月・時間） */
  overtime_hours: number | null;
  /** きまって支給する現金給与額（6月分・千円。残業代などを含む） */
  monthly_cash: number | null;
  /** 所定内給与額（6月分・千円。残業代などを除く） */
  monthly_scheduled: number | null;
  /** 年間賞与その他特別給与額（調査の前の年の1年間・千円） */
  annual_bonus: number | null;
  /** 労働者数（推計・十人） */
  workers: number | null;
  /** シルギアが計算した目安（統計の値ではない） */
  estimate: {
    /** 年収の目安（千円）= monthly_cash × 12 + annual_bonus */
    annual_income: number | null;
  };
}

/** 年齢階級 → 値。区分ごと「-」のときは null */
export type ByAge = Record<AgeKey, Cell | null>;
export type BySize = Record<SizeKey, ByAge>;
export type BySex = Record<SexKey, BySize>;

export interface Units {
  age: "歳"; tenure: "年"; scheduled_hours: "時間"; overtime_hours: "時間";
  monthly_cash: "千円"; monthly_scheduled: "千円"; annual_bonus: "千円"; workers: "十人";
  "estimate.annual_income": "千円";
}

/** /chingin/v1/{年}/{00〜47}.json */
export interface AreaFile {
  /** 調査の年（2025 なら令和7年調査＝2025年6月分の給与と2024年の賞与） */
  year: number;
  survey: string;
  scope: string;
  units: Units;
  /** 00 は全国 */
  area: { code: string; name: string };
  education: "all";
  data: BySex;
}

/** /chingin/v1/{年}/education.json（全国だけ） */
export interface EducationFile {
  year: number;
  survey: string;
  scope: string;
  units: Units;
  area: { code: "00"; name: "全国" };
  data: Record<EducationKey, BySex>;
}

/** /chingin/v1/summary.json: 年 → 地域コード → 性別 → 値（企業規模計・年齢計・学歴計） */
export interface Summary {
  scope: string;
  years: number[];
  units: Units;
  data: Record<string, Record<string, Record<SexKey, Cell>>>;
}

/** /chingin/v1/meta.json */
export interface Meta {
  data_version: string;
  checked_at: string;
  years: number[];
  latest_year: number;
  scope: string;
  areas: { code: string; name: string }[];
  fields: { key: string; name: string; unit: string }[];
  estimate: { key: string; name: string; unit: string; formula: string; note: string };
  sexes: { key: SexKey; name: string }[];
  sizes: { key: SizeKey; name: string }[];
  age_groups: { key: AgeKey; name: string }[];
  educations: { key: EducationKey; name: string }[];
  definitions: Record<string, string>;
  nulls: string;
  comparability: string;
  sources: { label: string; url: string }[];
  files_by_year: { year: number; survey: string; published: string; national: string; prefectures_list: string; fetched_at: string }[];
  license: string;
}

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