{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://arr.local/schemas/opera-daily-structured-result-v4.json",
  "title": "ARR Opera daily 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", "candidate_rows",
    "review_required_rows", "review_issue_count", "review_issues", "review_case_id",
    "manual_override_sha256", "manually_priced_rows", "channels", "artifacts", "records", "errors"
  ],
  "properties": {
    "result_schema_version": { "type": "string", "const": "4.0" },
    "status": { "type": "string", "enum": ["success", "review_required", "failed"] },
    "activation_eligible": { "type": "boolean" },
    "ingestion_mode": { "type": "string", "const": "opera_xml" },
    "business_date": { "type": ["string", "null"], "format": "date" },
    "processor_version": { "type": "string", "const": "4.0.0" },
    "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 },
    "candidate_rows": { "type": "integer", "minimum": 0 },
    "review_required_rows": { "type": "integer", "minimum": 0 },
    "review_issue_count": { "type": "integer", "minimum": 0 },
    "review_issues": { "type": "array", "items": { "$ref": "#/$defs/reviewIssue" } },
    "review_case_id": { "type": ["string", "null"], "pattern": "^dailyreview-[0-9a-f]{32}$" },
    "manual_override_sha256": { "oneOf": [{ "$ref": "#/$defs/sha256" }, { "type": "null" }] },
    "manually_priced_rows": { "type": "integer", "minimum": 0 },
    "outcome_counts": {
      "type": "object", "additionalProperties": false,
      "required": ["candidate", "duplicate", "excluded_rate_code", "price_unmatched", "retained", "validation_failed"],
      "properties": {
        "candidate": { "type": "integer", "minimum": 0 },
        "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", "result_json", "exception_report", "manual_override_json"],
      "properties": {
        "source_xml": { "$ref": "#/$defs/nullableArtifact" },
        "daily_report": { "$ref": "#/$defs/nullableArtifact" },
        "result_json": { "$ref": "#/$defs/nullableArtifact" },
        "exception_report": { "$ref": "#/$defs/nullableArtifact" },
        "manual_override_json": { "$ref": "#/$defs/nullableArtifact" }
      }
    },
    "records": { "type": "array", "items": { "$ref": "#/$defs/record" } },
    "errors": { "type": "array", "items": { "$ref": "#/$defs/error" } }
  },
  "allOf": [
    {
      "if": { "properties": { "status": { "const": "success" } }, "required": ["status"] },
      "then": {
        "properties": {
          "activation_eligible": { "const": true }, "business_date": { "type": "string", "format": "date" },
          "errors": { "maxItems": 0 }, "candidate_rows": { "const": 0 }, "review_required_rows": { "const": 0 },
          "review_issue_count": { "const": 0 }, "review_issues": { "maxItems": 0 },
          "artifacts": { "properties": {
            "source_xml": { "$ref": "#/$defs/artifact" }, "daily_report": { "$ref": "#/$defs/artifact" },
            "result_json": { "$ref": "#/$defs/artifact" }, "exception_report": { "type": "null" }
          } }
        }
      }
    },
    {
      "if": { "properties": { "status": { "const": "review_required" } }, "required": ["status"] },
      "then": {
        "properties": {
          "activation_eligible": { "const": false }, "business_date": { "type": "string", "format": "date" },
          "output_rows": { "const": 0 }, "review_required_rows": { "minimum": 1 }, "review_issue_count": { "minimum": 1 },
          "errors": { "minItems": 1 }, "channels": { "maxItems": 0 }, "review_case_id": { "type": "null" },
          "manual_override_sha256": { "type": "null" }, "manually_priced_rows": { "const": 0 },
          "artifacts": { "properties": {
            "source_xml": { "$ref": "#/$defs/artifact" }, "result_json": { "$ref": "#/$defs/artifact" },
            "daily_report": { "type": "null" }, "exception_report": { "type": "null" }, "manual_override_json": { "type": "null" }
          } }
        }
      }
    },
    {
      "if": { "properties": { "status": { "const": "failed" } }, "required": ["status"] },
      "then": {
        "properties": {
          "activation_eligible": { "const": false }, "output_rows": { "const": 0 }, "candidate_rows": { "const": 0 },
          "review_issue_count": { "const": 0 }, "review_issues": { "maxItems": 0 }, "review_case_id": { "type": "null" },
          "manual_override_sha256": { "type": "null" }, "manually_priced_rows": { "const": 0 }, "channels": { "maxItems": 0 },
          "errors": { "minItems": 1 },
          "artifacts": { "properties": {
            "daily_report": { "type": "null" }, "exception_report": { "$ref": "#/$defs/artifact" },
            "manual_override_json": { "type": "null" }
          } }
        }
      }
    }
  ],
  "$defs": {
    "sha256": { "type": "string", "pattern": "^[0-9a-f]{64}$" },
    "artifact": {
      "type": "object", "additionalProperties": false,
      "required": ["file_kind", "original_filename", "sha256", "byte_size", "mime_type"],
      "properties": {
        "file_kind": { "type": "string", "enum": ["opera_xml", "daily_xlsx", "result_json", "exception_xlsx", "manual_override_json"] },
        "original_filename": { "type": "string", "minLength": 1 },
        "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 } }
    },
    "reviewIssue": {
      "type": "object", "additionalProperties": false,
      "required": ["company_key", "rate_code", "effective_rate_amount", "candidate_prices", "affected_records", "affected_rooms", "affected_room_nights"],
      "properties": {
        "company_key": { "type": "string", "minLength": 1 }, "rate_code": { "type": "string", "minLength": 1 },
        "effective_rate_amount": { "type": "number", "minimum": 0 },
        "candidate_prices": { "type": "array", "items": { "$ref": "#/$defs/priceCandidate" } },
        "affected_records": { "type": "integer", "minimum": 1 }, "affected_rooms": { "type": "integer", "minimum": 1 },
        "affected_room_nights": { "type": "integer", "minimum": 0 }
      }
    },
    "priceCandidate": {
      "type": "object", "additionalProperties": false, "required": ["effective_rate_amount", "real_price"],
      "properties": { "effective_rate_amount": { "type": "number", "minimum": 0 }, "real_price": { "type": "number", "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", "candidate", "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", "manual_review", null] }
      },
      "allOf": [
        {
          "if": { "properties": { "outcome": { "const": "duplicate" } }, "required": ["outcome"] },
          "then": { "properties": { "duplicate_of_source_sequence": { "type": "integer", "minimum": 1 } } },
          "else": { "properties": { "duplicate_of_source_sequence": { "type": "null" } } }
        },
        {
          "if": { "properties": { "outcome": { "enum": ["retained", "candidate"] } }, "required": ["outcome"] },
          "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 },
            "channel_key": { "type": "string", "minLength": 1 }, "pricing_method": { "type": "string", "enum": ["zero_price_exception", "price_reference_exact", "manual_review"] }
          } }
        }
      ]
    },
    "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 }
      }
    }
  }
}
