{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://hupe1980.github.io/emob/schemas/events/de.emob.cdr.issued.json",
  "title": "de.emob.cdr.issued",
  "description": "A charging session's record was issued by csmsd: its signed evidence verified and the record was stored. Structured-mode CloudEvent 1.0, signed per Standard Webhooks; webhook-id equals id.",
  "type": "object",
  "required": [
    "specversion",
    "id",
    "source",
    "type",
    "subject",
    "time",
    "datacontenttype",
    "data"
  ],
  "if": {
    "required": ["emobtest"],
    "properties": { "emobtest": { "const": true } }
  },
  "else": {
    "properties": {
      "data": {
        "properties": {
          "cdr": {
            "required": ["key", "session_id", "evse_id", "started_at", "ended_at", "periods", "total_energy", "direction", "auth_path"],
            "properties": {
              "key": {
                "type": "object",
                "required": ["party", "id"],
                "properties": {
                  "party": { "type": "string", "pattern": "^[A-Z]{2}\\*[A-Z0-9]{3}$" },
                  "id": { "type": "string", "minLength": 1 }
                }
              },
              "session_id": { "type": "string", "minLength": 1 },
              "evse_id": { "type": "string", "minLength": 1 },
              "started_at": { "type": "string", "format": "date-time" },
              "ended_at": { "type": "string", "format": "date-time" },
              "periods": { "type": "array", "items": { "type": "object" } },
              "total_energy": { "type": "string", "pattern": "^[0-9]+(\\.[0-9]+)?$", "description": "Exact, non-negative kWh." },
              "direction": { "enum": ["import", "export"] }
            }
          },
          "token": {
            "required": ["uid", "token_type", "contract_id", "issuer"],
            "properties": {
              "uid": { "type": "string" },
              "token_type": { "enum": ["Rfid", "AppUser", "Emaid", "AdHocUser", "Other"] },
              "contract_id": { "type": "string", "minLength": 1 },
              "issuer": { "type": "string", "pattern": "^[A-Z]{2}\\*[A-Z0-9]{3}$" }
            }
          }
        }
      }
    }
  },
  "properties": {
    "specversion": {
      "const": "1.0"
    },
    "id": {
      "type": "string",
      "minLength": 1,
      "description": "The record's key; the receiver's idempotency key."
    },
    "source": {
      "const": "urn:emob:csmsd"
    },
    "type": {
      "const": "de.emob.cdr.issued"
    },
    "subject": {
      "type": "string",
      "pattern": "^cdr/.+"
    },
    "time": {
      "type": "string",
      "format": "date-time"
    },
    "datacontenttype": {
      "const": "application/json"
    },
    "emobtest": {
      "type": "boolean",
      "description": "Present and true only on a sample `emob subscribers test` or emobtest sent: take it (2xx) and never book it."
    },
    "data": {
      "type": "object",
      "required": [
        "cdr",
        "token",
        "signed",
        "station"
      ],
      "properties": {
        "cdr": {
          "type": "object",
          "description": "The record as emob-cdr serialises it: key, session, energy, cost, evidence reference."
        },
        "token": {
          "type": "object",
          "description": "Whom it bills: the token as presented, its contract and issuer. Personal data."
        },
        "signed": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "description": "The signed OCMF records it rests on, verbatim."
        },
        "station": {
          "type": "string",
          "description": "The OCPP identity of the station that ran it."
        },
        "retail": {
          "type": "object",
          "description": "Native agreement and price recorded at transaction authorization. Only the private delivery to billd contains this member.",
          "required": ["agreement", "reference"],
          "additionalProperties": false,
          "properties": {
            "agreement": {"type": "string", "pattern": "^[0-9a-f]{64}$"},
            "reference": {"type": "string", "pattern": "^[0-9a-f]{64}$"}
          }
        },
        "receipt": {
          "type": "string",
          "format": "uri",
          "description": "The driver's receipt page, for every receiver except the roaming node."
        },
        "point": {
          "type": "object",
          "required": [
            "evse",
            "connector",
            "rated_power_kw",
            "location"
          ],
          "description": "The point and its place, as the station registry holds them: EVSE id, connector, rated power, and the location (id, name, address, coordinates, time zone).",
          "properties": {
            "evse": {
              "type": "string"
            },
            "connector": {
              "type": "string"
            },
            "rated_power_kw": {
              "type": "string"
            },
            "location": {
              "type": "object"
            }
          }
        }
      }
    }
  }
}
