{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://local.anonymisation/admin-rules.schema.json",
  "title": "Admin rules configuration",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "version",
    "rules"
  ],
  "properties": {
    "version": {
      "type": "integer",
      "minimum": 1
    },
    "defaults": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "review_required_for_activation": {
          "type": "boolean"
        },
        "environments": {
          "type": "array",
          "items": {
            "type": "string",
            "enum": [
              "test",
              "staging",
              "prod"
            ]
          },
          "uniqueItems": true
        },
        "sections": {
          "type": "array",
          "items": {
            "type": "string",
            "enum": [
              "narrative",
              "structured",
              "table",
              "header",
              "footer"
            ]
          },
          "uniqueItems": true
        }
      }
    },
    "rules": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/rule"
      }
    }
  },
  "$defs": {
    "scope": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "document_families",
        "environments",
        "sections"
      ],
      "properties": {
        "document_families": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          },
          "minItems": 1,
          "uniqueItems": true
        },
        "environments": {
          "type": "array",
          "items": {
            "type": "string",
            "enum": [
              "test",
              "staging",
              "prod"
            ]
          },
          "minItems": 1,
          "uniqueItems": true
        },
        "sections": {
          "type": "array",
          "items": {
            "type": "string",
            "enum": [
              "narrative",
              "structured",
              "table",
              "header",
              "footer"
            ]
          },
          "minItems": 1,
          "uniqueItems": true
        }
      }
    },
    "tests": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "required_case_ids"
      ],
      "properties": {
        "required_case_ids": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          },
          "minItems": 1,
          "uniqueItems": true
        }
      }
    },
    "governance": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "owner",
        "justification",
        "created_at",
        "review_required_for_activation",
        "tests"
      ],
      "properties": {
        "owner": {
          "type": "string",
          "minLength": 2
        },
        "justification": {
          "type": "string",
          "minLength": 8
        },
        "created_at": {
          "type": "string",
          "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
        },
        "review_required_for_activation": {
          "type": "boolean"
        },
        "approved_by": {
          "type": [
            "string",
            "null"
          ]
        },
        "ticket": {
          "type": "string"
        },
        "tests": {
          "$ref": "#/$defs/tests"
        }
      }
    },
    "normalization": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "case_insensitive": {
          "type": "boolean"
        },
        "whole_word": {
          "type": "boolean"
        },
        "multiline": {
          "type": "boolean"
        },
        "allow_bare_value": {
          "type": "boolean"
        },
        "accepted_prefixes": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          },
          "uniqueItems": true
        },
        "prefix_value_separators": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "uniqueItems": true
        }
      }
    },
    "match": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "exact_value": {
          "type": "string",
          "minLength": 1
        },
        "canonical_value": {
          "type": "string",
          "minLength": 1
        },
        "context_prefixes": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          },
          "uniqueItems": true
        },
        "context_separators": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "uniqueItems": true
        }
      }
    },
    "rule": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "label",
        "type",
        "action",
        "status",
        "match",
        "scope",
        "governance"
      ],
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[a-z0-9_][a-z0-9_-]{2,63}$"
        },
        "label": {
          "type": "string",
          "minLength": 3
        },
        "description": {
          "type": "string"
        },
        "type": {
          "type": "string",
          "enum": [
            "exact_term",
            "normalized_identifier",
            "contextual_identifier",
            "preserve_phrase"
          ]
        },
        "action": {
          "type": "string",
          "enum": [
            "mask",
            "preserve"
          ]
        },
        "placeholder": {
          "type": "string",
          "pattern": "^\\[[A-Z_]+\\]$"
        },
        "status": {
          "type": "string",
          "enum": [
            "draft",
            "candidate",
            "approved",
            "active",
            "disabled",
            "retired"
          ]
        },
        "match": {
          "$ref": "#/$defs/match"
        },
        "normalization": {
          "$ref": "#/$defs/normalization"
        },
        "scope": {
          "$ref": "#/$defs/scope"
        },
        "governance": {
          "$ref": "#/$defs/governance"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "action": {
                "const": "mask"
              }
            }
          },
          "then": {
            "required": [
              "placeholder"
            ]
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "exact_term"
              }
            }
          },
          "then": {
            "properties": {
              "match": {
                "required": [
                  "exact_value"
                ]
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "preserve_phrase"
              }
            }
          },
          "then": {
            "properties": {
              "action": {
                "const": "preserve"
              },
              "match": {
                "required": [
                  "exact_value"
                ]
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "normalized_identifier"
              }
            }
          },
          "then": {
            "properties": {
              "match": {
                "required": [
                  "canonical_value"
                ]
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "contextual_identifier"
              }
            }
          },
          "then": {
            "properties": {
              "match": {
                "required": [
                  "canonical_value",
                  "context_prefixes"
                ]
              }
            }
          }
        }
      ]
    }
  }
}
