{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Coverage Policy Schema",
  "type": "object",
  "required": [
    "version",
    "jurisdiction",
    "tax_year",
    "tax_year_boundary",
    "defaults",
    "document_kinds",
    "triggers",
    "schedules",
    "status_classifier",
    "conflict_resolution",
    "question_templates"
  ],
  "properties": {
    "version": {
      "type": "string",
      "pattern": "^\\d+\\.\\d+$"
    },
    "jurisdiction": {
      "type": "string",
      "enum": ["UK", "US", "CA", "AU"]
    },
    "tax_year": {
      "type": "string",
      "pattern": "^\\d{4}-\\d{2}$"
    },
    "tax_year_boundary": {
      "type": "object",
      "required": ["start", "end"],
      "properties": {
        "start": {
          "type": "string",
          "format": "date"
        },
        "end": {
          "type": "string",
          "format": "date"
        }
      }
    },
    "defaults": {
      "type": "object",
      "required": ["confidence_thresholds"],
      "properties": {
        "confidence_thresholds": {
          "type": "object",
          "properties": {
            "ocr": {
              "type": "number",
              "minimum": 0,
              "maximum": 1
            },
            "extract": {
              "type": "number",
              "minimum": 0,
              "maximum": 1
            }
          }
        },
        "date_tolerance_days": {
          "type": "integer",
          "minimum": 0
        },
        "require_lineage_bbox": {
          "type": "boolean"
        },
        "allow_bank_substantiation": {
          "type": "boolean"
        }
      }
    },
    "document_kinds": {
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 1
      },
      "minItems": 1,
      "uniqueItems": true
    },
    "guidance_refs": {
      "type": "object",
      "patternProperties": {
        "^[A-Z0-9_]+$": {
          "type": "object",
          "required": ["doc_id", "kind"],
          "properties": {
            "doc_id": {
              "type": "string",
              "minLength": 1
            },
            "kind": {
              "type": "string",
              "minLength": 1
            }
          }
        }
      }
    },
    "triggers": {
      "type": "object",
      "patternProperties": {
        "^SA\\d+[A-Z]*$": {
          "type": "object",
          "properties": {
            "any_of": {
              "type": "array",
              "items": {
                "type": "string",
                "minLength": 1
              }
            },
            "all_of": {
              "type": "array",
              "items": {
                "type": "string",
                "minLength": 1
              }
            }
          },
          "anyOf": [{ "required": ["any_of"] }, { "required": ["all_of"] }]
        }
      }
    },
    "schedules": {
      "type": "object",
      "patternProperties": {
        "^SA\\d+[A-Z]*$": {
          "type": "object",
          "properties": {
            "guidance_hint": {
              "type": "string"
            },
            "evidence": {
              "type": "array",
              "items": {
                "type": "object",
                "required": ["id", "role"],
                "properties": {
                  "id": {
                    "type": "string",
                    "minLength": 1
                  },
                  "role": {
                    "type": "string",
                    "enum": ["REQUIRED", "CONDITIONALLY_REQUIRED", "OPTIONAL"]
                  },
                  "condition": {
                    "type": "string"
                  },
                  "boxes": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "pattern": "^SA\\d+[A-Z]*_b\\d+(_\\d+)?$"
                    },
                    "minItems": 0
                  },
                  "acceptable_alternatives": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "minLength": 1
                    }
                  },
                  "validity": {
                    "type": "object",
                    "properties": {
                      "within_tax_year": {
                        "type": "boolean"
                      },
                      "available_by": {
                        "type": "string",
                        "format": "date"
                      }
                    }
                  },
                  "reasons": {
                    "type": "object",
                    "properties": {
                      "short": {
                        "type": "string"
                      }
                    }
                  }
                }
              }
            },
            "cross_checks": {
              "type": "array",
              "items": {
                "type": "object",
                "required": ["name", "logic"],
                "properties": {
                  "name": {
                    "type": "string",
                    "minLength": 1
                  },
                  "logic": {
                    "type": "string",
                    "minLength": 1
                  }
                }
              }
            },
            "selection_rule": {
              "type": "object"
            },
            "notes": {
              "type": "object"
            }
          }
        }
      }
    },
    "status_classifier": {
      "type": "object",
      "required": [
        "present_verified",
        "present_unverified",
        "conflicting",
        "missing"
      ],
      "properties": {
        "present_verified": {
          "$ref": "#/definitions/statusClassifier"
        },
        "present_unverified": {
          "$ref": "#/definitions/statusClassifier"
        },
        "conflicting": {
          "$ref": "#/definitions/statusClassifier"
        },
        "missing": {
          "$ref": "#/definitions/statusClassifier"
        }
      }
    },
    "conflict_resolution": {
      "type": "object",
      "required": ["precedence"],
      "properties": {
        "precedence": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          },
          "minItems": 1
        },
        "escalation": {
          "type": "object"
        }
      }
    },
    "question_templates": {
      "type": "object",
      "required": ["default"],
      "properties": {
        "default": {
          "type": "object",
          "required": ["text", "why"],
          "properties": {
            "text": {
              "type": "string",
              "minLength": 1
            },
            "why": {
              "type": "string",
              "minLength": 1
            }
          }
        },
        "reasons": {
          "type": "object",
          "patternProperties": {
            "^[A-Za-z0-9_]+$": {
              "type": "string",
              "minLength": 1
            }
          }
        }
      }
    },
    "privacy": {
      "type": "object",
      "properties": {
        "vector_pii_free": {
          "type": "boolean"
        },
        "redact_patterns": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          }
        }
      }
    }
  },
  "definitions": {
    "statusClassifier": {
      "type": "object",
      "properties": {
        "min_ocr": {
          "type": "number",
          "minimum": 0,
          "maximum": 1
        },
        "min_extract": {
          "type": "number",
          "minimum": 0,
          "maximum": 1
        },
        "date_in_year": {
          "type": "boolean"
        },
        "date_in_year_or_tolerance": {
          "type": "boolean"
        },
        "conflict_rules": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          }
        },
        "default": {
          "type": "boolean"
        }
      }
    }
  }
}
