/**
 * シルギア ナンバープレートの地域名API v1 の型定義
 * https://api.shirugear.com/plate/
 *
 * 国土交通省「ナンバープレートの地域名と市区町村の対応表」を加工して作成（アナライズギア株式会社）。
 * 国土交通省の公式の API ではありません。
 * 登録自動車と軽自動車の地域名は同じです。原付（125cc以下）の市区町村のナンバーは対象外です。
 *
 * 互換の方針: v1 の中では項目を「足す」ことはあっても、名前や意味は変えません。
 * 知らない項目が増えていても無視してください。
 */

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

/** 地域名の id（英小文字。"shinagawa"・"fujisan" など） */
export type PlateId = string;

/** 自治体コード API と同じ団体の種類 */
export type MunicipalityKind = "city" | "designated_city" | "special_ward" | "town" | "village" | "ward";

/**
 * その都道府県の中で、交付対象の市区町村がどう決まっているか
 * - "list": 市区町村を並べている
 * - "rest": 同じ都道府県のほかの地域名の残り全部（「〜の交付対象以外」「仙台市以外」など）
 * - "all": 都道府県内全域
 */
export type Coverage = "list" | "rest" | "all";

/** 地域名の短い形（市区町村の応答・都道府県の応答に入る） */
export interface PlateShort {
  id: PlateId;
  /** ナンバーに書かれる地域名（"品川"・"なにわ"・"尾張小牧" など） */
  name: string;
  /** 読み（ひらがな） */
  kana: string;
  /** ご当地ナンバーなら true */
  gotochi: boolean;
}

export interface PlatePrefecturePart {
  pref_code: PrefCode;
  prefecture: string;
  coverage: Coverage;
  /** coverage が "rest"・"all" のとき、対応表の書き方（"弘前及び八戸の交付対象以外"・"県内全域" など）。"list" は null */
  coverage_text: string | null;
  /** この都道府県の中の市区町村の数 */
  municipality_count: number;
}

/** 地域名 */
export interface PlateRegion extends PlateShort {
  /** 地域名がある都道府県。ふつうは1つ。富士山だけ山梨県・静岡県の2つ */
  prefectures: PlatePrefecturePart[];
  /** ご当地ナンバーの導入の弾（1〜4）。ご当地ナンバーでなければ null */
  gotochi_round: number | null;
  /** ご当地ナンバーの交付が始まった年月 "YYYY-MM"。ご当地ナンバーでなければ null */
  gotochi_from: string | null;
  design: {
    /** 地方版の図柄入りナンバー（地域名ごとの図柄）がある */
    local: boolean;
    /** 都道府県単位の図柄入りナンバーがある（秋田県・栃木県・群馬県・東京都・沖縄県） */
    prefecture: boolean;
  };
  /** 交付対象の市区町村の数（政令市は1つと数え、区は含まない） */
  municipality_count: number;
}

export interface Municipality {
  /** 自治体コード5桁 */
  code: string;
  /** 検査数字付き6桁 */
  code6: string;
  name: string;
  kind: MunicipalityKind;
  pref_code: PrefCode;
  prefecture: string;
}

/** GET /plate/v1/{5桁または6桁}.json — 市区町村 → 地域名（政令市の区も） */
export interface PlateMunicipalityResponse extends Municipality {
  /** 政令市の区なら市のコード。それ以外は null */
  parent_code: string | null;
  plate: PlateShort;
  /** 自治体コード API の同じ団体の URL */
  lgcode: string;
  data_version: string;
}

/** GET /plate/v1/{id}.json — 地域名1件と交付対象の市区町村 */
export interface PlateRegionResponse extends PlateRegion {
  /** 読みの出典（"自治体コード 11103（さいたま市大宮区）の読み" など） */
  kana_source: string;
  /** 市区町村コードの順 */
  municipalities: Municipality[];
  data_version: string;
}

/** GET /plate/v1/pref/{01〜47}.json — 都道府県の地域名 */
export interface PlatePrefResponse {
  pref_code: PrefCode;
  prefecture: string;
  regions: (PlateShort & {
    coverage: Coverage;
    coverage_text: string | null;
    municipalities: Municipality[];
  })[];
  data_version: string;
}

/** GET /plate/v1/all.json — 全138の地域名 */
export interface PlateAllResponse {
  count: number;
  regions: (PlateRegion & {
    /** 交付対象の市区町村の自治体コード5桁（政令市は市のコード） */
    municipalities: string[];
  })[];
  data_version: string;
}

/** GET /plate/v1/meta.json */
export interface PlateMetaResponse {
  /** データの版（国土交通省の対応表を取り込んだ日 "YYYY-MM-DD"） */
  data_version: string;
  /** 作った日時（ISO 8601・UTC） */
  built_at: string;
  source: string;
  sources: { name: string; url: string; page?: string; fetched_at?: string; sha256?: string; data_version?: string }[];
  license: string;
  /** 市区町村の名前とコードに使った自治体コード API の版 */
  lgcode_data_version: string;
  counts: {
    regions: number;
    gotochi: number;
    gotochi_by_round: Record<string, number>;
    design_local: number;
    design_prefecture: number;
    municipalities: number;
    wards: number;
  };
  gotochi_rounds: { round: number; from: string; count: number; note: string; source: string }[];
  /** 地域名の無い団体（北方領土の6村） */
  excluded: { code: string; name: string; district: string; reason: string }[];
  kana_note: string;
}

/** エラーの応答 */
export interface PlateError {
  error: { status: number; code: string; message: string };
}
