{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://kiwerb.de/forschung/value-sharing-agreement.schema.json",
  "title": "KIWERB OpenLab possible value-sharing pre-work agreement",
  "description": "Structural schema for a future separately funded project. It is not an adopted contract, payment promise, legal or tax clearance. Cross-field timing, amendment and hash rules still require the published local validator and manual review.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version",
    "project_id",
    "currency",
    "signed_before_work",
    "agreement_effective_at",
    "compensated_work_not_before",
    "amendment",
    "pool_bps",
    "rounding_rule",
    "participants",
    "permitted_deductions",
    "accounting_frequency",
    "objection_days",
    "tax_handling",
    "payment_method",
    "zero_revenue_rule",
    "mandatory_claims_preserved",
    "patent_rights_unaffected"
  ],
  "properties": {
    "schema_version": {
      "const": "openlab-value-sharing-v1"
    },
    "project_id": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9-]{1,63}$"
    },
    "currency": {
      "const": "EUR"
    },
    "signed_before_work": {
      "const": true
    },
    "agreement_effective_at": {
      "type": "string",
      "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$"
    },
    "compensated_work_not_before": {
      "type": "string",
      "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$"
    },
    "amendment": {
      "oneOf": [
        {
          "type": "null"
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "supersedes_agreement_sha256",
            "amendment_reason",
            "new_terms_apply_not_before",
            "prior_work_remains_under_superseded_agreement"
          ],
          "properties": {
            "supersedes_agreement_sha256": {
              "type": "string",
              "pattern": "^[0-9a-f]{64}$"
            },
            "amendment_reason": {
              "type": "string",
              "minLength": 10,
              "maxLength": 500
            },
            "new_terms_apply_not_before": {
              "type": "string",
              "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$"
            },
            "prior_work_remains_under_superseded_agreement": {
              "const": true
            }
          }
        }
      ]
    },
    "pool_bps": {
      "type": "integer",
      "minimum": 0,
      "maximum": 10000
    },
    "rounding_rule": {
      "const": "largest_remainder_cents_participant_id_tiebreak"
    },
    "participants": {
      "type": "array",
      "minItems": 1,
      "maxItems": 100,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "participant_id",
          "eligible_contribution_scope",
          "weight_basis",
          "weight",
          "signed",
          "signed_at"
        ],
        "properties": {
          "participant_id": {
            "type": "string",
            "pattern": "^[0-9a-f]{32}$"
          },
          "eligible_contribution_scope": {
            "type": "string",
            "minLength": 10,
            "maxLength": 500
          },
          "weight_basis": {
            "type": "string",
            "minLength": 10,
            "maxLength": 500
          },
          "weight": {
            "type": "integer",
            "minimum": 1,
            "maximum": 1000000
          },
          "signed": {
            "const": true
          },
          "signed_at": {
            "type": "string",
            "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$"
          }
        }
      }
    },
    "permitted_deductions": {
      "type": "array",
      "maxItems": 30,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "code",
          "description",
          "cap_cents"
        ],
        "properties": {
          "code": {
            "type": "string",
            "pattern": "^[a-z0-9][a-z0-9-]{1,39}$"
          },
          "description": {
            "type": "string",
            "minLength": 5,
            "maxLength": 300
          },
          "cap_cents": {
            "type": "integer",
            "minimum": 0,
            "maximum": 1000000000000
          }
        }
      }
    },
    "accounting_frequency": {
      "type": "string",
      "minLength": 3,
      "maxLength": 100
    },
    "objection_days": {
      "type": "integer",
      "minimum": 7,
      "maximum": 365
    },
    "tax_handling": {
      "type": "string",
      "minLength": 10,
      "maxLength": 500
    },
    "payment_method": {
      "type": "string",
      "minLength": 5,
      "maxLength": 300
    },
    "zero_revenue_rule": {
      "type": "string",
      "minLength": 10,
      "maxLength": 300
    },
    "mandatory_claims_preserved": {
      "const": true
    },
    "patent_rights_unaffected": {
      "const": true
    }
  },
  "x-openlab-cross-field-rules": [
    "Every participant signed_at must be no later than agreement_effective_at.",
    "compensated_work_not_before must be no earlier than agreement_effective_at.",
    "Participant IDs and deduction codes must be unique.",
    "An amendment must bind the exact superseded agreement hash, preserve project and currency identity, become effective later, preserve earlier work under the old agreement, and never move the work boundary backward."
  ],
  "x-openlab-boundaries": {
    "adopted_contract": false,
    "payment_promised": false,
    "automatic_payment": false,
    "legal_or_tax_clearance": false
  }
}
