{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:open-utility-stack:appointment:1",
  "title": "appointment@1",
  "description": "A field visit was agreed, done, failed or called off. A profile of TMF646 Appointment (v4): its status values, with the fields a receiver that owes or refuses compensation for a missed visit decides by (§ 58 TKG, telco). Any field-service system emits it; any role that ordered the visit consumes it.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "profile",
    "client_reference",
    "status",
    "on"
  ],
  "properties": {
    "profile": {
      "const": "appointment@1"
    },
    "client_reference": {
      "description": "The reference the ordering party gave the visit, unchanged; `subject` carries the same value (REF-2).",
      "$ref": "urn:open-utility-stack:common:reference:1#/$defs/ClientReference"
    },
    "appointment": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id"
      ],
      "properties": {
        "id": {
          "description": "The field-service system's own id: it may name the job for `confirmed` and `cancelled` and the visit for `completed` and `failed`. Never a link between statuses; `client_reference` is.",
          "type": "string",
          "pattern": "\\S",
          "maxLength": 128
        }
      }
    },
    "status": {
      "description": "TMF646 AppointmentStatusType. A later `confirmed` for the same `client_reference` replaces the earlier one; consumers order by the CloudEvents `sequence` (CE-9), which an emitter that can send more than one `confirmed` must set.",
      "enum": [
        "confirmed",
        "completed",
        "failed",
        "cancelled"
      ]
    },
    "on": {
      "description": "Per status: `confirmed` the agreed day; `completed` and `failed` the day of the visit; `cancelled` the day it was called off (the day that was called off is the latest `confirmed`'s `on`).",
      "$ref": "urn:open-utility-stack:common:time:1#/$defs/ZonedDate"
    },
    "from": {
      "description": "Start of the agreed slot: a local time in `on.zone`.",
      "$ref": "urn:open-utility-stack:common:time:1#/$defs/LocalTime"
    },
    "until": {
      "description": "End of the agreed slot: a local time in `on.zone`.",
      "$ref": "urn:open-utility-stack:common:time:1#/$defs/LocalTime"
    },
    "half_day": {
      "description": "The agreed half of the day, where no clock window was agreed: `morning` before noon, `afternoon` after, in `on.zone`. Never beside `from` or `until`.",
      "enum": [
        "morning",
        "afternoon"
      ]
    },
    "performer": {
      "description": "The firm that comes or came.",
      "type": "string",
      "pattern": "\\S",
      "maxLength": 120
    },
    "reason": {
      "description": "Why it failed or was called off, in a sentence staff read.",
      "type": "string",
      "pattern": "\\S",
      "maxLength": 200
    },
    "cancelled_at": {
      "description": "When it was called off. Set on `cancelled` whenever the emitter knows it: a receiver decides by it whether the call-off came late (§ 58 TKG).",
      "$ref": "urn:open-utility-stack:common:time:1#/$defs/Instant"
    },
    "cancelled_by": {
      "description": "Who called it off, where the sender knows.",
      "enum": [
        "firm",
        "client",
        "customer"
      ]
    },
    "responsible": {
      "description": "Whose side a failed visit fell on, where the sender says.",
      "enum": [
        "firm",
        "client",
        "customer",
        "nobody"
      ]
    },
    "disputed": {
      "description": "The two sides tell it differently; nothing is settled by it yet.",
      "type": "boolean"
    },
    "extensions": {
      "type": "object",
      "propertyNames": {
        "pattern": "^[a-z][a-z0-9-]*(\\.[a-z][a-z0-9-]*)*$"
      },
      "additionalProperties": {
        "type": "object"
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "status": {
            "const": "confirmed"
          }
        }
      },
      "then": {
        "not": {
          "anyOf": [
            {
              "required": [
                "reason"
              ]
            },
            {
              "required": [
                "responsible"
              ]
            },
            {
              "required": [
                "disputed"
              ]
            }
          ]
        }
      },
      "else": {
        "not": {
          "anyOf": [
            {
              "required": [
                "from"
              ]
            },
            {
              "required": [
                "until"
              ]
            },
            {
              "required": [
                "half_day"
              ]
            }
          ]
        }
      }
    },
    {
      "if": {
        "required": [
          "half_day"
        ]
      },
      "then": {
        "not": {
          "anyOf": [
            {
              "required": [
                "from"
              ]
            },
            {
              "required": [
                "until"
              ]
            }
          ]
        }
      }
    },
    {
      "if": {
        "properties": {
          "status": {
            "not": {
              "const": "cancelled"
            }
          }
        }
      },
      "then": {
        "not": {
          "anyOf": [
            {
              "required": [
                "cancelled_at"
              ]
            },
            {
              "required": [
                "cancelled_by"
              ]
            }
          ]
        }
      }
    }
  ]
}
