{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://breakpilot.example.com/template-spec.schema.json",
  "title": "TemplateSpec v1 (STRICT)",
  "description": "Input contract for the BreakPilot Compliance Document Generator. Validates the structured context before rendering any legal template.",
  "type": "object",
  "additionalProperties": false,
  "required": ["spec_version", "doc_type", "render", "context", "modules"],
  "properties": {
    "spec_version": {
      "type": "string",
      "pattern": "^[0-9]+\\.[0-9]+\\.[0-9]+$"
    },
    "doc_type": {
      "type": "string",
      "enum": [
        "nda_de",
        "nda_en",
        "sla_de",
        "acceptable_use_en",
        "community_de",
        "copyright_de",
        "cloud_contract_de",
        "data_usage_clause_de",
        "cookie_banner_de",
        "agb_de",
        "liability_clause_en"
      ]
    },
    "render": {
      "type": "object",
      "additionalProperties": false,
      "required": ["lang", "variant", "format", "strict"],
      "properties": {
        "lang": { "type": "string", "enum": ["de", "en"] },
        "variant": { "type": "string", "enum": ["standard", "b2b", "b2c"] },
        "format": { "type": "string", "enum": ["plaintext", "markdown", "html"] },
        "strict": { "type": "boolean", "const": true }
      }
    },
    "context": {
      "type": "object",
      "additionalProperties": false,
      "required": ["PROVIDER", "CUSTOMER", "SERVICE", "LEGAL", "PRIVACY", "SLA", "PAYMENTS", "SECURITY", "NDA", "CONSENT"],
      "properties": {
        "PROVIDER":  { "$ref": "#/$defs/Provider" },
        "CUSTOMER":  { "$ref": "#/$defs/Customer" },
        "SERVICE":   { "$ref": "#/$defs/Service" },
        "LEGAL":     { "$ref": "#/$defs/Legal" },
        "PRIVACY":   { "$ref": "#/$defs/Privacy" },
        "SLA":       { "$ref": "#/$defs/SLA" },
        "PAYMENTS":  { "$ref": "#/$defs/Payments" },
        "SECURITY":  { "$ref": "#/$defs/Security" },
        "NDA":       { "$ref": "#/$defs/NDA" },
        "CONSENT":   { "$ref": "#/$defs/Consent" }
      }
    },
    "modules": {
      "type": "object",
      "additionalProperties": false,
      "required": ["enabled"],
      "properties": {
        "enabled": {
          "type": "array",
          "items": { "type": "string", "minLength": 1 },
          "uniqueItems": true
        }
      }
    },
    "computed_flags": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "IS_B2C":           { "type": "boolean" },
        "IS_B2B":           { "type": "boolean" },
        "SERVICE_IS_SAAS":  { "type": "boolean" },
        "SERVICE_IS_HYBRID":{ "type": "boolean" },
        "HAS_PENALTY":      { "type": "boolean" },
        "HAS_ANALYTICS":    { "type": "boolean" },
        "HAS_MARKETING":    { "type": "boolean" }
      }
    }
  },
  "allOf": [
    { "if": { "properties": { "doc_type": { "const": "nda_de" } }, "required": ["doc_type"] }, "then": { "properties": { "render": { "properties": { "lang": { "const": "de" } } } } } },
    { "if": { "properties": { "doc_type": { "const": "nda_en" } }, "required": ["doc_type"] }, "then": { "properties": { "render": { "properties": { "lang": { "const": "en" } } } } } },
    { "if": { "properties": { "doc_type": { "const": "sla_de" } }, "required": ["doc_type"] }, "then": { "properties": { "render": { "properties": { "lang": { "const": "de" } } } } } },
    { "if": { "properties": { "doc_type": { "const": "acceptable_use_en" } }, "required": ["doc_type"] }, "then": { "properties": { "render": { "properties": { "lang": { "const": "en" } } } } } },
    { "if": { "properties": { "doc_type": { "const": "community_de" } }, "required": ["doc_type"] }, "then": { "properties": { "render": { "properties": { "lang": { "const": "de" } } } } } },
    { "if": { "properties": { "doc_type": { "const": "copyright_de" } }, "required": ["doc_type"] }, "then": { "properties": { "render": { "properties": { "lang": { "const": "de" } } } } } },
    { "if": { "properties": { "doc_type": { "const": "cloud_contract_de" } }, "required": ["doc_type"] }, "then": { "properties": { "render": { "properties": { "lang": { "const": "de" } } } } } },
    { "if": { "properties": { "doc_type": { "const": "data_usage_clause_de" } }, "required": ["doc_type"] }, "then": { "properties": { "render": { "properties": { "lang": { "const": "de" } } } } } },
    { "if": { "properties": { "doc_type": { "const": "cookie_banner_de" } }, "required": ["doc_type"] }, "then": { "properties": { "render": { "properties": { "lang": { "const": "de" } } } } } },
    { "if": { "properties": { "doc_type": { "const": "agb_de" } }, "required": ["doc_type"] }, "then": { "properties": { "render": { "properties": { "lang": { "const": "de" } } } } } },
    { "if": { "properties": { "doc_type": { "const": "liability_clause_en" } }, "required": ["doc_type"] }, "then": { "properties": { "render": { "properties": { "lang": { "const": "en" } } } } } }
  ],
  "$defs": {
    "NonEmptyString": { "type": "string", "minLength": 1 },
    "Email": { "type": "string", "format": "email" },
    "Date": { "type": "string", "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}$" },
    "Provider": {
      "type": "object",
      "additionalProperties": false,
      "required": ["LEGAL_NAME", "ADDRESS_LINE", "POSTAL_CODE", "CITY", "COUNTRY", "EMAIL", "WEBSITE_URL"],
      "properties": {
        "LEGAL_NAME":       { "$ref": "#/$defs/NonEmptyString" },
        "LEGAL_FORM":       { "type": "string" },
        "ADDRESS_LINE":     { "$ref": "#/$defs/NonEmptyString" },
        "POSTAL_CODE":      { "$ref": "#/$defs/NonEmptyString" },
        "CITY":             { "$ref": "#/$defs/NonEmptyString" },
        "COUNTRY":          { "$ref": "#/$defs/NonEmptyString" },
        "EMAIL":            { "$ref": "#/$defs/Email" },
        "PHONE":            { "type": "string" },
        "WEBSITE_URL":      { "$ref": "#/$defs/NonEmptyString" },
        "CEO_NAME":         { "type": "string" },
        "REGISTER_COURT":   { "type": "string" },
        "REGISTER_NUMBER":  { "type": "string" },
        "VAT_ID":           { "type": "string" }
      }
    },
    "Customer": {
      "type": "object",
      "additionalProperties": false,
      "required": ["LEGAL_NAME", "EMAIL", "IS_CONSUMER", "IS_BUSINESS"],
      "properties": {
        "LEGAL_NAME":    { "$ref": "#/$defs/NonEmptyString" },
        "ADDRESS_LINE":  { "type": "string" },
        "POSTAL_CODE":   { "type": "string" },
        "CITY":          { "type": "string" },
        "COUNTRY":       { "type": "string" },
        "CONTACT_NAME":  { "type": "string" },
        "EMAIL":         { "$ref": "#/$defs/Email" },
        "IS_CONSUMER":   { "type": "boolean" },
        "IS_BUSINESS":   { "type": "boolean" }
      }
    },
    "Service": {
      "type": "object",
      "additionalProperties": false,
      "required": ["NAME", "DESCRIPTION", "MODEL", "TIER", "DATA_LOCATION"],
      "properties": {
        "NAME":              { "$ref": "#/$defs/NonEmptyString" },
        "DESCRIPTION":       { "$ref": "#/$defs/NonEmptyString" },
        "MODEL":             { "type": "string", "enum": ["SaaS", "PaaS", "IaaS", "OnPrem", "Hybrid"] },
        "TIER":              { "$ref": "#/$defs/NonEmptyString" },
        "DATA_LOCATION":     { "$ref": "#/$defs/NonEmptyString" },
        "EXPORT_FORMATS":    { "type": "array", "items": { "$ref": "#/$defs/NonEmptyString" }, "uniqueItems": true },
        "EXPORT_WINDOW_DAYS":{ "type": "integer", "minimum": 0 },
        "MIN_TERM_MONTHS":   { "type": "integer", "minimum": 1 },
        "TERMINATION_NOTICE_DAYS": { "type": "integer", "minimum": 0 }
      }
    },
    "Legal": {
      "type": "object",
      "additionalProperties": false,
      "required": ["GOVERNING_LAW", "JURISDICTION_CITY", "VERSION_DATE", "EFFECTIVE_DATE", "LANG"],
      "properties": {
        "GOVERNING_LAW":      { "$ref": "#/$defs/NonEmptyString" },
        "JURISDICTION_CITY":  { "$ref": "#/$defs/NonEmptyString" },
        "VERSION_DATE":       { "$ref": "#/$defs/Date" },
        "EFFECTIVE_DATE":     { "$ref": "#/$defs/Date" },
        "LANG":               { "type": "string", "enum": ["de", "en"] }
      }
    },
    "Privacy": {
      "type": "object",
      "additionalProperties": false,
      "required": ["CONTACT_EMAIL", "DPO_NAME", "DPO_EMAIL", "PRIVACY_POLICY_URL"],
      "properties": {
        "CONTACT_EMAIL":                 { "$ref": "#/$defs/Email" },
        "DPO_NAME":                      { "$ref": "#/$defs/NonEmptyString" },
        "DPO_EMAIL":                     { "$ref": "#/$defs/Email" },
        "SUPERVISORY_AUTHORITY_NAME":    { "type": "string" },
        "SUPERVISORY_AUTHORITY_ADDRESS": { "type": "string" },
        "PRIVACY_POLICY_URL":            { "$ref": "#/$defs/NonEmptyString" },
        "COOKIE_POLICY_URL":             { "type": "string" },
        "ANALYTICS_RETENTION_MONTHS":    { "type": "integer", "minimum": 1 },
        "DATA_TRANSFER_THIRD_COUNTRIES": { "type": "string" }
      }
    },
    "SLA": {
      "type": "object",
      "additionalProperties": false,
      "required": ["AVAILABILITY_PERCENT", "MAINTENANCE_NOTICE_HOURS", "SUPPORT_EMAIL", "SUPPORT_HOURS",
                   "RESPONSE_CRITICAL_H", "RESOLUTION_CRITICAL_H", "RESPONSE_HIGH_H", "RESOLUTION_HIGH_H",
                   "RESPONSE_MEDIUM_H", "RESOLUTION_MEDIUM_H", "RESPONSE_LOW_H"],
      "properties": {
        "AVAILABILITY_PERCENT":    { "type": "number", "minimum": 90, "maximum": 99.99 },
        "MAINTENANCE_NOTICE_HOURS":{ "type": "integer", "minimum": 0 },
        "SUPPORT_EMAIL":           { "$ref": "#/$defs/Email" },
        "SUPPORT_PHONE":           { "type": "string" },
        "SUPPORT_HOURS":           { "$ref": "#/$defs/NonEmptyString" },
        "RESPONSE_CRITICAL_H":     { "type": "integer", "minimum": 0 },
        "RESOLUTION_CRITICAL_H":   { "type": "integer", "minimum": 0 },
        "RESPONSE_HIGH_H":         { "type": "integer", "minimum": 0 },
        "RESOLUTION_HIGH_H":       { "type": "integer", "minimum": 0 },
        "RESPONSE_MEDIUM_H":       { "type": "integer", "minimum": 0 },
        "RESOLUTION_MEDIUM_H":     { "type": "integer", "minimum": 0 },
        "RESPONSE_LOW_H":          { "type": "integer", "minimum": 0 }
      }
    },
    "Payments": {
      "type": "object",
      "additionalProperties": false,
      "required": ["MONTHLY_FEE_EUR", "PAYMENT_DUE_DAY", "PAYMENT_METHOD", "PAYMENT_DAYS"],
      "properties": {
        "MONTHLY_FEE_EUR":  { "type": "number", "minimum": 0 },
        "PAYMENT_DUE_DAY":  { "type": "integer", "minimum": 1, "maximum": 31 },
        "PAYMENT_METHOD":   { "$ref": "#/$defs/NonEmptyString" },
        "PAYMENT_DAYS":     { "type": "integer", "minimum": 0 }
      }
    },
    "Security": {
      "type": "object",
      "additionalProperties": false,
      "required": ["INCIDENT_NOTICE_HOURS", "LOG_RETENTION_DAYS", "SECURITY_LOG_RETENTION_DAYS"],
      "properties": {
        "INCIDENT_NOTICE_HOURS":        { "type": "integer", "minimum": 0 },
        "LOG_RETENTION_DAYS":           { "type": "integer", "minimum": 0 },
        "SECURITY_LOG_RETENTION_DAYS":  { "type": "integer", "minimum": 0 }
      }
    },
    "NDA": {
      "type": "object",
      "additionalProperties": false,
      "required": ["PURPOSE", "DURATION_YEARS", "PENALTY_AMOUNT_EUR"],
      "properties": {
        "PURPOSE":              { "$ref": "#/$defs/NonEmptyString" },
        "DURATION_YEARS":       { "type": "integer", "minimum": 1, "maximum": 30 },
        "PENALTY_AMOUNT_EUR":   { "type": ["number", "null"], "minimum": 0 }
      }
    },
    "Consent": {
      "type": "object",
      "additionalProperties": false,
      "required": ["ANALYTICS_TOOLS", "MARKETING_PARTNERS", "WEBSITE_NAME"],
      "properties": {
        "WEBSITE_NAME":       { "$ref": "#/$defs/NonEmptyString" },
        "ANALYTICS_TOOLS":    { "type": ["string", "null"] },
        "MARKETING_PARTNERS": { "type": ["string", "null"] }
      }
    }
  }
}
