{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://arr.local/schemas/opera-structured-result.json",
  "title": "Opera XML finance-ready structured result",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "result_schema_version",
    "status",
    "activation_eligible",
    "ingestion_mode",
    "business_date",
    "processor_version",
    "rule_set_sha256",
    "source_rows",
    "removed_by_rate_code",
    "removed_as_duplicates",
    "output_rows",
    "outcome_counts",
    "channels",
    "artifacts",
    "records",
    "errors"
  ],
  "properties": {
    "result_schema_version": { "type": "string", "enum": ["1.0", "2.0"] },
    "processing_mode": { "type": "string", "const": "daily" },
    "status": { "type": "string", "enum": ["success", "failed"] },
    "activation_eligible": { "type": "boolean" },
    "ingestion_mode": { "type": "string", "const": "opera_xml" },
    "business_date": { "type": ["string", "null"], "format": "date" },
    "processor_version": { "type": "string", "minLength": 1 },
    "rule_set_sha256": { "$ref": "#/$defs/sha256" },
    "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 },
    "outcome_counts": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "duplicate",
        "excluded_rate_code",
        "price_unmatched",
        "retained",
        "validation_failed"
      ],
      "properties": {
        "duplicate": { "type": "integer", "minimum": 0 },
        "excluded_rate_code": { "type": "integer", "minimum": 0 },
        "price_unmatched": { "type": "integer", "minimum": 0 },
        "retained": { "type": "integer", "minimum": 0 },
        "validation_failed": { "type": "integer", "minimum": 0 }
      }
    },
    "channels": {
      "type": "array",
      "items": { "$ref": "#/$defs/channel" }
    },
    "artifacts": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "source_xml",
        "daily_report",
        "monthly_report",
        "result_json",
        "exception_report"
      ],
      "properties": {
        "source_xml": { "$ref": "#/$defs/nullableArtifact" },
        "daily_report": { "$ref": "#/$defs/nullableArtifact" },
        "monthly_report": { "$ref": "#/$defs/nullableArtifact" },
        "result_json": { "$ref": "#/$defs/nullableArtifact" },
        "exception_report": { "$ref": "#/$defs/nullableArtifact" }
      }
    },
    "records": {
      "type": "array",
      "items": { "$ref": "#/$defs/record" }
    },
    "errors": {
      "type": "array",
      "items": { "$ref": "#/$defs/error" }
    }
  },
  "allOf": [
    {
      "if": { "properties": { "status": { "const": "success" } } },
      "then": {
        "properties": {
          "activation_eligible": { "const": true },
          "business_date": { "type": "string", "format": "date" },
          "errors": { "maxItems": 0 },
          "artifacts": {
            "properties": {
              "source_xml": { "$ref": "#/$defs/artifact" },
              "daily_report": { "$ref": "#/$defs/artifact" },
              "result_json": { "$ref": "#/$defs/artifact" },
              "exception_report": { "type": "null" }
            }
          }
        }
      },
      "else": {
        "properties": {
          "activation_eligible": { "const": false },
          "output_rows": { "const": 0 },
          "channels": { "maxItems": 0 },
          "errors": { "minItems": 1 },
          "artifacts": {
            "properties": {
              "daily_report": { "type": "null" },
              "monthly_report": { "type": "null" },
              "exception_report": { "$ref": "#/$defs/artifact" }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": { "result_schema_version": { "const": "2.0" } },
        "required": ["result_schema_version"]
      },
      "then": {
        "required": ["processing_mode"],
        "properties": {
          "processing_mode": { "const": "daily" },
          "artifacts": {
            "properties": { "monthly_report": { "type": "null" } }
          }
        }
      },
      "else": {
        "not": { "required": ["processing_mode"] }
      }
    },
    {
      "if": {
        "properties": {
          "result_schema_version": { "const": "1.0" },
          "status": { "const": "success" }
        },
        "required": ["result_schema_version", "status"]
      },
      "then": {
        "properties": {
          "artifacts": {
            "properties": {
              "monthly_report": { "$ref": "#/$defs/artifact" }
            }
          }
        }
      }
    }
  ],
  "$defs": {
    "sha256": {
      "type": "string",
      "pattern": "^[0-9a-f]{64}$"
    },
    "artifact": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "file_kind",
        "original_filename",
        "path",
        "sha256",
        "byte_size",
        "mime_type"
      ],
      "properties": {
        "file_kind": {
          "type": "string",
          "enum": [
            "opera_xml",
            "daily_xlsx",
            "monthly_xlsx",
            "result_json",
            "exception_xlsx"
          ]
        },
        "original_filename": { "type": "string", "minLength": 1 },
        "path": { "type": "string", "pattern": "^/" },
        "sha256": { "$ref": "#/$defs/sha256" },
        "byte_size": { "type": "integer", "minimum": 0 },
        "mime_type": { "type": "string", "minLength": 1 }
      }
    },
    "nullableArtifact": {
      "oneOf": [
        { "$ref": "#/$defs/artifact" },
        { "type": "null" }
      ]
    },
    "channel": {
      "type": "object",
      "additionalProperties": false,
      "required": ["worksheet", "rows"],
      "properties": {
        "worksheet": { "type": "string", "minLength": 1 },
        "rows": { "type": "integer", "minimum": 0 }
      }
    },
    "record": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "source_sequence",
        "source_location",
        "source_worksheet",
        "source_row_no",
        "outcome",
        "decision_codes",
        "duplicate_of_source_sequence",
        "adults",
        "children",
        "block_code",
        "no_of_rooms",
        "company_name",
        "company_key",
        "confirmation_no",
        "disp_room_no",
        "effective_rate_amount",
        "full_name",
        "res_comment",
        "group_code_key",
        "booking_source_match_status",
        "trace_text",
        "products",
        "rate_code",
        "normalized_rate_code",
        "room_category_label",
        "arrival",
        "departure",
        "nights",
        "real_price",
        "total_price",
        "kb_amount",
        "channel_key",
        "pricing_method"
      ],
      "properties": {
        "source_sequence": { "type": "integer", "minimum": 1 },
        "source_location": { "type": "string", "minLength": 1 },
        "source_worksheet": { "type": "null" },
        "source_row_no": { "type": "null" },
        "outcome": {
          "type": "string",
          "enum": [
            "retained",
            "excluded_rate_code",
            "duplicate",
            "validation_failed",
            "price_unmatched"
          ]
        },
        "decision_codes": {
          "type": "array",
          "uniqueItems": true,
          "items": { "type": "string", "minLength": 1 }
        },
        "duplicate_of_source_sequence": { "type": ["integer", "null"], "minimum": 1 },
        "adults": { "type": ["integer", "null"] },
        "children": { "type": ["integer", "null"] },
        "block_code": { "type": "string" },
        "no_of_rooms": { "type": ["integer", "null"] },
        "company_name": { "type": "string" },
        "company_key": { "type": ["string", "null"], "minLength": 1 },
        "confirmation_no": { "type": "string" },
        "disp_room_no": { "type": "string" },
        "effective_rate_amount": { "type": ["number", "null"] },
        "full_name": { "type": "string" },
        "res_comment": { "type": "string" },
        "group_code_key": { "type": ["string", "null"], "minLength": 1 },
        "booking_source_match_status": {
          "type": "string",
          "enum": ["missing_group_code", "not_checked"]
        },
        "trace_text": { "type": "string" },
        "products": { "type": "string" },
        "rate_code": { "type": "string" },
        "normalized_rate_code": { "type": ["string", "null"], "minLength": 1 },
        "room_category_label": { "type": "string" },
        "arrival": { "type": ["string", "null"], "format": "date" },
        "departure": { "type": ["string", "null"], "format": "date" },
        "nights": { "type": ["integer", "null"] },
        "real_price": { "type": ["number", "null"] },
        "total_price": { "type": ["number", "null"] },
        "kb_amount": { "type": ["number", "null"] },
        "channel_key": { "type": ["string", "null"], "minLength": 1 },
        "pricing_method": {
          "type": ["string", "null"],
          "enum": ["zero_price_exception", "price_reference_exact", null]
        }
      },
      "allOf": [
        {
          "if": { "properties": { "outcome": { "const": "duplicate" } } },
          "then": {
            "properties": {
              "duplicate_of_source_sequence": { "type": "integer", "minimum": 1 }
            }
          },
          "else": {
            "properties": { "duplicate_of_source_sequence": { "type": "null" } }
          }
        },
        {
          "if": { "properties": { "outcome": { "const": "retained" } } },
          "then": {
            "properties": {
              "adults": { "type": "integer", "minimum": 0 },
              "children": { "type": "integer", "minimum": 0 },
              "no_of_rooms": { "type": "integer", "minimum": 1 },
              "effective_rate_amount": { "type": "number", "minimum": 0 },
              "nights": { "type": "integer", "minimum": 0 },
              "real_price": { "type": "number", "minimum": 0 },
              "total_price": { "type": "number", "minimum": 0 },
              "kb_amount": { "type": ["number", "null"], "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 }
      }
    }
  }
}
