/**
 * シルギア 駅API v1 の型定義
 * https://api.shirugear.com/station/
 *
 * 「国土数値情報（鉄道データ）」（国土交通省）（https://nlftp.mlit.go.jp/ksj/gml/datalist/KsjTmplt-N02-2025.html）を加工して作成（アナライズギア株式会社）。
 * 都道府県・市区町村は「国土数値情報（行政区域データ）」（国土交通省）を加工して決めています。
 * 読みは Wikidata（CC0）のかな表記を、駅名と位置で突き合わせて付けたものです（付けられない駅は null）。
 * 国土交通省や鉄道会社の公式の API ではありません。
 *
 * 互換の方針: v1 の中では項目を「足す」ことはあっても、名前や意味は変えません。
 * 知らない項目が増えていても無視してください。
 */

/** 駅のコード（国土数値情報の駅のグループコード・6桁。"003922" 渋谷） */
export type StationCode = string;

/** 路線 ID（英小文字 a〜f と数字の8桁。"12e493d1" 山手線） */
export type LineId = string;

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

/** 自治体コード（検査数字付きの6桁。自治体コードAPI と同じ。政令市は区のコード） */
export type LgCode = string;

/** 事業者の種類: "1" 新幹線・"2" JR在来線・"3" 公営鉄道・"4" 民営鉄道・"5" 第三セクター */
export type OperatorKind = "1" | "2" | "3" | "4" | "5";

/**
 * 鉄道の種類: "11" 普通鉄道JR・"12" 普通鉄道・"13" 鋼索鉄道・"14" 懸垂式鉄道・"15" 跨座式鉄道・"16" 案内軌条式鉄道・
 * "17" 無軌条鉄道・"21" 軌道・"22" 懸垂式モノレール・"23" 跨座式モノレール・"24" 案内軌条式・"25" 浮上式
 */
export type RailwayKind = "11" | "12" | "13" | "14" | "15" | "16" | "17" | "21" | "22" | "23" | "24" | "25";

/** 駅の短い形（路線の駅の並び・names.json に入る） */
export interface StationBrief {
  code: StationCode;
  /** 駅名（「駅」は付かない） */
  name: string;
  /** 読み（ひらがな）。付けられなかった駅は null */
  reading: string | null;
  pref_code: PrefCode;
  lg_code: LgCode;
}

/** 駅で乗り換えられる路線（国土数値情報の路線＝正式な路線名の区間） */
export interface StationLine {
  line_id: LineId;
  /** 事業者の正式な名前（"東日本旅客鉄道"） */
  operator: string;
  /** 事業者のふだんの呼び名（"JR東日本"）。呼び名が無ければ operator と同じ */
  operator_label: string;
  /** 正式な路線名（"3号線銀座線"） */
  line_name: string;
  /** 短い路線名（"銀座線"） */
  name: string;
  /** 表示用（"東京メトロ 銀座線"） */
  label: string;
  /** この路線のホームの駅コード（ふつうは1つ） */
  station_codes: string[];
  operator_kind: OperatorKind;
  operator_kind_name: string;
  railway_kind: RailwayKind;
  railway_kind_name: string;
  /** 1日あたりの乗降客数（国土数値情報 駅別乗降客数データ）。データに無い駅は null */
  ridership: Ridership | null;
}

export type RidershipStatus = "ok" | "other" | "none" | "private" | "nostation";

/** 路線ごとの乗降客数。数え方は鉄道会社ごとに違うので、会社をまたいで足さない */
export interface Ridership {
  /** 最新の年度（2024） */
  year: number;
  status: RidershipStatus;
  /** 人/日。status が "ok" のときだけ */
  passengers: number | null;
  /** true なら乗車人員（乗った人だけ） */
  boarding_only: boolean;
  /** 元のデータの備考（"山手線を含む" など） */
  note: string | null;
  /** status が "other" のとき、まとめて数えている路線の駅コード（分かるときだけ） */
  included_in: string | null;
  history: { year: number; status: RidershipStatus; passengers: number | null; boarding_only: boolean }[];
}

/** 運転系統（山手線・京浜東北線など9つ）の短い形 */
export interface ServiceRef {
  line_id: LineId;
  name: string;
  label: string;
}

/** /station/v1/{code}.json */
export interface Station {
  code: StationCode;
  name: string;
  reading: string | null;
  /** 読みの出どころ。今は "wikidata" だけ。読みが無ければ null */
  reading_source: "wikidata" | null;
  /** 突き合わせた Wikidata の項目の ID（"Q428811"）。読みが無ければ空の配列 */
  wikidata: string[];
  pref_code: PrefCode;
  prefecture: string;
  lg_code: LgCode;
  /** 市区町村名（政令市は区まで。"札幌市北区"） */
  city: string;
  /** 緯度（世界測地系・小数6桁） */
  lat: number;
  /** 経度（世界測地系・小数6桁） */
  lng: number;
  lines: StationLine[];
  /** この駅に止まる運転系統。無ければ空の配列 */
  services: ServiceRef[];
  /** カイシャログの「この駅の近くの会社」のページ */
  kaishalog: string;
  /** 国土数値情報（鉄道データ）のデータの基準日 "YYYY-MM-DD" */
  data_version: string;
}

/** /station/v1/line/{line_id}.json */
export interface Line {
  line_id: LineId;
  operator: string;
  operator_label: string;
  /** 正式な路線名。運転系統として足した路線は "京浜東北線" など */
  line_name: string;
  name: string;
  label: string;
  /** 国土数値情報にある路線なら true。運転系統として足した路線は false */
  official: boolean;
  /** 駅の並びの決め方。"approximate" 位置から決めたおおよその順・"service" 運転系統の駅名の並び */
  order: "approximate" | "service";
  station_count: number;
  stations: StationBrief[];
  /** 山手線・横須賀線だけ: 正式な路線の区間の駅 */
  official_stations?: StationBrief[];
  /** official が true のときだけ */
  operator_kind?: string;
  /** official が true のときだけ */
  railway_kinds?: RailwayKind[];
  prefs: PrefCode[];
  /** カイシャログの「沿線の会社」のページ */
  kaishalog: string;
  data_version: string;
}

/** /station/v1/pref/{pref_code}.json */
export interface PrefStations {
  pref_code: PrefCode;
  prefecture: string;
  count: number;
  stations: {
    code: StationCode;
    name: string;
    reading: string | null;
    lg_code: LgCode;
    city: string;
    lat: number;
    lng: number;
    /** この駅で乗り換えられる路線（国土数値情報の路線）の ID */
    line_ids: LineId[];
  }[];
  lines: {
    line_id: LineId;
    label: string;
    official: boolean;
    /** この都道府県の中の駅の数 */
    station_count_in_pref: number;
  }[];
  data_version: string;
}

/** /station/v1/names.json（駅名の順） */
export interface StationNames {
  count: number;
  items: StationBrief[];
  data_version: string;
}

/** /station/v1/near.json?lat=&lng=&limit=&within=（近い順） */
export interface NearStations {
  lat: number;
  lng: number;
  limit: number;
  within_km: number | null;
  count: number;
  stations: {
    code: StationCode;
    name: string;
    reading: string | null;
    /** 直線距離（m・地球を球とした大円距離。道のりではない） */
    distance_m: number;
    pref_code: PrefCode;
    prefecture: string;
    lg_code: LgCode;
    city: string;
    lat: number;
    lng: number;
    lines: { line_id: LineId; label: string }[];
    url: string;
    kaishalog: string;
  }[];
  distance: string;
  data_version: string;
}

/** /station/v1/points.json（駅のコードの順） */
export interface StationPoints {
  count: number;
  fields: ["code", "name", "lat", "lng"];
  items: [StationCode, string, number, number][];
  data_version: string;
}

/** /station/v1/lines.json */
export interface LineList {
  count: number;
  items: {
    line_id: LineId;
    operator: string;
    operator_label: string;
    line_name: string;
    name: string;
    label: string;
    official: boolean;
    order: "approximate" | "service";
    station_count: number;
    prefs: PrefCode[];
  }[];
  data_version: string;
}

/** /station/v1/meta.json */
export interface StationMeta {
  data_version: string;
  n02_version: string;
  n03_version: string;
  n03_as_of: string;
  lgcode_data_version: string;
  wikidata_fetched_at: string;
  built_at: string;
  source: string;
  sources: {
    name: string;
    credit?: string;
    url: string;
    page?: string;
    version?: string;
    as_of?: string;
    fetched_at?: string;
    sha256?: string;
    license?: string;
    used_for?: string;
    data_version?: string;
  }[];
  notice: string;
  kaishalog: { site: string; station_url: string; line_url: string; note: string };
  counts: {
    stations: number;
    station_entries: number;
    lines: number;
    lines_official: number;
    lines_service: number;
    operators: number;
    with_reading: number;
    reading_ratio: number;
    reading_conflicts: number;
    same_name_groups: number;
    place_nearest: number;
    by_pref: Record<PrefCode, number>;
  };
}

/** エラーの応答 */
export interface ApiError {
  error: {
    status: 400 | 403 | 404 | 405;
    code: "invalid_request" | "region_not_allowed" | "not_found" | "method_not_allowed";
    message: string;
  };
}
