{
  "ruleset_version": "1.0.0",
  "engine": {
    "language": "jsonlogic",
    "phases": ["compute_flags", "auto_defaults", "hard_validations", "auto_remove_blocks", "module_requirements", "warnings"]
  },
  "compute_flags": [
    { "id": "FLAG_IS_B2C",          "set": "computed_flags.IS_B2C",           "expr": { "==": [{ "var": "context.CUSTOMER.IS_CONSUMER" }, true] } },
    { "id": "FLAG_IS_B2B",          "set": "computed_flags.IS_B2B",           "expr": { "==": [{ "var": "context.CUSTOMER.IS_BUSINESS" }, true] } },
    { "id": "FLAG_SERVICE_IS_SAAS", "set": "computed_flags.SERVICE_IS_SAAS",  "expr": { "==": [{ "var": "context.SERVICE.MODEL" }, "SaaS"] } },
    { "id": "FLAG_SERVICE_IS_HYBRID","set": "computed_flags.SERVICE_IS_HYBRID","expr": { "==": [{ "var": "context.SERVICE.MODEL" }, "Hybrid"] } },
    { "id": "FLAG_HAS_PENALTY",     "set": "computed_flags.HAS_PENALTY",      "expr": { "!=": [{ "var": "context.NDA.PENALTY_AMOUNT_EUR" }, null] } },
    { "id": "FLAG_HAS_ANALYTICS",   "set": "computed_flags.HAS_ANALYTICS",    "expr": { "!=": [{ "var": "context.CONSENT.ANALYTICS_TOOLS" }, null] } },
    { "id": "FLAG_HAS_MARKETING",   "set": "computed_flags.HAS_MARKETING",    "expr": { "!=": [{ "var": "context.CONSENT.MARKETING_PARTNERS" }, null] } }
  ],
  "auto_defaults": [
    {
      "id": "DEFAULT_SECURITY_LOG_RETENTION",
      "when": { "==": [{ "var": "context.SECURITY.SECURITY_LOG_RETENTION_DAYS" }, 0] },
      "actions": [{ "type": "set", "path": "context.SECURITY.SECURITY_LOG_RETENTION_DAYS", "value": 30 }],
      "note": "Set SECURITY_LOG_RETENTION_DAYS=30 when 0 is supplied."
    },
    {
      "id": "DEFAULT_LOG_RETENTION",
      "when": { "==": [{ "var": "context.SECURITY.LOG_RETENTION_DAYS" }, 0] },
      "actions": [{ "type": "set", "path": "context.SECURITY.LOG_RETENTION_DAYS", "value": 7 }]
    }
  ],
  "hard_validations": [
    {
      "id": "DOC_LANG_MATCH_DE",
      "severity": "ERROR",
      "when": { "in": [{ "var": "doc_type" }, ["nda_de", "sla_de", "community_de", "copyright_de", "cloud_contract_de", "data_usage_clause_de", "cookie_banner_de", "agb_de"]] },
      "assert_all": [{ "==": [{ "var": "render.lang" }, "de"] }],
      "message": "doc_type requires render.lang=de."
    },
    {
      "id": "DOC_LANG_MATCH_EN",
      "severity": "ERROR",
      "when": { "in": [{ "var": "doc_type" }, ["nda_en", "acceptable_use_en", "liability_clause_en"]] },
      "assert_all": [{ "==": [{ "var": "render.lang" }, "en"] }],
      "message": "doc_type requires render.lang=en."
    },
    {
      "id": "SLA_AVAILABILITY_RANGE",
      "severity": "ERROR",
      "when": { "==": [{ "var": "doc_type" }, "sla_de"] },
      "assert_all": [
        { ">=": [{ "var": "context.SLA.AVAILABILITY_PERCENT" }, 90] },
        { "<=": [{ "var": "context.SLA.AVAILABILITY_PERCENT" }, 99.99] }
      ],
      "message": "SLA.AVAILABILITY_PERCENT must be between 90 and 99.99."
    },
    {
      "id": "CUSTOMER_ROLE_XOR",
      "severity": "ERROR",
      "when": { "==": [true, true] },
      "assert_all": [{
        "!=": [{ "var": "context.CUSTOMER.IS_CONSUMER" }, { "var": "context.CUSTOMER.IS_BUSINESS" }]
      }],
      "message": "Customer must be either consumer or business (exclusive)."
    },
    {
      "id": "PROVIDER_IDENTITY",
      "severity": "ERROR",
      "when": { "==": [true, true] },
      "assert_all": [
        { "!=": [{ "var": "context.PROVIDER.LEGAL_NAME" }, ""] },
        { "!=": [{ "var": "context.PROVIDER.EMAIL" }, ""] }
      ],
      "message": "PROVIDER.LEGAL_NAME and PROVIDER.EMAIL are required."
    }
  ],
  "auto_remove_blocks": [
    {
      "id": "REMOVE_NDA_PENALTY_BLOCK",
      "when": { "==": [{ "var": "computed_flags.HAS_PENALTY" }, false] },
      "actions": [{ "type": "remove_block", "block_id": "NDA_PENALTY_BLOCK" }]
    },
    {
      "id": "REMOVE_COOKIE_ANALYTICS_SECTION",
      "when": { "==": [{ "var": "computed_flags.HAS_ANALYTICS" }, false] },
      "actions": [{ "type": "remove_block", "block_id": "COOKIE_ANALYTICS_BLOCK" }]
    },
    {
      "id": "REMOVE_COOKIE_MARKETING_SECTION",
      "when": { "==": [{ "var": "computed_flags.HAS_MARKETING" }, false] },
      "actions": [{ "type": "remove_block", "block_id": "COOKIE_MARKETING_BLOCK" }]
    }
  ],
  "module_requirements": [
    {
      "id": "REQ_CLOUD_EXPORT_MODULE",
      "severity": "ERROR",
      "when": {
        "and": [
          { "==": [{ "var": "doc_type" }, "cloud_contract_de"] },
          { "or": [
            { "==": [{ "var": "computed_flags.SERVICE_IS_SAAS" }, true] },
            { "==": [{ "var": "computed_flags.SERVICE_IS_HYBRID" }, true] }
          ]}
        ]
      },
      "assert_all": [{ "in": ["CLOUD_EXPORT_DELETE_DE", { "var": "modules.enabled" }] }],
      "message": "Cloud SaaS/Hybrid requires module CLOUD_EXPORT_DELETE_DE."
    },
    {
      "id": "REQ_B2C_WITHDRAWAL_MODULE",
      "severity": "WARN",
      "when": {
        "and": [
          { "==": [{ "var": "doc_type" }, "agb_de"] },
          { "==": [{ "var": "computed_flags.IS_B2C" }, true] },
          { "==": [{ "var": "computed_flags.SERVICE_IS_SAAS" }, true] }
        ]
      },
      "assert_all": [{ "in": ["B2C_WITHDRAWAL_DE", { "var": "modules.enabled" }] }],
      "message": "B2C + SaaS AGB: Consider adding module B2C_WITHDRAWAL_DE (business-model dependent)."
    }
  ],
  "warnings": [
    {
      "id": "WARN_EXPORT_FORMATS_MISSING",
      "severity": "WARN",
      "when": {
        "and": [
          { "==": [{ "var": "doc_type" }, "cloud_contract_de"] },
          { "or": [
            { "==": [{ "var": "computed_flags.SERVICE_IS_SAAS" }, true] },
            { "==": [{ "var": "computed_flags.SERVICE_IS_HYBRID" }, true] }
          ]}
        ]
      },
      "assert_all": [{ ">=": [{ "var": "context.SERVICE.EXPORT_WINDOW_DAYS" }, 1] }],
      "message": "Consider setting SERVICE.EXPORT_FORMATS and EXPORT_WINDOW_DAYS for clearer exit handling."
    },
    {
      "id": "WARN_LEGAL_REVIEW",
      "severity": "WARN",
      "when": { "==": [true, true] },
      "assert_all": [],
      "message": "These templates are self-authored and MIT-licensed. For production use, legal review is strongly recommended."
    }
  ],
  "execution_contract": {
    "strict_placeholder_policy": {
      "mode": "error_on_missing",
      "missing_placeholder_error_code": "MISSING_PLACEHOLDER"
    }
  }
}
