{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://konstruktiva.se/schemas/party-manifest-1.0.schema.json",
  "title": "Konstruktiva Party Manifest",
  "description": "Portable provenance-first manifest for a user-created political fork. The format records relationships to Konstruktiva without implying endorsement, ranking or recommendation.",
  "type": "object",
  "additionalProperties": false,
  "required": ["formatVersion", "party"],
  "properties": {
    "formatVersion": {
      "const": "1.0"
    },
    "party": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "jurisdiction"],
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1,
          "maxLength": 160
        },
        "jurisdiction": {
          "type": "object",
          "additionalProperties": false,
          "required": ["country", "level"],
          "properties": {
            "country": {
              "type": "string",
              "minLength": 2,
              "maxLength": 80
            },
            "level": {
              "enum": ["national", "regional", "municipal", "other"]
            },
            "area": {
              "type": "string",
              "minLength": 1,
              "maxLength": 160
            }
          }
        },
        "language": {
          "type": "string",
          "minLength": 2,
          "maxLength": 20,
          "default": "sv"
        }
      }
    },
    "upstream": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "project": {
          "const": "Konstruktiva",
          "default": "Konstruktiva"
        },
        "currentPath": {
          "const": "CURRENT.md",
          "default": "CURRENT.md"
        }
      },
      "default": {
        "project": "Konstruktiva",
        "currentPath": "CURRENT.md"
      }
    },
    "principles": {
      "type": "array",
      "items": {
        "allOf": [
          {
            "$ref": "#/$defs/blockCore"
          }
        ],
        "unevaluatedProperties": false
      },
      "default": []
    },
    "policies": {
      "type": "array",
      "items": {
        "allOf": [
          {
            "$ref": "#/$defs/blockCore"
          },
          {
            "type": "object",
            "required": ["area"],
            "properties": {
              "area": {
                "type": "string",
                "minLength": 1,
                "maxLength": 120
              }
            }
          }
        ],
        "unevaluatedProperties": false
      },
      "default": []
    },
    "openQuestions": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/openQuestion"
      },
      "default": []
    }
  },
  "$defs": {
    "id": {
      "type": "string",
      "minLength": 1,
      "maxLength": 80,
      "pattern": "^[a-z0-9]+(?:-[a-z0-9]+)*$"
    },
    "source": {
      "type": "object",
      "additionalProperties": false,
      "required": ["path", "heading"],
      "properties": {
        "path": {
          "type": "string",
          "minLength": 1
        },
        "heading": {
          "type": "string",
          "minLength": 1
        }
      }
    },
    "blockCore": {
      "type": "object",
      "required": ["id", "title", "relationship"],
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "title": {
          "type": "string",
          "minLength": 1,
          "maxLength": 160
        },
        "relationship": {
          "enum": ["inherit", "adapt", "exclude", "original"]
        },
        "source": {
          "$ref": "#/$defs/source"
        },
        "text": {
          "type": "string",
          "minLength": 1
        },
        "note": {
          "type": "string",
          "minLength": 1
        }
      },
      "oneOf": [
        {
          "properties": {
            "relationship": {
              "const": "inherit"
            }
          },
          "required": ["source"],
          "not": {
            "required": ["text"]
          }
        },
        {
          "properties": {
            "relationship": {
              "const": "adapt"
            }
          },
          "required": ["source", "text"]
        },
        {
          "properties": {
            "relationship": {
              "const": "exclude"
            }
          },
          "required": ["source"],
          "not": {
            "required": ["text"]
          }
        },
        {
          "properties": {
            "relationship": {
              "const": "original"
            }
          },
          "required": ["text"],
          "not": {
            "required": ["source"]
          }
        }
      ]
    },
    "openQuestion": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "question"],
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "question": {
          "type": "string",
          "minLength": 1
        },
        "status": {
          "enum": ["open", "decided"],
          "default": "open"
        },
        "decision": {
          "type": "string",
          "minLength": 1
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "status": {
                "const": "decided"
              }
            },
            "required": ["status"]
          },
          "then": {
            "required": ["decision"]
          }
        },
        {
          "if": {
            "properties": {
              "status": {
                "const": "open"
              }
            }
          },
          "then": {
            "not": {
              "required": ["decision"]
            }
          }
        }
      ]
    }
  }
}
