/**
 * シルギア 気温・降水量の平年値API v1 の型定義
 * https://api.shirugear.com/heinen/
 *
 * 気象庁ホームページ「平年値ダウンロード」の2020年平年値（統計期間1991〜2020年）を加工して作成（アナライズギア株式会社）。
 * 気象庁の公式のAPIではありません。平年値は過去30年の平均で、予想や予報ではありません。
 *
 * 互換の方針: v1 の中では項目を「足す」ことはあっても、名前や意味は変えません。
 * 知らない項目が増えていても無視してください。
 */

/** 地点番号（アメダスの観測所番号5桁。阿蘇山だけは気象台などの番号 "47821"） */
export type StationId = string;

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

/** 1つの項目の平年値 */
export interface Normal {
  /** 1月〜12月（[0] が1月）。その月の平年値が無いときは null */
  months: (number | null)[];
  /** 年の値（平均の項目は年平均、合計の項目は年合計、雪の項目は寒候年＝前年8月〜その年7月の値） */
  year: number | null;
  /** 日照時間が推計値（推計気象分布）のときだけ true */
  estimated?: true;
  /** 気象庁が［参考］としている値（観測をやめた・途中で途切れたなど）のときだけ true */
  reference?: true;
  /** もとになった年数（30年と違うときだけ） */
  years?: number;
  /** 統計の最初と最後の年（1991〜2020年と違うときだけ） */
  from?: number;
  to?: number;
}

/** 項目のキー（単位は meta.json の elements にもあります） */
export interface Normals {
  /** 平均気温（℃） */
  temp?: Normal;
  /** 日最高気温の平均（℃） */
  temp_max?: Normal;
  /** 日最低気温の平均（℃） */
  temp_min?: Normal;
  /** 降水量の合計（mm） */
  precip?: Normal;
  /** 日照時間の合計（時間）。estimated: true は推計値 */
  sunshine?: Normal;
  /** 平均風速（m/s） */
  wind?: Normal;
  /** 平均の相対湿度（%）。気象台などだけ */
  humidity?: Normal;
  /** 全天日射量（1日の合計の平均・MJ/m²）。気象台などの一部だけ */
  solar_radiation?: Normal;
  /** 降雪の深さの合計（cm）。気象台などだけ。年の値は寒候年 */
  snowfall?: Normal;
  /** 最深積雪（cm）。年の値は寒候年の最大 */
  snow_depth_max?: Normal;
  /** 真冬日（日最高気温0℃未満）の日数 */
  days_max_lt_0?: Normal;
  /** 冬日（日最低気温0℃未満）の日数 */
  days_min_lt_0?: Normal;
  /** 夏日（日最高気温25℃以上）の日数 */
  days_max_ge_25?: Normal;
  /** 真夏日（日最高気温30℃以上）の日数 */
  days_max_ge_30?: Normal;
  /** 猛暑日（日最高気温35℃以上）の日数 */
  days_max_ge_35?: Normal;
  /** 日最低気温25℃以上の日数 */
  days_min_ge_25?: Normal;
  /** 日降水量1mm以上の日数 */
  days_precip_ge_1?: Normal;
  /** 日降水量10mm以上の日数 */
  days_precip_ge_10?: Normal;
}

export type ElementKey = keyof Normals;

/** 地点の基本の情報 */
export interface StationBase {
  id: StationId;
  name: string;
  /** 読み（カタカナ） */
  name_kana: string;
  /** ローマ字（気象庁の地点情報の表記。"TOKYO" など） */
  name_romaji: string;
  pref_code: PrefCode;
  pref: string;
  /** 緯度・経度（10進の度） */
  lat: number;
  lon: number;
  /** 標高（m） */
  alt: number;
  /** 同じ場所の気象台・特別地域気象観測所の地点番号。無ければ null */
  surface_id: string | null;
  /** 同じ地点・同じ平年値のもう一方の番号（45148→"45147" など）。ふつうは null */
  duplicate_of: StationId | null;
}

interface Common {
  /** いつも "1991-2020" */
  period: string;
  /** "2020年平年値（第5版）" */
  data_version: string;
}

/** GET /heinen/v1/{地点番号}.json */
export interface StationResponse extends StationBase, Common {
  normals: Normals;
}

/** GET /heinen/v1/stations.json */
export interface StationsResponse extends Common {
  count: number;
  items: (StationBase & {
    /** 持っている項目のキー */
    elements: ElementKey[];
    /** 日照時間が推計値なら true */
    sunshine_estimated: boolean;
    /** どれかの項目に［参考］の値があれば true */
    reference: boolean;
  })[];
}

/** 最寄りの地点・代表の地点 */
export interface StationRef {
  id: StationId;
  name: string;
  name_kana: string;
  pref_code: PrefCode;
  alt: number;
  /** 役所からの距離（km・直線） */
  distance_km: number;
  normals: Normals;
}

/** GET /heinen/v1/pref/{01〜47}.json */
export interface PrefResponse extends Common {
  pref_code: PrefCode;
  pref: string;
  /** 都道府県庁のある市区町村（capital）の役所にいちばん近い、気温を観測している地点。normals はすべての項目 */
  representative: StationRef & { capital: { code: string; name: string } };
  count: number;
  stations: {
    id: StationId;
    name: string;
    name_kana: string;
    alt: number;
    lat: number;
    lon: number;
    duplicate_of: StationId | null;
    /** 年の値（temp・temp_max・temp_min・precip・sunshine・snow_depth_max のうち、その地点にあるもの） */
    year: Partial<Record<"temp" | "temp_max" | "temp_min" | "precip" | "sunshine" | "snow_depth_max", number | null>>;
  }[];
}

interface CityBase {
  /** 5桁の自治体コード */
  code: string;
  /** 検査数字つき6桁の自治体コード */
  code6: string;
  name: string;
  /** "city" / "ward" / "designated_city" / "special_ward" / "town" / "village" */
  kind: string;
  pref_code: PrefCode;
  pref: string;
}

/** GET /heinen/v1/city/{6桁}.json */
export interface CityResponse extends CityBase, Common {
  /** 気温を観測している地点のうち、役所にいちばん近い地点（normals は temp・temp_max・temp_min）。役所の位置が無ければ null */
  temperature: StationRef | null;
  /** 降水量を観測している地点のうち、役所にいちばん近い地点（normals は precip・days_precip_ge_1）。役所の位置が無ければ null */
  precipitation: StationRef | null;
  /** 最寄りの地点を選べなかった理由（temperature が null のときだけ） */
  note?: string;
}

/** GET /heinen/v1/cities.json */
export interface CitiesResponse extends Common {
  count: number;
  items: (CityBase & {
    temperature: { id: StationId; distance_km: number } | null;
    precipitation: { id: StationId; distance_km: number } | null;
  })[];
}

/** GET /heinen/v1/meta.json */
export interface MetaResponse {
  data_version: string;
  /** その版を気象庁が使いはじめた日 "YYYY-MM-DD" */
  valid_from: string;
  period: { from: number; to: number };
  source: string;
  sources: { name: string; url: string; fetched_at?: string; files?: string[] }[];
  elements: {
    key: ElementKey;
    label: string;
    unit: string;
    /** 気象庁の平年値のファイルの要素番号 */
    jma_element: string;
    /** 気象台などにしか無い項目なら true */
    only_surface: boolean;
    /** 年の値の意味（"平均" / "合計" / "最大（寒候年）"） */
    year: string;
  }[];
  notes: Record<string, string>;
  counts: Record<string, number>;
  excluded: ({ surface_id: string; reason: string } | { id: string; name: string; reason: string })[];
}

/** エラー（400 / 403 / 404 / 405） */
export interface ErrorResponse {
  error: { status: number; code: "invalid_request" | "region_not_allowed" | "not_found" | "method_not_allowed"; message: string };
}
