{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "required": ["id", "label", "type", "status", "relations"],
  "additionalProperties": false,
  "properties": {
    "id": { "type": "string", "pattern": "^(CUST-[0-9]{2}|(PRJ|AGR|AMD|DEL)-[0-9]{3})$" },
    "label": { "type": "string", "minLength": 1, "pattern": "\\S" },
    "type": { "enum": ["customer", "project", "agreement", "amendment", "delivery"] },
    "status": { "type": "string" },
    "planned_date": { "type": "string", "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}$" },
    "relations": {
      "type": "array",
      "items": {
        "type": "object", "required": ["type", "target"], "additionalProperties": false,
        "properties": {
          "type": { "enum": ["owns", "applies_to", "belongs_to", "depends_on", "amends", "affects"] },
          "target": { "type": "string", "pattern": "^(CUST-[0-9]{2}|(PRJ|AGR|AMD|DEL)-[0-9]{3})$" }
        }
      }
    }
  },
  "allOf": [
    { "if": { "properties": { "type": { "const": "customer" } } }, "then": { "properties": { "id": { "type": "string", "pattern": "^CUST-" }, "status": { "enum": ["active", "inactive"] } } } },
    { "if": { "properties": { "type": { "const": "project" } } }, "then": { "properties": { "id": { "type": "string", "pattern": "^PRJ-" }, "status": { "enum": ["planned", "active", "closed"] } } } },
    { "if": { "properties": { "type": { "const": "agreement" } } }, "then": { "properties": { "id": { "type": "string", "pattern": "^AGR-" }, "status": { "enum": ["draft", "effective", "expired"] } } } },
    { "if": { "properties": { "type": { "const": "amendment" } } }, "then": { "properties": { "id": { "type": "string", "pattern": "^AMD-" }, "status": { "enum": ["proposed", "effective", "withdrawn"] } } } },
    { "if": { "properties": { "type": { "const": "delivery" } } }, "then": { "properties": { "id": { "type": "string", "pattern": "^DEL-" }, "status": { "enum": ["not-recorded", "planned", "in-progress", "complete", "cancelled"] } } } },
    { "if": { "not": { "properties": { "type": { "const": "delivery" } } } }, "then": { "not": { "required": ["planned_date"] } } }
  ]
}
