/**
 * シルギア 学校コードAPI v1 の型定義
 * https://api.shirugear.com/school/
 *
 * 文部科学省「学校コード」の全件（廃止された学校も含む）を返します。
 * 出典: 文部科学省ホームページ「学校コード」（https://www.mext.go.jp/b_menu/toukei/mext_01087.html）を加工して作成（アナライズギア株式会社）。
 * 文部科学省の公式のサービスではありません。
 *
 * 学校コードは13桁: 学校種(2) + 都道府県番号(2) + 設置区分(1) + 学校番号(7) + 検査数字(1)。
 * 検査数字が公表の計算方法と合わないコードが実在する（約200件）ので、check_digit_valid は参考の情報です。
 *
 * 互換の方針: v1 の中では項目を「足す」ことはあっても、名前や意味は変えません。
 * 知らない項目が増えていても無視してください。
 */

/** 学校コード13桁（例 "B113110000016"。先頭は大文字の A〜H） */
export type SchoolCode = string;

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

/** 学校種 */
export type SchoolKind =
  | "A1" // 幼稚園（幼稚園型認定こども園を含む）
  | "A2" // 幼保連携型認定こども園
  | "B1" // 小学校
  | "C1" // 中学校
  | "C2" // 義務教育学校
  | "D1" // 高等学校
  | "D2" // 中等教育学校
  | "E1" // 特別支援学校
  | "F1" // 大学
  | "F2" // 短期大学
  | "G1" // 高等専門学校
  | "H1" // 専修学校
  | "H2"; // 各種学校

/** 設置区分 "1" 国立 / "2" 公立 / "3" 私立 */
export type Establishment = "1" | "2" | "3";

/** 本校 / 分校 / 廃校 */
export type Branch = "main" | "branch" | "closed";

/** 移行後・移行前の学校 */
export interface SchoolRef {
  code: SchoolCode;
  /** 学校名。文部科学省の一覧に無いコードのときは null */
  name: string | null;
  /** その学校の URL。一覧に無いコードのときは null */
  url: string | null;
}

/** GET /school/v1/{学校コード13桁}.json */
export interface SchoolResponse {
  code: SchoolCode;
  /** 検査数字を除いた12桁 */
  code12: string;
  /** 検査数字（13桁目） */
  check_digit: string;
  /** 公表の計算方法（「学校コードの取り扱いについて」別紙）で求めた検査数字と合えば true */
  check_digit_valid: boolean;
  /** 合わないときだけ、公表の計算方法で求めた検査数字。合うときは null */
  check_digit_expected: string | null;
  kind: SchoolKind;
  /** 学校種の名前（"小学校" など） */
  kind_label: string;
  /** 特別支援学校（E1）だけ、元の資料の区別（"養護" / "特支聾" / "特支盲"）。ほかは null */
  kind_detail: string | null;
  /** 学校コードの都道府県番号（大学等は本部の所在地。学校のある場所と違うことがある） */
  prefcode: PrefCode;
  prefecture: string;
  establishment: Establishment;
  /** "国立" / "公立" / "私立" */
  establishment_label: string;
  /** 学校番号7桁 */
  school_number: string;
  branch: Branch;
  /** "本校" / "分校" / "廃校" */
  branch_label: string;
  /** 学校名（元の資料のまま。英数字は全角） */
  name: string;
  /** 学校所在地（元の資料のまま。都道府県名から始まらないものもある） */
  address: string;
  /** 郵便番号7桁（ハイフンなし）。元の資料の値が7桁でないときは null */
  zipcode: string | null;
  /** 全国地方公共団体コード6桁（検査数字付き。/lgcode/v1/{6桁}.json で引ける）。付けられなかったときは null */
  lgcode: string | null;
  /** その自治体の名前（政令市は区まで。例 "札幌市中央区"） */
  city: string | null;
  /** 自治体コードを何から付けたか: 所在地の市区町村名 / 郵便番号 / 所在地の合併前などの名前 */
  lgcode_from: "address" | "zipcode" | "former_address" | null;
  /** 属性情報設定年月日 "YYYY-MM-DD" */
  valid_from: string | null;
  /** 属性情報廃止年月日 "YYYY-MM-DD"。今ある学校は null */
  abolished: string | null;
  /** 移行後の学校コード（統合などで後を継いだ学校。複数のこともある） */
  successors: SchoolRef[];
  /** このコードを「移行後の学校コード」に持つ学校（移行前） */
  predecessors: SchoolRef[];
  /** 旧学校調査番号 */
  former_survey_number: string | null;
  /** 元の資料の基準日 "YYYY-MM-DD"（"2026-05-01" なら令和8年5月1日時点） */
  data_version: string;
}

/** /school/v1/pref/{都道府県}/{学校種}.json の1件 */
export interface SchoolListItem {
  code: SchoolCode;
  name: string;
  establishment: Establishment;
  branch: Branch;
  address: string;
  zipcode: string | null;
  lgcode: string | null;
  abolished: string | null;
}

/** GET /school/v1/pref/{都道府県コード2桁}/{学校種}.json */
export interface SchoolListResponse {
  prefcode: PrefCode;
  prefecture: string;
  kind: SchoolKind;
  kind_label: string;
  /** 廃止された学校も含めた件数 */
  count: number;
  /** 今ある学校の件数 */
  active: number;
  /** 学校コードの順。廃止された学校も含む（abolished で見分ける） */
  items: SchoolListItem[];
  data_version: string;
}

/** GET /school/v1/pref/{都道府県コード2桁}.json */
export interface SchoolPrefectureResponse {
  prefcode: PrefCode;
  prefecture: string;
  kinds: {
    kind: SchoolKind;
    kind_label: string;
    count: number;
    active: number;
    /** 一覧の URL。1件も無い学校種は null */
    url: string | null;
  }[];
  names_url: string;
  data_version: string;
}

/** GET /school/v1/names/{都道府県コード2桁}.json（今ある学校だけ） */
export interface SchoolNamesResponse {
  prefcode: PrefCode;
  prefecture: string;
  count: number;
  fields: ["code", "name"];
  /** [学校コード, 学校名] の配列。学校コードの順 */
  items: [SchoolCode, string][];
  data_version: string;
}

/** GET /school/v1/meta.json */
export interface SchoolMetaResponse {
  data_version: string;
  /** "暫定版" / "確定版" */
  edition: string;
  /** "令和8年5月1日時点（暫定版）・2026-08-26 更新" など */
  edition_label: string;
  /** 文部科学省が CSV を更新した日（分からなければ null） */
  source_published: string | null;
  /** データを作った日時（ISO 8601・UTC） */
  built_at: string;
  /** 出典の書き方 */
  source: string;
  sources: { name: string; url: string; edition?: string; files?: { url: string; last_modified: string | null }[]; fetched_at: string | null }[];
  counts: {
    schools: number;
    active: number;
    abolished: number;
    by_kind: Record<SchoolKind, { label: string; count: number; active: number }>;
    /** 検査数字が公表の計算方法と合わないコードの数 */
    check_digit_mismatch: number;
    /** 自治体コードを何から付けたかの件数 */
    lgcode: { address: number; zipcode: number; former_address: number; none: number };
  };
  kinds: { kind: SchoolKind; label: string }[];
}

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