/**
 * シルギア 消費者物価指数API v1 の型定義
 * https://api.shirugear.com/cpi/
 *
 * 総務省統計局の消費者物価指数（全国・2025年基準）の、年平均と月ごとの指数（2025年=100）と
 * 前年比・前年同月比を返します。2024年12月以前は旧基準の指数を 2025年=100 に換算した接続指数です。
 * 前年比・前年同月比は統計局の公表値で、指数から計算し直した値とは一致しないことがあります。
 * 出典: 総務省統計局「消費者物価指数」2025年基準 長期時系列データ（e-Stat）を加工して作成。
 *
 * 互換の方針: v1 の中では項目を「足す」ことはあっても、名前や意味は変えません。
 * 知らない項目（系列が増えたときの新しいキーなど）は無視してください。
 */

/** 系列のキー */
export type SeriesKey =
  /** 総合（1970年1月〜） */
  | "all"
  /** 生鮮食品を除く総合（いわゆるコアCPI。1970年1月〜） */
  | "less_fresh_food"
  /** 生鮮食品及びエネルギーを除く総合（1970年1月〜） */
  | "less_fresh_food_energy"
  /** 食料（酒類を除く）及びエネルギーを除く総合（1970年1月〜） */
  | "less_food_energy"
  /** 持家の帰属家賃を除く総合（1946年8月〜。年平均は1947年〜） */
  | "less_imputed_rent";

/** 系列ごとの値。その期間に値が無い系列は null */
export type BySeries = Record<SeriesKey, number | null>;

export interface Series {
  key: SeriesKey;
  /** 統計局の類・品目符号（"0001" など） */
  code: string;
  /** 名前（"総合" など） */
  name: string;
  name_en: string;
  /** 月の値がある最初の月 "YYYY-MM" */
  monthly_from: string;
  /** 年平均がある最初の年 */
  annual_from: number;
}

export interface Values {
  /** 指数（2025年=100。小数第1位まで） */
  index: BySeries;
  /** 前年比（年）・前年同月比（月）。%・小数第1位。統計局の公表値。公表値が無い期間は null */
  yoy: BySeries;
}

export interface AnnualRow extends Values {
  year: number;
}

export interface MonthlyRow extends Values {
  /** "YYYY-MM" */
  month: string;
}

interface Common {
  /** 基準年（2025） */
  base_year: number;
  /** "2025年=100" */
  unit: string;
  /** "全国" */
  area: string;
  /** データの版（統計局のデータを取り込んだ日・日本時間 "YYYY-MM-DD"） */
  data_version: string;
}

/** GET /cpi/v1/annual.json */
export interface AnnualResponse extends Common {
  from: number;
  to: number;
  series: Series[];
  /** 古い年から順 */
  years: AnnualRow[];
}

/** GET /cpi/v1/monthly.json */
export interface MonthlyResponse extends Common {
  /** "YYYY-MM" */
  from: string;
  to: string;
  series: Series[];
  /** 古い月から順 */
  months: MonthlyRow[];
}

/** GET /cpi/v1/{年}.json（1946〜最新の月の年） */
export interface YearResponse extends Common {
  year: number;
  /** その年の年平均。まだ公表されていない年（今年）や1946年は null */
  annual: Values | null;
  /** その年の月（古い順）。今年は公表済みの月まで */
  months: MonthlyRow[];
  /** 12か月と年平均がそろっているか */
  complete: boolean;
}

/** GET /cpi/v1/latest.json */
export interface LatestResponse extends Common {
  /** いちばん新しい月 */
  month: MonthlyRow;
  /** いちばん新しい年平均 */
  year: AnnualRow;
  series: Series[];
}

/** GET /cpi/v1/meta.json */
export interface MetaResponse extends Common {
  latest_month: string;
  latest_year: number;
  series: Series[];
  /** 年ごとのファイルがある範囲 [最初, 最後] */
  years: [number, number];
  files: string[];
  /** 指数と前年比の性格についての注意 */
  notes: string[];
  source: string;
  sources: {
    label: string;
    url: string;
    license: string;
    /** 取り込んだ日時（UTC） */
    fetched_at: string;
    files: { title: string; url: string }[];
  }[];
  counts: { series: number; years: number; months: number; year_files: number };
}

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