{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:orbiplex:schema:ubc-allocation:v1",
  "title": "UBCAllocation v1",
  "description": "Machine-readable schema for Universal Basic Compute allocations, including guaranteed access modes and recognition through federation or FIP bridge paths.",
  "x-dia-basis": [
    "doc/normative/40-constitution/pl/CONSTITUTION.pl.md",
    "doc/normative/50-constitutional-ops/pl/UNIVERSAL-BASIC-COMPUTE.pl.md",
    "doc/normative/50-constitutional-ops/pl/UBC-LIMIT-PROFILES.pl.md"
  ],
  "type": "object",
  "additionalProperties": true,
  "required": [
    "schema/v",
    "allocation_id",
    "subject_ref",
    "federation_id",
    "attestation_ref",
    "recognition_source",
    "recognition_ref",
    "measurement_period",
    "valid_from",
    "compute_unit",
    "portability_scope",
    "guaranteed_modes",
    "funding_policy_ref",
    "limit_policy_ref",
    "created_at"
  ],
  "properties": {
    "schema/v": {
      "const": 1,
      "description": "Schema version."
    },
    "allocation_id": {
      "type": "string",
      "minLength": 1,
      "description": "Stable identifier of this allocation record."
    },
    "subject_ref": {
      "type": "string",
      "minLength": 1,
      "description": "Stable anonymous handle of the person receiving the compute floor."
    },
    "federation_id": {
      "type": "string",
      "minLength": 1
    },
    "attestation_ref": {
      "type": "string",
      "minLength": 1,
      "description": "Reference to the Proof-of-Personhood attestation that justifies the allocation."
    },
    "recognition_source": {
      "type": "string",
      "enum": [
        "federation_local",
        "federation_cross_recognition",
        "fip_bridge"
      ],
      "description": "Which recognition path made this allocation valid in the current federation."
    },
    "recognition_ref": {
      "type": "string",
      "minLength": 1,
      "description": "Reference to the recognizing federation or the FIP bridge."
    },
    "measurement_period": {
      "type": "string",
      "minLength": 1,
      "description": "Named accounting window used to measure and settle the allocation."
    },
    "valid_from": {
      "type": "string",
      "format": "date-time"
    },
    "valid_until": {
      "type": "string",
      "format": "date-time"
    },
    "compute_unit": {
      "type": "string",
      "minLength": 1,
      "description": "Declared unit of account for the allocated compute floor."
    },
    "portability_scope": {
      "type": "string",
      "enum": [
        "local",
        "trans_federation_limited",
        "trans_federation_extended"
      ],
      "description": "How far this concrete allocation may travel beyond the issuing federation."
    },
    "guaranteed_modes": {
      "$ref": "#/$defs/guaranteedModes",
      "description": "Guaranteed access contract for emergency, communication, and care paths.",
      "x-dia-basis": [
        "doc/normative/50-constitutional-ops/pl/UNIVERSAL-BASIC-COMPUTE.pl.md",
        "doc/normative/50-constitutional-ops/pl/UBC-LIMIT-PROFILES.pl.md"
      ]
    },
    "funding_policy_ref": {
      "type": "string",
      "minLength": 1,
      "description": "Reference to the policy defining who funds the allocation floor."
    },
    "limit_policy_ref": {
      "type": "string",
      "minLength": 1,
      "description": "Reference to the profile that defines communication and care limits."
    },
    "policy_annotations": {
      "type": "object",
      "additionalProperties": true
    },
    "created_at": {
      "type": "string",
      "format": "date-time"
    }
  },
  "$defs": {
    "modeAccess": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "access",
        "limit_profile"
      ],
      "properties": {
        "access": {
          "type": "boolean",
          "description": "Whether the given mode is guaranteed to be available."
        },
        "limit_profile": {
          "type": "string",
          "enum": [
            "unlimited",
            "limited",
            "extended"
          ],
          "description": "Strength of the limit profile for the given mode."
        },
        "limit_ref": {
          "type": "string",
          "minLength": 1,
          "description": "Reference to the concrete quantitative profile when the mode is not unlimited."
        }
      }
    },
    "guaranteedModes": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "emergency",
        "communication",
        "care"
      ],
      "properties": {
        "emergency": {
          "$ref": "#/$defs/modeAccess"
        },
        "communication": {
          "$ref": "#/$defs/modeAccess"
        },
        "care": {
          "$ref": "#/$defs/modeAccess"
        }
      }
    }
  },
  "allOf": [
    {
      "properties": {
        "guaranteed_modes": {
          "properties": {
            "emergency": {
              "properties": {
                "access": {
                  "const": true
                },
                "limit_profile": {
                  "const": "unlimited"
                }
              }
            },
            "communication": {
              "properties": {
                "access": {
                  "const": true
                }
              }
            },
            "care": {
              "properties": {
                "access": {
                  "const": true
                }
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "recognition_source": {
            "const": "fip_bridge"
          }
        },
        "required": [
          "recognition_source"
        ]
      },
      "then": {
        "properties": {
          "portability_scope": {
            "const": "trans_federation_limited"
          },
          "guaranteed_modes": {
            "properties": {
              "communication": {
                "properties": {
                  "limit_profile": {
                    "const": "limited"
                  }
                },
                "required": [
                  "limit_ref"
                ]
              },
              "care": {
                "properties": {
                  "limit_profile": {
                    "const": "limited"
                  }
                },
                "required": [
                  "limit_ref"
                ]
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "portability_scope": {
            "const": "trans_federation_extended"
          }
        },
        "required": [
          "portability_scope"
        ]
      },
      "then": {
        "properties": {
          "guaranteed_modes": {
            "properties": {
              "communication": {
                "required": [
                  "limit_ref"
                ]
              },
              "care": {
                "required": [
                  "limit_ref"
                ]
              }
            }
          }
        }
      }
    }
  ]
}
