{
 "openapi": "3.1.0",
 "info": {
  "title": "Taw'am connector and webhook API",
  "version": "2.0",
  "description": "Send hospital events to a Taw'am twin. Authenticate with a per-hospital API key (Bearer twk_…). Patient identifiers are replaced by a pseudonym before anything is stored. Events with an id are idempotent."
 },
 "servers": [
  {
   "url": "https://tawam.zohdi-portfolio.workers.dev"
  }
 ],
 "components": {
  "securitySchemes": {
   "key": {
    "type": "http",
    "scheme": "bearer",
    "description": "twk_… (production) or twk_test_… (sandbox)"
   }
  },
  "schemas": {
   "Result": {
    "type": "object",
    "properties": {
     "accepted": {
      "type": "integer"
     },
     "rejected": {
      "type": "integer"
     },
     "duplicates": {
      "type": "integer"
     },
     "errors": {
      "type": "array",
      "items": {
       "type": "string"
      }
     }
    }
   },
   "Event": {
    "type": "object",
    "required": [
     "kind"
    ],
    "properties": {
     "id": {
      "type": "string",
      "description": "Your message id; repeats are reported as duplicates"
     },
     "kind": {
      "type": "string",
      "enum": [
       "arrival",
       "triage",
       "admit_decision",
       "move",
       "discharge",
       "bed_status",
       "ble"
      ]
     },
     "at": {
      "type": "string",
      "format": "date-time"
     },
     "patient": {
      "type": "string",
      "description": "MRN or other identifier; pseudonymised on arrival"
     },
     "esi": {
      "type": "integer",
      "minimum": 1,
      "maximum": 5
     },
     "type": {
      "type": "string",
      "enum": [
       "medical",
       "surgical",
       "icu"
      ]
     },
     "bed": {
      "type": "string",
      "example": "F1-A03-2"
     },
     "status": {
      "type": "string",
      "enum": [
       "dirty",
       "cleaning",
       "free"
      ]
     },
     "sex": {
      "type": "string",
      "enum": [
       "F",
       "M"
      ],
      "description": "Used only for single-sex room rules"
     },
     "isolation": {
      "type": "boolean",
      "description": "Contact isolation (cohorting)"
     },
     "left_without_being_seen": {
      "type": "boolean"
     }
    }
   }
  }
 },
 "security": [
  {
   "key": []
  }
 ],
 "paths": {
  "/api/v1/events": {
   "post": {
    "summary": "Native JSON events (up to 500 per request)",
    "requestBody": {
     "content": {
      "application/json": {
       "schema": {
        "oneOf": [
         {
          "type": "array",
          "items": {
           "$ref": "#/components/schemas/Event"
          }
         },
         {
          "type": "object",
          "properties": {
           "events": {
            "type": "array",
            "items": {
             "$ref": "#/components/schemas/Event"
            }
           }
          }
         }
        ]
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "Applied",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Result"
        }
       }
      }
     },
     "401": {
      "description": "Missing or revoked key"
     },
     "422": {
      "description": "Nothing could be applied"
     },
     "429": {
      "description": "Rate limit (120/min per key)"
     }
    }
   }
  },
  "/api/v1/hl7": {
   "post": {
    "summary": "HL7 v2 ADT messages: A01 admit, A02 transfer, A03 discharge, A04 register. PV1-3 = unit^room^bed, PID-8 sex.",
    "requestBody": {
     "content": {
      "text/plain": {
       "schema": {
        "type": "string"
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "Applied",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Result"
        }
       }
      }
     }
    }
   }
  },
  "/api/v1/fhir": {
   "post": {
    "summary": "FHIR R4 Encounter (arrived/triaged, in-progress at a bed Location, finished) and Location operationalStatus (H/K dirty, U free); a resource or a Bundle",
    "requestBody": {
     "content": {
      "application/fhir+json": {
       "schema": {
        "type": "object"
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "Applied",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Result"
        }
       }
      }
     }
    }
   }
  },
  "/api/v1/ble": {
   "post": {
    "summary": "BLE gateway batch: readings per tag; the twin locates each tag with an HMM filter",
    "requestBody": {
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "properties": {
         "gateway": {
          "type": "string"
         },
         "at": {
          "type": "string",
          "format": "date-time"
         },
         "readings": {
          "type": "array",
          "items": {
           "type": "object",
           "required": [
            "tag",
            "beacon",
            "rssi"
           ],
           "properties": {
            "tag": {
             "type": "string"
            },
            "beacon": {
             "type": "string"
            },
            "rssi": {
             "type": "number"
            },
            "kind": {
             "type": "string",
             "enum": [
              "wheelchair",
              "pump",
              "bed",
              "monitor"
             ]
            }
           }
          }
         }
        }
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "Applied",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Result"
        }
       }
      }
     }
    }
   }
  }
 },
 "webhooks": {
  "event": {
   "post": {
    "summary": "Outgoing webhook (er.warning, task.created, task.done, bed.assigned, infection.exposure, test). Verify Tawam-Signature: t=<unix>,v1=hex(HMAC-SHA256(secret, '<t>.<body>')).",
    "requestBody": {
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "properties": {
         "event": {
          "type": "string"
         },
         "at": {
          "type": "string"
         },
         "data": {
          "type": "object"
         }
        }
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "Any 2xx counts as delivered"
     }
    }
   }
  }
 }
}