{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$comment": "Copyright \u00a9 2026 Stephen Makonin. Draft schema.",
  "$id": "https://carbonkit.org/schemas/0.1/estimate.schema.json",
  "title": "CarbonKit estimate record - proposal 0.1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "spec_version",
    "id",
    "subject_ref",
    "provider_ref",
    "created_at",
    "boundary",
    "consent_ref",
    "supersedes",
    "record_type",
    "activity_ids",
    "kind",
    "amount",
    "factors",
    "calculation",
    "uncertainty",
    "limitations"
  ],
  "properties": {
    "spec_version": {
      "const": "0.1"
    },
    "id": {
      "type": "string",
      "minLength": 1
    },
    "subject_ref": {
      "type": "string",
      "minLength": 1
    },
    "provider_ref": {
      "type": "string",
      "minLength": 1
    },
    "created_at": {
      "type": "string",
      "format": "date-time"
    },
    "boundary": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "description",
        "geography"
      ],
      "properties": {
        "description": {
          "type": "string",
          "minLength": 1
        },
        "geography": {
          "type": "string",
          "minLength": 1
        }
      }
    },
    "consent_ref": {
      "type": "string",
      "minLength": 1
    },
    "supersedes": {
      "type": [
        "string",
        "null"
      ],
      "minLength": 1
    },
    "record_type": {
      "const": "estimate"
    },
    "activity_ids": {
      "type": "array",
      "minItems": 1,
      "uniqueItems": true,
      "items": {
        "type": "string",
        "minLength": 1
      }
    },
    "kind": {
      "enum": [
        "inventory",
        "baseline",
        "comparison"
      ]
    },
    "amount": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "value",
        "unit"
      ],
      "properties": {
        "value": {
          "type": [
            "number",
            "null"
          ]
        },
        "unit": {
          "const": "kgCO2e"
        }
      }
    },
    "factors": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "id",
          "version",
          "value",
          "unit",
          "geography",
          "valid_from",
          "valid_to",
          "source_uri",
          "artifact_sha256",
          "gas_coverage",
          "gwp_basis",
          "synthetic"
        ],
        "properties": {
          "id": {
            "type": "string",
            "minLength": 1
          },
          "version": {
            "type": "string",
            "minLength": 1
          },
          "value": {
            "type": "number",
            "minimum": 0
          },
          "unit": {
            "const": "kgCO2e/kWh"
          },
          "geography": {
            "type": "string",
            "minLength": 1
          },
          "valid_from": {
            "type": "string",
            "format": "date-time"
          },
          "valid_to": {
            "type": "string",
            "format": "date-time"
          },
          "source_uri": {
            "type": "string",
            "format": "uri"
          },
          "artifact_sha256": {
            "type": "string",
            "pattern": "^[a-f0-9]{64}$"
          },
          "gas_coverage": {
            "type": "string",
            "minLength": 1
          },
          "gwp_basis": {
            "type": "string",
            "minLength": 1
          },
          "synthetic": {
            "type": "boolean"
          }
        }
      }
    },
    "calculation": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "method_id",
        "version",
        "expression",
        "rounding",
        "input_sha256",
        "input_canonicalization",
        "input_scope"
      ],
      "properties": {
        "method_id": {
          "type": "string",
          "minLength": 1
        },
        "version": {
          "type": "string",
          "minLength": 1
        },
        "expression": {
          "type": "string",
          "minLength": 1
        },
        "rounding": {
          "type": "string",
          "minLength": 1
        },
        "input_canonicalization": {
          "type": "string",
          "minLength": 1
        },
        "input_scope": {
          "type": "string",
          "minLength": 1
        },
        "input_sha256": {
          "type": "string",
          "pattern": "^[a-f0-9]{64}$"
        }
      }
    },
    "uncertainty": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "status",
        "lower",
        "upper",
        "rationale"
      ],
      "properties": {
        "status": {
          "enum": [
            "unknown",
            "quantified"
          ]
        },
        "lower": {
          "type": [
            "number",
            "null"
          ]
        },
        "upper": {
          "type": [
            "number",
            "null"
          ]
        },
        "rationale": {
          "type": "string",
          "minLength": 1
        }
      }
    },
    "limitations": {
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 1
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "kind": {
            "const": "inventory"
          }
        }
      },
      "then": {
        "properties": {
          "amount": {
            "properties": {
              "value": {
                "minimum": 0
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "uncertainty": {
            "properties": {
              "status": {
                "const": "unknown"
              }
            }
          }
        }
      },
      "then": {
        "properties": {
          "uncertainty": {
            "properties": {
              "lower": {
                "type": "null"
              },
              "upper": {
                "type": "null"
              }
            }
          }
        }
      },
      "else": {
        "properties": {
          "uncertainty": {
            "properties": {
              "lower": {
                "type": "number"
              },
              "upper": {
                "type": "number"
              }
            }
          }
        }
      }
    }
  ]
}
