{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "ARR Opera daily Agent frozen result",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "contract_version",
    "job_id",
    "source_file_id",
    "status",
    "business_date",
    "processor_result",
    "files"
  ],
  "properties": {
    "contract_version": {
      "type": "string",
      "const": "arr-opera-daily-agent-1"
    },
    "job_id": { "$ref": "#/$defs/opaqueId" },
    "source_file_id": { "$ref": "#/$defs/opaqueId" },
    "status": { "type": "string", "enum": ["success", "failed"] },
    "business_date": {
      "type": "string",
      "pattern": "^(?:[0-9]{4}-[0-9]{2}-[0-9]{2})?$",
      "description": "Skill business date, or an empty string when the Skill result has no business date"
    },
    "processor_result": { "$ref": "#/$defs/processorResult" },
    "files": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "daily_report",
        "result_json",
        "structured_result",
        "exception_report"
      ],
      "properties": {
        "daily_report": { "$ref": "#/$defs/nullableFile" },
        "result_json": { "$ref": "#/$defs/jsonFile" },
        "structured_result": { "$ref": "#/$defs/nullableFile" },
        "exception_report": { "$ref": "#/$defs/nullableFile" }
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": { "status": { "const": "success" } },
        "required": ["status"]
      },
      "then": {
        "properties": {
          "business_date": { "type": "string", "format": "date" },
          "processor_result": {
            "properties": { "status": { "const": "success" } }
          },
          "files": {
            "properties": {
              "daily_report": { "$ref": "#/$defs/xlsxFile" },
              "result_json": { "$ref": "#/$defs/jsonFile" },
              "structured_result": { "$ref": "#/$defs/jsonFile" },
              "exception_report": { "type": "null" }
            }
          }
        }
      },
      "else": {
        "properties": {
          "processor_result": {
            "properties": { "status": { "const": "failed" } }
          },
          "files": {
            "properties": {
              "daily_report": { "type": "null" },
              "result_json": { "$ref": "#/$defs/jsonFile" },
              "structured_result": { "$ref": "#/$defs/jsonFile" },
              "exception_report": { "$ref": "#/$defs/xlsxFile" }
            }
          }
        }
      }
    }
  ],
  "$defs": {
    "opaqueId": {
      "type": "string",
      "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$"
    },
    "file": {
      "type": "object",
      "additionalProperties": false,
      "required": ["filename", "local_path"],
      "properties": {
        "filename": {
          "type": "string",
          "pattern": "^[^/\\\\]+$"
        },
        "local_path": {
          "type": "string",
          "pattern": "^/"
        }
      }
    },
    "xlsxFile": {
      "allOf": [
        { "$ref": "#/$defs/file" },
        {
          "properties": {
            "filename": { "pattern": "^[^/\\\\]+\\.xlsx$" }
          }
        }
      ]
    },
    "jsonFile": {
      "allOf": [
        { "$ref": "#/$defs/file" },
        {
          "properties": {
            "filename": { "pattern": "^[^/\\\\]+\\.json$" }
          }
        }
      ]
    },
    "nullableFile": {
      "oneOf": [
        { "$ref": "#/$defs/file" },
        { "type": "null" }
      ]
    },
    "processorResult": {
      "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" },
        "outputs": { "type": "object" },
        "errors": { "type": "array" }
      }
    }
  }
}
