{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "ARR authenticated Opera daily processing result",
  "type": "object",
  "additionalProperties": false,
  "required": ["signed_envelope_version", "payload", "signature"],
  "properties": {
    "signed_envelope_version": {
      "type": "string",
      "const": "arr-processing-signed-1"
    },
    "payload": { "$ref": "#/$defs/result" },
    "signature": { "$ref": "#/$defs/signature" }
  },
  "$defs": {
    "opaqueId": {
      "type": "string",
      "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$"
    },
    "sha256": {
      "type": "string",
      "pattern": "^[0-9a-f]{64}$"
    },
    "signature": {
      "type": "object",
      "additionalProperties": false,
      "required": ["algorithm", "key_id", "issued_at", "nonce", "value"],
      "properties": {
        "algorithm": { "type": "string", "const": "hmac-sha256" },
        "key_id": { "$ref": "#/$defs/opaqueId" },
        "issued_at": { "type": "string", "format": "date-time" },
        "nonce": { "$ref": "#/$defs/opaqueId" },
        "value": { "$ref": "#/$defs/sha256" }
      }
    },
    "baseFile": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "file_handle",
        "original_filename",
        "sha256",
        "byte_size",
        "mime_type"
      ],
      "properties": {
        "file_handle": { "$ref": "#/$defs/opaqueId" },
        "original_filename": {
          "type": "string",
          "minLength": 1,
          "maxLength": 255,
          "pattern": "^[^/\\\\]+$"
        },
        "sha256": { "$ref": "#/$defs/sha256" },
        "byte_size": { "type": "integer", "minimum": 0 },
        "mime_type": { "type": "string" }
      }
    },
    "dailyReport": {
      "allOf": [
        { "$ref": "#/$defs/baseFile" },
        {
          "properties": {
            "original_filename": { "pattern": "^[^/\\\\]+\\.xlsx$" },
            "byte_size": { "maximum": 104857600 },
            "mime_type": {
              "const": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
            }
          }
        }
      ]
    },
    "resultJson": {
      "allOf": [
        { "$ref": "#/$defs/baseFile" },
        {
          "properties": {
            "original_filename": { "pattern": "^[^/\\\\]+\\.json$" },
            "byte_size": { "maximum": 5242880 },
            "mime_type": { "const": "application/json" }
          }
        }
      ]
    },
    "structuredResult": {
      "allOf": [
        { "$ref": "#/$defs/baseFile" },
        {
          "properties": {
            "original_filename": { "pattern": "^[^/\\\\]+\\.json$" },
            "byte_size": { "maximum": 52428800 },
            "mime_type": { "const": "application/json" }
          }
        }
      ]
    },
    "exceptionReport": {
      "allOf": [
        { "$ref": "#/$defs/baseFile" },
        {
          "properties": {
            "original_filename": { "pattern": "^[^/\\\\]+\\.xlsx$" },
            "byte_size": { "maximum": 20971520 },
            "mime_type": {
              "const": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
            }
          }
        }
      ]
    },
    "result": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "contract_version",
        "delivery_id",
        "job_id",
        "attempt_no",
        "remote_run_id",
        "status",
        "business_date",
        "processor_version",
        "rule_set_sha256",
        "result_schema_version",
        "artifacts"
      ],
      "properties": {
        "contract_version": {
          "type": "string",
          "const": "arr-opera-daily-result-1"
        },
        "delivery_id": { "$ref": "#/$defs/opaqueId" },
        "job_id": { "$ref": "#/$defs/opaqueId" },
        "attempt_no": { "type": "integer", "minimum": 1, "maximum": 9999 },
        "remote_run_id": { "$ref": "#/$defs/opaqueId" },
        "status": { "type": "string", "enum": ["success", "failed"] },
        "business_date": { "type": ["string", "null"], "format": "date" },
        "processor_version": {
          "type": "string",
          "pattern": "^[A-Za-z0-9][A-Za-z0-9._+-]{0,63}$"
        },
        "rule_set_sha256": { "$ref": "#/$defs/sha256" },
        "result_schema_version": { "type": "string", "const": "3.0" },
        "artifacts": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "daily_report",
            "result_json",
            "structured_result_json",
            "exception_report"
          ],
          "properties": {
            "daily_report": {
              "oneOf": [{ "$ref": "#/$defs/dailyReport" }, { "type": "null" }]
            },
            "result_json": { "$ref": "#/$defs/resultJson" },
            "structured_result_json": { "$ref": "#/$defs/structuredResult" },
            "exception_report": {
              "oneOf": [{ "$ref": "#/$defs/exceptionReport" }, { "type": "null" }]
            }
          }
        }
      },
      "allOf": [
        {
          "if": {
            "properties": { "status": { "const": "success" } },
            "required": ["status"]
          },
          "then": {
            "properties": {
              "business_date": { "type": "string", "format": "date" },
              "artifacts": {
                "properties": {
                  "daily_report": { "$ref": "#/$defs/dailyReport" },
                  "exception_report": { "type": "null" }
                }
              }
            }
          },
          "else": {
            "properties": {
              "artifacts": {
                "properties": {
                  "daily_report": { "type": "null" },
                  "exception_report": { "$ref": "#/$defs/exceptionReport" }
                }
              }
            }
          }
        }
      ]
    }
  }
}
