{
  "openapi": "3.0.3",
  "info": {
    "title": "Vietnam Administrative Data API",
    "description": "API cung cấp dữ liệu hành chính Việt Nam 2025: Bộ/ban/ngành, Tỉnh/thành phố, Xã/phường, Trường ĐH & CĐ.\n\n**Tính năng nổi bật:**\n- Tìm kiếm không phân biệt dấu tiếng Việt (\"ha noi\" khớp \"Hà Nội\")\n- Phân trang linh hoạt với `page` & `limit`\n- CORS mở cho mọi origin\n- Cache 1 giờ tại Cloudflare edge",
    "version": "1.0.0",
    "contact": {
      "name": "Vietnam Data API"
    },
    "license": {
      "name": "MIT"
    }
  },
  "servers": [
    {
      "url": "/api/v1",
      "description": "Production (Cloudflare Pages)"
    }
  ],
  "tags": [
    { "name": "overview",   "description": "Tổng quan & thống kê" },
    { "name": "ministries", "description": "Bộ / ban / ngành trung ương" },
    { "name": "provinces",  "description": "Tỉnh / thành phố & xã / phường" },
    { "name": "schools",    "description": "Trường Đại học & Cao đẳng" },
    { "name": "search",     "description": "Tìm kiếm toàn văn" },
    { "name": "data",       "description": "Tải xuống dữ liệu thô" }
  ],
  "paths": {
    "/total": {
      "get": {
        "operationId": "getTotal",
        "tags": ["overview"],
        "summary": "Thống kê tổng quan",
        "description": "Trả về số lượng bộ/ngành, tỉnh/thành, xã/phường và trường ĐH/CĐ.",
        "responses": {
          "200": {
            "description": "Thành công",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "meta":          { "type": "object" },
                    "ministries":    { "type": "integer", "example": 17 },
                    "provinces":     { "type": "integer", "example": 34 },
                    "wards":         { "type": "integer", "example": 3321 },
                    "schools":       { "type": "integer", "example": 245 },
                    "total_records": { "type": "integer", "example": 3617 }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/ministries": {
      "get": {
        "operationId": "getMinistries",
        "tags": ["ministries"],
        "summary": "Danh sách bộ / ban / ngành",
        "description": "Trả về danh sách 17 bộ/ban/ngành trung ương (có phân trang).",
        "parameters": [
          { "$ref": "#/components/parameters/page" },
          { "$ref": "#/components/parameters/limit" }
        ],
        "responses": {
          "200": {
            "description": "Thành công",
            "content": {
              "application/json": {
                "schema": {
                  "allOf": [
                    { "$ref": "#/components/schemas/PaginatedResponse" },
                    {
                      "properties": {
                        "count": { "type": "integer", "example": 17 },
                        "data":  { "type": "array", "items": { "$ref": "#/components/schemas/Ministry" } }
                      }
                    }
                  ]
                }
              }
            }
          },
          "400": { "$ref": "#/components/responses/BadRequest" }
        }
      }
    },
    "/province/wards/": {
      "get": {
        "operationId": "getProvinces",
        "tags": ["provinces"],
        "summary": "Danh sách tỉnh / thành phố",
        "description": "Trả về danh sách 34 tỉnh/thành phố kèm số xã/phường (có phân trang).",
        "parameters": [
          { "$ref": "#/components/parameters/page" },
          { "$ref": "#/components/parameters/limit" }
        ],
        "responses": {
          "200": {
            "description": "Thành công",
            "content": {
              "application/json": {
                "schema": {
                  "allOf": [
                    { "$ref": "#/components/schemas/PaginatedResponse" },
                    {
                      "properties": {
                        "data": { "type": "array", "items": { "$ref": "#/components/schemas/ProvinceSummary" } }
                      }
                    }
                  ]
                }
              }
            }
          },
          "400": { "$ref": "#/components/responses/BadRequest" }
        }
      }
    },
    "/province/wards/{id}": {
      "get": {
        "operationId": "getProvinceWards",
        "tags": ["provinces"],
        "summary": "Xã / phường của một tỉnh",
        "description": "Trả về danh sách xã/phường của tỉnh/thành phố theo ID.\n\n`{id}` có thể là:\n- Mã tỉnh: `01`, `12`\n- Mã không có số 0 đầu: `1`\n- Tên tiếng Việt: `TP. Hà Nội`\n- Tên không dấu: `ha noi`, `tp ha noi`",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Mã tỉnh hoặc tên (tiếng Việt / không dấu)",
            "schema": { "type": "string" },
            "example": "12"
          },
          { "$ref": "#/components/parameters/page" },
          { "$ref": "#/components/parameters/limit" }
        ],
        "responses": {
          "200": {
            "description": "Thành công",
            "content": {
              "application/json": {
                "schema": {
                  "allOf": [
                    { "$ref": "#/components/schemas/PaginatedResponse" },
                    {
                      "properties": {
                        "id":          { "type": "string",  "example": "12" },
                        "name_vn":     { "type": "string",  "example": "TP. Hà Nội" },
                        "name_en":     { "type": "string",  "example": "Ha Noi City" },
                        "wards_count": { "type": "integer", "example": 126 },
                        "data":        { "type": "array",   "items": { "$ref": "#/components/schemas/Ward" } }
                      }
                    }
                  ]
                }
              }
            }
          },
          "400": { "$ref": "#/components/responses/BadRequest" },
          "404": { "$ref": "#/components/responses/NotFound" }
        }
      }
    },
    "/search": {
      "get": {
        "operationId": "search",
        "tags": ["search"],
        "summary": "Tìm kiếm toàn văn",
        "description": "Tìm kiếm trên cả 4 bộ dữ liệu (bộ/ngành, tỉnh, xã/phường, trường).\n\n**Đặc điểm:** Không phân biệt hoa/thường và dấu tiếng Việt — `ha noi` khớp `Hà Nội`.\n\n**Độ ưu tiên:** Khớp chính xác > Khớp đầu chuỗi > Khớp giữa chuỗi.",
        "parameters": [
          {
            "name": "q",
            "in": "query",
            "required": true,
            "description": "Từ khoá tìm kiếm (hỗ trợ không dấu)",
            "schema": { "type": "string" },
            "example": "ha noi"
          },
          {
            "name": "type",
            "in": "query",
            "required": false,
            "description": "Lọc theo loại kết quả",
            "schema": {
              "type": "string",
              "enum": ["ministry", "ministries", "province", "provinces", "ward", "wards", "school", "schools"]
            }
          },
          { "$ref": "#/components/parameters/page" },
          {
            "name": "limit",
            "in": "query",
            "required": false,
            "description": "Số kết quả mỗi trang (mặc định: 200, tối đa: 500)",
            "schema": { "type": "integer", "default": 200, "minimum": 1, "maximum": 500 }
          }
        ],
        "responses": {
          "200": {
            "description": "Thành công",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "query":          { "type": "string",  "example": "ha noi" },
                    "type":           { "type": "string",  "example": "all" },
                    "total_found":    { "type": "integer", "example": 24 },
                    "count":          { "type": "integer", "example": 20 },
                    "counts_by_type": {
                      "type": "object",
                      "properties": {
                        "ministry": { "type": "integer" },
                        "province": { "type": "integer" },
                        "ward":     { "type": "integer" },
                        "school":   { "type": "integer" }
                      }
                    },
                    "pagination": { "$ref": "#/components/schemas/Pagination" },
                    "results":    { "type": "array", "items": { "$ref": "#/components/schemas/SearchResult" } }
                  }
                }
              }
            }
          },
          "400": { "$ref": "#/components/responses/BadRequest" }
        }
      }
    },
    "/all": {
      "get": {
        "operationId": "getAll",
        "tags": ["data"],
        "summary": "Toàn bộ dữ liệu qua API",
        "description": "Trả về toàn bộ dataset dưới dạng JSON qua Cloudflare Function (~500KB). Dùng khi cần cache phía client.",
        "responses": {
          "200": {
            "description": "Thành công — toàn bộ dataset",
            "content": {
              "application/json": { "schema": { "$ref": "#/components/schemas/FullDataset" } }
            }
          }
        }
      }
    },
    "/data": {
      "get": {
        "operationId": "getDataAlias",
        "tags": ["data"],
        "summary": "Tải data.json (alias của /data.json)",
        "description": "Alias rewrite nội bộ (HTTP 200, URL giữ nguyên) tới file tĩnh `data.json` — phục vụ qua CDN, không tốn Function invocation. Chỉ rewrite một chiều alias → file tĩnh nên không tạo vòng lặp chuyển hướng.",
        "responses": {
          "200": {
            "description": "Thành công — nội dung của data.json",
            "content": {
              "application/json": { "schema": { "$ref": "#/components/schemas/FullDataset" } }
            }
          }
        }
      }
    },
    "/data.json": {
      "get": {
        "operationId": "getDataJson",
        "tags": ["data"],
        "summary": "Tải xuống data.json (file tĩnh)",
        "description": "File tĩnh JSON — phục vụ trực tiếp qua Cloudflare CDN (~500KB). Phù hợp cho tải về và cache.\n\nAlias: `/data` (rewrite 200). Cũng truy cập được qua `/api/v1/all` nếu muốn qua Function.",
        "responses": {
          "200": {
            "description": "Thành công",
            "content": {
              "application/json": { "schema": { "$ref": "#/components/schemas/FullDataset" } }
            }
          }
        }
      }
    },
    "/data.jsonl": {
      "get": {
        "operationId": "getDataJsonl",
        "tags": ["data"],
        "summary": "Tải xuống data.jsonl (file tĩnh, NDJSON)",
        "description": "File tĩnh NDJSON — mỗi dòng là một JSON object (~559KB).\n\nPhù hợp cho:\n- Stream processing\n- Import vào BigQuery, Elasticsearch, DuckDB\n- Công cụ dòng lệnh: `jq`, `grep`",
        "responses": {
          "200": {
            "description": "Thành công",
            "content": {
              "application/x-ndjson": {
                "schema": { "type": "string", "description": "Newline-delimited JSON lines" }
              }
            }
          }
        }
      }
    },
    "/openapi": {
      "get": {
        "operationId": "getOpenApiSpec",
        "tags": ["overview"],
        "summary": "OpenAPI 3.0 spec (alias của /openapi.json)",
        "description": "Alias rewrite nội bộ (HTTP 200) tới file tĩnh `openapi.json` — chính là spec mà Swagger UI tại `/docs` đang dùng.",
        "responses": {
          "200": {
            "description": "Thành công — OpenAPI 3.0 spec",
            "content": {
              "application/json": { "schema": { "type": "object" } }
            }
          }
        }
      }
    }
  },
  "components": {
    "parameters": {
      "page": {
        "name": "page",
        "in": "query",
        "required": false,
        "description": "Số trang (bắt đầu từ 1)",
        "schema": { "type": "integer", "default": 1, "minimum": 1 }
      },
      "limit": {
        "name": "limit",
        "in": "query",
        "required": false,
        "description": "Số bản ghi mỗi trang (mặc định: 100, tối đa: 500)",
        "schema": { "type": "integer", "default": 100, "minimum": 1, "maximum": 500 }
      }
    },
    "schemas": {
      "Pagination": {
        "type": "object",
        "properties": {
          "page":        { "type": "integer", "example": 2 },
          "limit":       { "type": "integer", "example": 20 },
          "total":       { "type": "integer", "example": 126 },
          "total_pages": { "type": "integer", "example": 7 },
          "has_prev":    { "type": "boolean", "example": true },
          "has_next":    { "type": "boolean", "example": true }
        }
      },
      "PaginatedResponse": {
        "type": "object",
        "properties": {
          "pagination": { "$ref": "#/components/schemas/Pagination" }
        }
      },
      "Ministry": {
        "type": "object",
        "properties": {
          "id":      { "type": "string", "example": "MOD" },
          "name_vn": { "type": "string", "example": "Bộ Quốc phòng" },
          "name_en": { "type": "string", "example": "Ministry of National Defence" }
        }
      },
      "ProvinceSummary": {
        "type": "object",
        "properties": {
          "id":          { "type": "string",  "example": "12" },
          "name_vn":     { "type": "string",  "example": "TP. Hà Nội" },
          "name_en":     { "type": "string",  "example": "Ha Noi City" },
          "wards_count": { "type": "integer", "example": 126 }
        }
      },
      "Ward": {
        "type": "object",
        "properties": {
          "id":      { "type": "string", "example": "00001" },
          "name_vn": { "type": "string", "example": "Phường Phúc Xá" },
          "name_en": { "type": "string", "example": "Phuc Xa Ward" }
        }
      },
      "SearchResult": {
        "type": "object",
        "properties": {
          "type":          { "type": "string", "enum": ["ministry", "province", "ward", "school"] },
          "id":            { "type": "string" },
          "name_vn":       { "type": "string" },
          "name_en":       { "type": "string" },
          "province_id":   { "type": "string",  "description": "Chỉ có với type=ward" },
          "province_name": { "type": "string",  "description": "Chỉ có với type=ward" },
          "abbr":          { "type": "string",  "description": "Chỉ có với type=school" },
          "category":      { "type": "string",  "description": "Chỉ có với type=school" },
          "location":      { "type": "string",  "description": "Chỉ có với type=school" }
        }
      },
      "FullDataset": {
        "type": "object",
        "properties": {
          "meta": {
            "type": "object",
            "properties": {
              "counts": {
                "type": "object",
                "properties": {
                  "ministries":  { "type": "integer" },
                  "provinces":   { "type": "integer" },
                  "wards_total": { "type": "integer" },
                  "schools":     { "type": "integer" }
                }
              }
            }
          },
          "ministries": { "type": "array", "items": { "$ref": "#/components/schemas/Ministry" } },
          "provinces": {
            "type": "array",
            "items": {
              "allOf": [
                { "$ref": "#/components/schemas/ProvinceSummary" },
                {
                  "properties": {
                    "wards": { "type": "array", "items": { "$ref": "#/components/schemas/Ward" } }
                  }
                }
              ]
            }
          },
          "schools": { "type": "array", "items": { "type": "object" } }
        }
      },
      "Error": {
        "type": "object",
        "properties": {
          "error": { "type": "string", "example": "Thiếu tham số q." }
        }
      }
    },
    "responses": {
      "BadRequest": {
        "description": "Tham số không hợp lệ",
        "content": {
          "application/json": { "schema": { "$ref": "#/components/schemas/Error" } }
        }
      },
      "NotFound": {
        "description": "Không tìm thấy tỉnh/thành phố",
        "content": {
          "application/json": { "schema": { "$ref": "#/components/schemas/Error" } }
        }
      }
    }
  }
}
