{
  "$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": "4.0" },
    "status": { "type": "string", "enum": ["success", "review_required", "failed"] },
    "business_date": { "type": ["string", "null"], "format": "date" },
    "message": { "type": "string", "minLength": 1 },
    "metrics": {
      "type": "object",
      "additionalProperties": false,
      "required": ["source_rows", "removed_by_rate_code", "removed_as_duplicates", "output_rows", "candidate_rows", "review_required_rows", "review_issue_count", "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 },
        "candidate_rows": { "type": "integer", "minimum": 0 },
        "review_required_rows": { "type": "integer", "minimum": 0 },
        "review_issue_count": { "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" },
          "errors": { "maxItems": 0 },
          "metrics": { "properties": { "candidate_rows": { "const": 0 }, "review_required_rows": { "const": 0 }, "review_issue_count": { "const": 0 } } },
          "outputs": { "properties": { "daily_report": { "type": "string" }, "structured_result": { "type": "string" }, "exception_report": { "type": "null" } } }
        }
      }
    },
    {
      "if": { "properties": { "status": { "const": "review_required" } }, "required": ["status"] },
      "then": {
        "properties": {
          "business_date": { "type": "string", "format": "date" },
          "errors": { "minItems": 1 },
          "metrics": { "properties": { "output_rows": { "const": 0 }, "review_required_rows": { "minimum": 1 }, "review_issue_count": { "minimum": 1 }, "channels": { "maxItems": 0 } } },
          "outputs": { "properties": { "daily_report": { "type": "null" }, "structured_result": { "type": "string" }, "exception_report": { "type": "null" } } }
        }
      }
    },
    {
      "if": { "properties": { "status": { "const": "failed" } }, "required": ["status"] },
      "then": {
        "properties": {
          "errors": { "minItems": 1 },
          "metrics": { "properties": { "output_rows": { "const": 0 }, "candidate_rows": { "const": 0 }, "review_issue_count": { "const": 0 }, "channels": { "maxItems": 0 } } },
          "outputs": { "properties": { "daily_report": { "type": "null" }, "exception_report": { "type": "string" } } }
        }
      }
    }
  ],
  "$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 }
      }
    }
  }
}
