{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://commonworld.net/contracts/commonworld/proposal.schema.json",
  "title": "Commonworld Commons Proposal v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version",
    "kind",
    "proposal_id",
    "submitted_at",
    "status",
    "project",
    "consent"
  ],
  "properties": {
    "schema_version": {
      "const": 1
    },
    "kind": {
      "const": "commonworld_commons_proposal"
    },
    "proposal_id": {
      "type": "string",
      "pattern": "^cw-[0-9]{8}t[0-9]{6}z-[a-z0-9-]{3,48}$"
    },
    "submitted_at": {
      "type": "string",
      "format": "date-time"
    },
    "status": {
      "const": "submitted"
    },
    "project": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "name",
        "description",
        "official_website",
        "commons_type",
        "presence_geographic",
        "presence_digital",
        "actions",
        "sources",
        "sensitive_location_risk"
      ],
      "properties": {
        "name": {
          "type": "string",
          "minLength": 2,
          "maxLength": 140
        },
        "description": {
          "type": "string",
          "minLength": 40,
          "maxLength": 800
        },
        "official_website": {
          "type": "string",
          "format": "uri",
          "maxLength": 300
        },
        "commons_type": {
          "type": "string",
          "enum": [
            "knowledge",
            "software",
            "culture",
            "food-seeds",
            "water",
            "energy",
            "housing-land",
            "health-care",
            "tools-repair",
            "community-network",
            "other"
          ]
        },
        "presence_geographic": {
          "type": "boolean"
        },
        "presence_digital": {
          "type": "boolean"
        },
        "region": {
          "type": "string",
          "minLength": 2,
          "maxLength": 120
        },
        "actions": {
          "type": "array",
          "minItems": 1,
          "maxItems": 3,
          "uniqueItems": true,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "type",
              "url"
            ],
            "properties": {
              "type": {
                "type": "string",
                "enum": [
                  "visit",
                  "use",
                  "borrow",
                  "learn",
                  "contribute",
                  "volunteer",
                  "donate",
                  "contact",
                  "replicate"
                ]
              },
              "url": {
                "type": "string",
                "format": "uri",
                "maxLength": 300
              }
            }
          }
        },
        "sources": {
          "type": "array",
          "minItems": 1,
          "maxItems": 5,
          "uniqueItems": true,
          "items": {
            "type": "string",
            "format": "uri",
            "maxLength": 300
          }
        },
        "sensitive_location_risk": {
          "type": "boolean"
        },
        "location_precision": {
          "const": "country_or_region_only"
        },
        "editorial_note": {
          "type": "string",
          "maxLength": 500
        }
      },
      "allOf": [
        {
          "anyOf": [
            {
              "properties": {
                "presence_geographic": {
                  "const": true
                }
              }
            },
            {
              "properties": {
                "presence_digital": {
                  "const": true
                }
              }
            }
          ]
        },
        {
          "if": {
            "properties": {
              "presence_geographic": {
                "const": true
              }
            },
            "required": [
              "presence_geographic"
            ]
          },
          "then": {
            "required": [
              "region",
              "location_precision"
            ]
          },
          "else": {
            "not": {
              "anyOf": [
                {
                  "required": [
                    "region"
                  ]
                },
                {
                  "required": [
                    "location_precision"
                  ]
                }
              ]
            }
          }
        }
      ]
    },
    "consent": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "public_issue_acknowledged",
        "processing_agreed",
        "no_sensitive_data_confirmed"
      ],
      "properties": {
        "public_issue_acknowledged": {
          "const": true
        },
        "processing_agreed": {
          "const": true
        },
        "no_sensitive_data_confirmed": {
          "const": true
        }
      }
    },
    "commons_basis_draft": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "status",
        "dimensions"
      ],
      "properties": {
        "status": {
          "const": "needs_review"
        },
        "dimensions": {
          "type": "object",
          "additionalProperties": false,
          "minProperties": 1,
          "maxProperties": 5,
          "properties": {
            "shared_good": {
              "$ref": "#/$defs/proposal_commons_dimension"
            },
            "community": {
              "$ref": "#/$defs/proposal_commons_dimension"
            },
            "rules_and_governance": {
              "$ref": "#/$defs/proposal_commons_dimension"
            },
            "stewardship": {
              "$ref": "#/$defs/proposal_commons_dimension"
            },
            "legitimacy": {
              "$ref": "#/$defs/proposal_commons_dimension"
            }
          }
        }
      }
    }
  },
  "$defs": {
    "proposal_commons_dimension": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "classification",
        "text",
        "refs"
      ],
      "properties": {
        "classification": {
          "type": "string",
          "enum": [
            "confirmed",
            "open",
            "not_applicable"
          ]
        },
        "text": {
          "type": "string",
          "maxLength": 800
        },
        "refs": {
          "type": "array",
          "maxItems": 5,
          "uniqueItems": true,
          "items": {
            "type": "string",
            "pattern": "^source-[1-5]$"
          }
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "classification": {
                "const": "confirmed"
              }
            },
            "required": [
              "classification"
            ]
          },
          "then": {
            "properties": {
              "text": {
                "minLength": 20
              },
              "refs": {
                "minItems": 1
              }
            }
          }
        }
      ]
    }
  }
}
