{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:aggregator-gg:cpapi:refund:v1",
  "title": "CPAPI full bet refund",
  "description": "Candidate studio Money contract for an eligible fiat or crypto session. Select Refund for the request and Result for a synchronous 201 receipt.",
  "$defs": {
    "Id": {
      "type": "string",
      "pattern": "^[A-Za-z0-9._:-]{1,128}$",
      "minLength": 1,
      "maxLength": 128
    },
    "Amount": {
      "type": "object",
      "additionalProperties": false,
      "required": ["currency", "amount_minor"],
      "properties": {
        "currency": {
          "type": "string",
          "pattern": "^[A-Z]{3}$",
          "minLength": 3,
          "maxLength": 3,
          "description": "Use the currency and minor-unit precision of the original session."
        },
        "amount_minor": {
          "type": "integer",
          "minimum": 0,
          "maximum": 9007199254740991,
          "description": "The original JSON token must be a plain unsigned integer. The Money raw-body rules also apply before schema validation."
        }
      }
    },
    "PositiveAmount": {
      "allOf": [
        { "$ref": "#/$defs/Amount" },
        { "properties": { "amount_minor": { "minimum": 1 } } }
      ]
    },
    "Refund": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "tenant_id", "integration_id", "environment", "player_ref", "session_id",
        "round_id", "transaction_id", "amount", "bet_transaction_id"
      ],
      "properties": {
        "tenant_id": { "$ref": "#/$defs/Id" },
        "integration_id": { "$ref": "#/$defs/Id" },
        "environment": { "enum": ["certification", "live"] },
        "player_ref": { "$ref": "#/$defs/Id" },
        "session_id": { "$ref": "#/$defs/Id" },
        "round_id": { "$ref": "#/$defs/Id" },
        "transaction_id": {
          "$ref": "#/$defs/Id",
          "description": "The refund's own transaction ID. Reuse it for recovery."
        },
        "amount": { "$ref": "#/$defs/PositiveAmount" },
        "bet_transaction_id": {
          "$ref": "#/$defs/Id",
          "description": "The studio transaction_id of the original bet. The refund must match its entire amount, currency, player, session and round."
        },
        "provider_transaction_ref": { "$ref": "#/$defs/Id" },
        "occurred_at": {
          "type": "string",
          "format": "date-time",
          "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}[Tt](?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](?:[.][0-9]+)?(?:[Zz]|[+-](?:[01][0-9]|2[0-3]):[0-5][0-9])$",
          "description": "Optional send time, excluded from the Money idempotency fingerprint."
        }
      }
    },
    "Result": {
      "type": "object",
      "additionalProperties": false,
      "required": ["transaction_id", "outcome", "outcome_finality", "balance"],
      "properties": {
        "transaction_id": { "$ref": "#/$defs/Id" },
        "outcome": { "enum": ["committed", "declined"] },
        "outcome_finality": { "const": "terminal" },
        "outcome_reason": {
          "type": "string",
          "maxLength": 128,
          "description": "A committed refund without money movement has reason unknown_original or original_declined. A confirmed refusal of this refund has reason wallet_refused."
        },
        "balance": { "$ref": "#/$defs/Amount" },
        "status_url": { "type": "string", "format": "uri-reference" }
      },
      "if": { "properties": { "outcome": { "const": "declined" } } },
      "then": { "required": ["outcome_reason"] }
    }
  }
}
