{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "ARR Opera daily processing result",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "version",
    "status",
    "business_date",
    "message",
    "metrics",
    "outputs",
    "errors"
  ],
  "properties": {
    "version": { "type": "string", "const": "3.0" },
    "status": { "type": "string", "enum": ["success", "failed"] },
    "business_date": { "type": ["string", "null"], "format": "date" },
    "message": { "type": "string" },
    "metrics": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "source_rows",
        "removed_by_rate_code",
        "removed_as_duplicates",
        "output_rows",
        "channels"
      ],
      "properties": {
        "source_rows": { "type": "integer", "minimum": 0 },
        "removed_by_rate_code": { "type": "integer", "minimum": 0 },
        "removed_as_duplicates": { "type": "integer", "minimum": 0 },
        "output_rows": { "type": "integer", "minimum": 0 },
        "channels": {
          "type": "array",
          "items": { "$ref": "#/$defs/channel" }
        }
      }
    },
    "outputs": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "daily_report",
        "structured_result",
        "exception_report"
      ],
      "properties": {
        "daily_report": {
          "type": ["string", "null"],
          "pattern": "^[^/\\\\]+\\.xlsx$"
        },
        "structured_result": {
          "type": ["string", "null"],
          "pattern": "^[^/\\\\]+\\.json$"
        },
        "exception_report": {
          "type": ["string", "null"],
          "pattern": "^[^/\\\\]+\\.xlsx$"
        }
      }
    },
    "errors": {
      "type": "array",
      "items": { "$ref": "#/$defs/error" }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": { "status": { "const": "success" } },
        "required": ["status"]
      },
      "then": {
        "properties": {
          "business_date": { "type": "string", "format": "date" },
          "outputs": {
            "properties": {
              "daily_report": { "type": "string" },
              "structured_result": { "type": "string" },
              "exception_report": { "type": "null" }
            }
          },
          "errors": { "maxItems": 0 }
        }
      },
      "else": {
        "properties": {
          "outputs": {
            "properties": {
              "daily_report": { "type": "null" },
              "exception_report": { "type": "string" }
            }
          },
          "errors": { "minItems": 1 }
        }
      }
    }
  ],
  "$defs": {
    "channel": {
      "type": "object",
      "additionalProperties": false,
      "required": ["worksheet", "rows"],
      "properties": {
        "worksheet": { "type": "string", "minLength": 1 },
        "rows": { "type": "integer", "minimum": 0 }
      }
    },
    "error": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "code",
        "stage",
        "source_location",
        "company_name",
        "rate_code",
        "effective_rate_amount",
        "confirmation_no",
        "message"
      ],
      "properties": {
        "code": { "type": "string", "minLength": 1 },
        "stage": { "type": "string", "minLength": 1 },
        "source_location": { "type": ["string", "null"] },
        "company_name": { "type": ["string", "null"] },
        "rate_code": { "type": ["string", "null"] },
        "effective_rate_amount": { "type": ["number", "null"] },
        "confirmation_no": { "type": ["string", "null"] },
        "message": { "type": "string", "minLength": 1 }
      }
    }
  }
}
