{
  "openapi": "3.1.0",
  "info": {
    "title": "Neebo Partner API",
    "version": "1.0.0",
    "description": "API mở cho đối tác và thiết bị. Xem docs/PARTNER_API.md."
  },
  "servers": [
    {
      "url": "https://api.neebo.vn/partner/v1"
    },
    {
      "url": "http://localhost:8787/partner/v1"
    }
  ],
  "security": [
    {
      "bearer": []
    }
  ],
  "components": {
    "securitySchemes": {
      "bearer": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "nb_live_…",
        "description": "API key của khách hàng (Console / web BQL)"
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "object",
            "properties": {
              "code": {
                "type": "string"
              },
              "message": {
                "type": "string"
              },
              "scope": {
                "type": "string"
              }
            },
            "required": [
              "code",
              "message"
            ]
          }
        }
      },
      "Building": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "projectId": {
            "type": [
              "string",
              "null"
            ]
          },
          "projectName": {
            "type": "string"
          },
          "address": {
            "type": "string"
          },
          "units": {
            "type": "integer"
          }
        }
      },
      "Unit": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "buildingId": {
            "type": "string"
          },
          "code": {
            "type": "string"
          },
          "floor": {
            "type": "integer"
          },
          "area": {
            "type": "number"
          },
          "handoverDate": {
            "type": [
              "string",
              "null"
            ],
            "format": "date"
          }
        }
      },
      "Resident": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "phone": {
            "type": "string",
            "description": "Che giữa, vd. 0912 *** 678"
          },
          "buildingId": {
            "type": "string"
          },
          "unitId": {
            "type": "string"
          },
          "unitCode": {
            "type": "string"
          },
          "role": {
            "type": "string",
            "enum": [
              "owner",
              "member",
              "tenant"
            ]
          },
          "verified": {
            "type": "boolean"
          }
        }
      },
      "Invoice": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "buildingId": {
            "type": "string"
          },
          "unitId": {
            "type": "string"
          },
          "unitCode": {
            "type": "string"
          },
          "period": {
            "type": "string"
          },
          "kind": {
            "type": "string",
            "enum": [
              "regular",
              "opening",
              "installment",
              "charge"
            ]
          },
          "total": {
            "type": "integer"
          },
          "paidAmount": {
            "type": "integer"
          },
          "dueDate": {
            "type": "string",
            "format": "date"
          },
          "status": {
            "type": "string",
            "enum": [
              "unpaid",
              "partial",
              "paid",
              "void"
            ]
          },
          "paidAt": {
            "type": [
              "integer",
              "null"
            ]
          }
        }
      },
      "AccessSubject": {
        "type": [
          "object",
          "null"
        ],
        "properties": {
          "kind": {
            "type": "string",
            "enum": [
              "unit",
              "guest",
              "move",
              "renovation",
              "vehicle"
            ]
          },
          "id": {
            "type": "string"
          },
          "label": {
            "type": "string"
          },
          "unitCode": {
            "type": "string"
          },
          "buildingId": {
            "type": "string"
          }
        }
      },
      "Webhook": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "url": {
            "type": "string",
            "format": "uri"
          },
          "events": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/WebhookEvent"
            }
          },
          "active": {
            "type": "boolean"
          },
          "failedDeliveries": {
            "type": "integer"
          },
          "createdAt": {
            "type": "integer"
          }
        }
      },
      "WebhookEvent": {
        "type": "string",
        "enum": [
          "invoice.paid",
          "ticket.created",
          "ticket.status_changed",
          "guest.checked_in",
          "vehicle.approved",
          "member.verified",
          "access.denied"
        ]
      },
      "WebhookPayload": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "event": {
            "$ref": "#/components/schemas/WebhookEvent"
          },
          "createdAt": {
            "type": "integer"
          },
          "buildingId": {
            "type": "string"
          },
          "data": {
            "type": "object"
          }
        },
        "description": "POST tới URL đã đăng ký. Header X-Neebo-Signature = sha256=hex(HMAC-SHA256(secret, `${X-Neebo-Timestamp}.${body}`))."
      }
    }
  },
  "paths": {
    "/buildings": {
      "get": {
        "summary": "Tòa trong phạm vi key",
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "items": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Building"
                      }
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "API key không hợp lệ",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Thiếu scope / gói",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Vượt giới hạn",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/units": {
      "get": {
        "summary": "Căn hộ (scope units:read)",
        "parameters": [
          {
            "name": "buildingId",
            "in": "query",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 500,
              "default": 100
            }
          },
          {
            "name": "offset",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 0,
              "default": 0
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "items": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Unit"
                      }
                    },
                    "nextOffset": {
                      "type": [
                        "integer",
                        "null"
                      ]
                    }
                  },
                  "required": [
                    "items"
                  ]
                }
              }
            }
          },
          "401": {
            "description": "API key không hợp lệ",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Thiếu scope / gói",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Vượt giới hạn",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/residents": {
      "get": {
        "summary": "Cư dân, SĐT che (scope residents:read)",
        "parameters": [
          {
            "name": "buildingId",
            "in": "query",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 500,
              "default": 100
            }
          },
          {
            "name": "offset",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 0,
              "default": 0
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "items": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Resident"
                      }
                    },
                    "nextOffset": {
                      "type": [
                        "integer",
                        "null"
                      ]
                    }
                  },
                  "required": [
                    "items"
                  ]
                }
              }
            }
          },
          "401": {
            "description": "API key không hợp lệ",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Thiếu scope / gói",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Vượt giới hạn",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/invoices": {
      "get": {
        "summary": "Hóa đơn (scope invoices:read)",
        "parameters": [
          {
            "name": "buildingId",
            "in": "query",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 500,
              "default": 100
            }
          },
          {
            "name": "offset",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 0,
              "default": 0
            }
          },
          {
            "name": "period",
            "in": "query",
            "schema": {
              "type": "string",
              "example": "2026-10"
            }
          },
          {
            "name": "status",
            "in": "query",
            "schema": {
              "type": "string",
              "enum": [
                "unpaid",
                "paid",
                "void"
              ]
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "items": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Invoice"
                      }
                    },
                    "nextOffset": {
                      "type": [
                        "integer",
                        "null"
                      ]
                    }
                  },
                  "required": [
                    "items"
                  ]
                }
              }
            }
          },
          "401": {
            "description": "API key không hợp lệ",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Thiếu scope / gói",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Vượt giới hạn",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/tickets": {
      "post": {
        "summary": "Tạo phản ánh (scope tickets:write)",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "buildingId",
                  "title"
                ],
                "properties": {
                  "buildingId": {
                    "type": "string"
                  },
                  "unitId": {
                    "type": "string"
                  },
                  "unitCode": {
                    "type": "string"
                  },
                  "title": {
                    "type": "string",
                    "minLength": 3,
                    "maxLength": 120
                  },
                  "description": {
                    "type": "string",
                    "maxLength": 2000
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Đã tạo",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "id": {
                      "type": "string"
                    },
                    "code": {
                      "type": "string"
                    },
                    "status": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "API key không hợp lệ",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Thiếu scope / gói",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Vượt giới hạn",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/access/verify": {
      "post": {
        "summary": "Kiểm tra quyền vào (scope access:verify; guests:verify cho QR; vehicles:verify cho biển số)",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "qr": {
                    "type": "string"
                  },
                  "cardId": {
                    "type": "string"
                  },
                  "plate": {
                    "type": "string"
                  },
                  "gate": {
                    "type": "string"
                  },
                  "buildingId": {
                    "type": "string"
                  }
                },
                "description": "Cần một trong qr, cardId, plate"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "allowed",
                    "reason",
                    "subject"
                  ],
                  "properties": {
                    "allowed": {
                      "type": "boolean"
                    },
                    "reason": {
                      "type": "string"
                    },
                    "subject": {
                      "$ref": "#/components/schemas/AccessSubject"
                    },
                    "logId": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "API key không hợp lệ",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Thiếu scope / gói",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Vượt giới hạn",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/access/events": {
      "post": {
        "summary": "Sự kiện thiết bị cửa / barrier (scope access:verify)",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "buildingId",
                  "gate",
                  "event"
                ],
                "properties": {
                  "buildingId": {
                    "type": "string"
                  },
                  "gate": {
                    "type": "string"
                  },
                  "event": {
                    "type": "string",
                    "enum": [
                      "door_open",
                      "door_close",
                      "door_forced",
                      "barrier_open",
                      "barrier_close",
                      "access_denied",
                      "alarm"
                    ]
                  },
                  "at": {
                    "type": "integer"
                  },
                  "subject": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Đã ghi",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    },
                    "id": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "API key không hợp lệ",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Thiếu scope / gói",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Vượt giới hạn",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/webhooks": {
      "get": {
        "summary": "Danh sách webhook (scope webhooks:manage)",
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "events": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/WebhookEvent"
                      }
                    },
                    "items": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Webhook"
                      }
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "API key không hợp lệ",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Thiếu scope / gói",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Vượt giới hạn",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      },
      "post": {
        "summary": "Tạo webhook; secret trả về một lần",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "url",
                  "events"
                ],
                "properties": {
                  "url": {
                    "type": "string",
                    "format": "uri"
                  },
                  "events": {
                    "type": "array",
                    "items": {
                      "$ref": "#/components/schemas/WebhookEvent"
                    },
                    "minItems": 1
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Đã tạo",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "id": {
                      "type": "string"
                    },
                    "url": {
                      "type": "string"
                    },
                    "events": {
                      "type": "array",
                      "items": {
                        "type": "string"
                      }
                    },
                    "active": {
                      "type": "boolean"
                    },
                    "secret": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "API key không hợp lệ",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Thiếu scope / gói",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Vượt giới hạn",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/webhooks/{id}": {
      "delete": {
        "summary": "Xóa webhook",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "API key không hợp lệ",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Thiếu scope / gói",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Vượt giới hạn",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/webhooks/{id}/deliveries": {
      "get": {
        "summary": "Nhật ký gửi (100 lần gần nhất)",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "items": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "id": {
                            "type": "string"
                          },
                          "event": {
                            "type": "string"
                          },
                          "status": {
                            "type": "string",
                            "enum": [
                              "pending",
                              "delivered",
                              "failed"
                            ]
                          },
                          "attempts": {
                            "type": "integer"
                          },
                          "responseStatus": {
                            "type": [
                              "integer",
                              "null"
                            ]
                          },
                          "error": {
                            "type": "string"
                          },
                          "createdAt": {
                            "type": "integer"
                          },
                          "deliveredAt": {
                            "type": [
                              "integer",
                              "null"
                            ]
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "API key không hợp lệ",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Thiếu scope / gói",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Vượt giới hạn",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    }
  },
  "webhooks": {
    "event": {
      "post": {
        "summary": "Sự kiện gửi tới URL đối tác",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/WebhookPayload"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Trả 2xx trong 10 giây để xác nhận"
          }
        }
      }
    }
  }
}
