{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://torneo.ai/schemas/cost_receipt.v1",
  "title": "cost_receipt.v1",
  "description": "Per-cycle cost receipt (runs/<cycle>/COST.json) produced by torneo_platform.allocation.allocate and verified by tools/check_cost_receipt.py. Structure only: an amount is a non-negative decimal string or number, or UNKNOWN for allocated and total (never rendered as 0). The criterion (marginal exactly 0 USD, total = marginal + allocated at the frozen rate, no UNKNOWN) is checked by the tool, not by this schema.",
  "type": "object",
  "required": [
    "schema",
    "cycle_id",
    "marginal",
    "allocated",
    "fx",
    "total"
  ],
  "additionalProperties": false,
  "properties": {
    "schema": {
      "const": "cost_receipt.v1"
    },
    "cycle_id": {
      "type": "string",
      "minLength": 1
    },
    "marginal": {
      "type": "object",
      "required": [
        "amount",
        "currency"
      ],
      "additionalProperties": false,
      "properties": {
        "amount": {
          "oneOf": [
            {
              "type": "string",
              "pattern": "^[0-9]+(\\.[0-9]+)?$"
            },
            {
              "type": "number",
              "minimum": 0
            }
          ]
        },
        "currency": {
          "const": "USD"
        },
        "status": {
          "const": "unknown"
        }
      },
      "allOf": [
        {
          "if": {
            "required": [
              "status"
            ]
          },
          "then": {
            "properties": {
              "amount": {
                "const": "UNKNOWN"
              }
            }
          }
        }
      ]
    },
    "allocated": {
      "type": "object",
      "required": [
        "amount",
        "currency"
      ],
      "additionalProperties": false,
      "properties": {
        "amount": {
          "anyOf": [
            {
              "type": "string",
              "pattern": "^[0-9]+(\\.[0-9]+)?$"
            },
            {
              "type": "number",
              "minimum": 0
            },
            {
              "const": "UNKNOWN"
            }
          ]
        },
        "currency": {
          "enum": [
            "USD",
            "CHF"
          ]
        },
        "status": {
          "const": "unknown"
        }
      },
      "allOf": [
        {
          "if": {
            "required": [
              "status"
            ]
          },
          "then": {
            "properties": {
              "amount": {
                "const": "UNKNOWN"
              }
            }
          }
        }
      ]
    },
    "fx": {
      "type": "object",
      "required": [
        "from",
        "to",
        "rate",
        "date",
        "source",
        "frozen"
      ],
      "additionalProperties": false,
      "properties": {
        "from": {
          "const": "USD"
        },
        "to": {
          "const": "CHF"
        },
        "rate": {
          "oneOf": [
            {
              "type": "string",
              "pattern": "^(?!0+(\\.0+)?$)[0-9]+(\\.[0-9]+)?$"
            },
            {
              "type": "number",
              "exclusiveMinimum": 0
            }
          ]
        },
        "date": {
          "type": "string",
          "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
        },
        "source": {
          "type": "string",
          "pattern": "\\S"
        },
        "frozen": {
          "const": true
        }
      }
    },
    "total": {
      "type": "object",
      "required": [
        "amount",
        "currency"
      ],
      "additionalProperties": false,
      "properties": {
        "amount": {
          "anyOf": [
            {
              "type": "string",
              "pattern": "^[0-9]+(\\.[0-9]+)?$"
            },
            {
              "type": "number",
              "minimum": 0
            },
            {
              "const": "UNKNOWN"
            }
          ]
        },
        "currency": {
          "const": "CHF"
        },
        "status": {
          "const": "unknown"
        }
      },
      "allOf": [
        {
          "if": {
            "required": [
              "status"
            ]
          },
          "then": {
            "properties": {
              "amount": {
                "const": "UNKNOWN"
              }
            }
          }
        }
      ]
    },
    "ceiling": {
      "type": "object",
      "required": [
        "amount",
        "currency",
        "rule",
        "within"
      ],
      "additionalProperties": false,
      "properties": {
        "amount": {
          "type": "string",
          "pattern": "^[0-9]+(\\.[0-9]+)?$"
        },
        "currency": {
          "const": "CHF"
        },
        "rule": {
          "const": "total <= ceiling"
        },
        "within": {
          "type": [
            "boolean",
            "null"
          ]
        }
      }
    },
    "method": {
      "type": "object",
      "required": [
        "id",
        "rule",
        "period_days"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "type": "string",
          "minLength": 1
        },
        "rule": {
          "type": "string",
          "minLength": 1
        },
        "period_days": {
          "type": "integer",
          "minimum": 1
        }
      }
    },
    "lines": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "required": [
          "id",
          "currency",
          "invoice_ref",
          "monthly_price",
          "hours",
          "share",
          "chf",
          "status"
        ],
        "additionalProperties": false,
        "properties": {
          "id": {
            "type": "string",
            "minLength": 1
          },
          "currency": {
            "enum": [
              "USD",
              "CHF"
            ]
          },
          "invoice_ref": {
            "type": [
              "string",
              "null"
            ]
          },
          "monthly_price": {
            "anyOf": [
              {
                "type": "string",
                "pattern": "^[0-9]+(\\.[0-9]+)?$"
              },
              {
                "type": "number",
                "minimum": 0
              },
              {
                "const": "UNKNOWN"
              }
            ]
          },
          "hours": {
            "anyOf": [
              {
                "type": "string",
                "pattern": "^[0-9]+(\\.[0-9]+)?$"
              },
              {
                "type": "number",
                "minimum": 0
              },
              {
                "const": "UNKNOWN"
              }
            ]
          },
          "share": {
            "anyOf": [
              {
                "type": "string",
                "pattern": "^[0-9]+(\\.[0-9]+)?$"
              },
              {
                "type": "number",
                "minimum": 0
              },
              {
                "const": "UNKNOWN"
              }
            ]
          },
          "chf": {
            "anyOf": [
              {
                "type": "string",
                "pattern": "^[0-9]+(\\.[0-9]+)?$"
              },
              {
                "type": "number",
                "minimum": 0
              },
              {
                "const": "UNKNOWN"
              }
            ]
          },
          "status": {
            "enum": [
              "known",
              "unknown"
            ]
          }
        }
      }
    },
    "reconciliation": {
      "type": "object",
      "required": [
        "status"
      ],
      "additionalProperties": false,
      "properties": {
        "status": {
          "enum": [
            "reconciled",
            "provisional"
          ]
        }
      }
    }
  }
}
