{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://raw.githubusercontent.com/api-evangelist/canvas-medical/main/json-schema/canvas-medical-patient-schema.json",
  "title": "Canvas Medical FHIR Patient",
  "description": "JSON Schema for a FHIR R4 Patient resource as supported by Canvas Medical. Includes Canvas-specific extensions for race, ethnicity, birthsex, and preferred pharmacy.",
  "type": "object",
  "required": ["resourceType"],
  "properties": {
    "resourceType": {
      "type": "string",
      "const": "Patient",
      "description": "FHIR resource type identifier"
    },
    "id": {
      "type": "string",
      "description": "Logical identifier assigned by Canvas Medical"
    },
    "meta": {
      "type": "object",
      "properties": {
        "versionId": { "type": "string" },
        "lastUpdated": { "type": "string", "format": "date-time" },
        "profile": {
          "type": "array",
          "items": { "type": "string", "format": "uri" }
        }
      }
    },
    "identifier": {
      "type": "array",
      "description": "External identifiers such as MRN, SSN (last 4), or external system IDs",
      "items": {
        "type": "object",
        "properties": {
          "use": {
            "type": "string",
            "enum": ["usual", "official", "temp", "secondary", "old"]
          },
          "type": {
            "type": "object",
            "properties": {
              "coding": {
                "type": "array",
                "items": {
                  "type": "object",
                  "properties": {
                    "system": { "type": "string", "format": "uri" },
                    "code": { "type": "string" },
                    "display": { "type": "string" }
                  }
                }
              }
            }
          },
          "system": { "type": "string", "format": "uri" },
          "value": { "type": "string" }
        }
      }
    },
    "active": {
      "type": "boolean",
      "description": "Whether the patient record is in active use"
    },
    "name": {
      "type": "array",
      "description": "Patient name(s)",
      "items": {
        "type": "object",
        "properties": {
          "use": {
            "type": "string",
            "enum": ["usual", "official", "temp", "nickname", "anonymous", "old", "maiden"]
          },
          "text": { "type": "string" },
          "family": { "type": "string" },
          "given": {
            "type": "array",
            "items": { "type": "string" }
          },
          "prefix": {
            "type": "array",
            "items": { "type": "string" }
          },
          "suffix": {
            "type": "array",
            "items": { "type": "string" }
          }
        }
      }
    },
    "telecom": {
      "type": "array",
      "description": "Contact details (phone, email)",
      "items": {
        "type": "object",
        "properties": {
          "system": {
            "type": "string",
            "enum": ["phone", "fax", "email", "pager", "url", "sms", "other"]
          },
          "value": { "type": "string" },
          "use": {
            "type": "string",
            "enum": ["home", "work", "temp", "old", "mobile"]
          }
        }
      }
    },
    "gender": {
      "type": "string",
      "enum": ["male", "female", "other", "unknown"],
      "description": "Administrative gender"
    },
    "birthDate": {
      "type": "string",
      "format": "date",
      "description": "Patient date of birth (YYYY-MM-DD)"
    },
    "deceasedBoolean": { "type": "boolean" },
    "deceasedDateTime": { "type": "string", "format": "date-time" },
    "address": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "use": {
            "type": "string",
            "enum": ["home", "work", "temp", "old", "billing"]
          },
          "type": {
            "type": "string",
            "enum": ["postal", "physical", "both"]
          },
          "line": {
            "type": "array",
            "items": { "type": "string" }
          },
          "city": { "type": "string" },
          "state": { "type": "string" },
          "postalCode": { "type": "string" },
          "country": { "type": "string" }
        }
      }
    },
    "communication": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "language": {
            "type": "object",
            "properties": {
              "coding": {
                "type": "array",
                "items": {
                  "type": "object",
                  "properties": {
                    "system": { "type": "string" },
                    "code": { "type": "string" },
                    "display": { "type": "string" }
                  }
                }
              }
            }
          },
          "preferred": { "type": "boolean" }
        }
      }
    },
    "extension": {
      "type": "array",
      "description": "Canvas Medical and US Core extensions",
      "items": {
        "type": "object",
        "required": ["url"],
        "properties": {
          "url": { "type": "string", "format": "uri" },
          "valueCode": { "type": "string" },
          "valueString": { "type": "string" },
          "extension": {
            "type": "array",
            "description": "Nested extensions for complex types like race, ethnicity, preferred pharmacy",
            "items": {
              "type": "object",
              "properties": {
                "url": { "type": "string" },
                "valueString": { "type": "string" },
                "valueCode": { "type": "string" },
                "valueIdentifier": {
                  "type": "object",
                  "properties": {
                    "value": { "type": "string" },
                    "system": { "type": "string" }
                  }
                },
                "valueCodeableConcept": {
                  "type": "object",
                  "properties": {
                    "coding": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "system": { "type": "string" },
                          "code": { "type": "string" },
                          "display": { "type": "string" }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      },
      "examples": [
        {
          "url": "http://hl7.org/fhir/us/core/StructureDefinition/us-core-race",
          "extension": [{ "url": "text", "valueString": "White" }]
        },
        {
          "url": "http://hl7.org/fhir/us/core/StructureDefinition/us-core-ethnicity",
          "extension": [{ "url": "text", "valueString": "Not Hispanic or Latino" }]
        },
        {
          "url": "http://hl7.org/fhir/us/core/StructureDefinition/us-core-birthsex",
          "valueCode": "M"
        },
        {
          "url": "http://schemas.canvasmedical.com/fhir/extensions/preferred-pharmacy",
          "extension": [
            {
              "url": "ncpdp-id",
              "valueIdentifier": {
                "value": "1123152",
                "system": "http://terminology.hl7.org/CodeSystem/NCPDPProviderIdentificationNumber"
              }
            }
          ]
        }
      ]
    }
  }
}
