{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://standards.rootblocks.com/schemas/data/data-construction.v1.schema.json",
  "title": "RootBlocks Data Construction v1",
  "type": "object",
  "additionalProperties": false,
  "required": ["schemaVersion", "dataConstructionId", "standard", "domains", "resources", "accessContracts", "evidence"],
  "properties": {
    "schemaVersion": {
      "const": "rootblocks.data-construction/v1"
    },
    "dataConstructionId": {
      "$ref": "#/$defs/id"
    },
    "standard": {
      "$ref": "#/$defs/standardRef"
    },
    "domains": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/domainRef"
      }
    },
    "resources": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/dataResource"
      }
    },
    "accessContracts": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/accessContract"
      }
    },
    "evidence": {
      "$ref": "#/$defs/evidencePolicy"
    }
  },
  "$defs": {
    "id": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9.-]{2,127}$"
    },
    "semver": {
      "type": "string",
      "pattern": "^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(-[A-Za-z0-9.-]+)?(\\+[A-Za-z0-9.-]+)?$"
    },
    "hash": {
      "type": "string",
      "pattern": "^[a-f0-9]{64,128}$"
    },
    "path": {
      "type": "string",
      "pattern": "^[A-Za-z0-9_./@-]+$"
    },
    "standardRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["bundleId", "version", "hash"],
      "properties": {
        "bundleId": {
          "$ref": "#/$defs/id"
        },
        "version": {
          "$ref": "#/$defs/semver"
        },
        "hash": {
          "$ref": "#/$defs/hash"
        }
      }
    },
    "domainRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["domainId", "boundedContextId"],
      "properties": {
        "domainId": {
          "$ref": "#/$defs/id"
        },
        "boundedContextId": {
          "$ref": "#/$defs/id"
        }
      }
    },
    "dataResource": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "kind",
        "providerClass",
        "ownership",
        "dataClassification",
        "schemaVersion",
        "migrationPolicy",
        "operationalPolicy"
      ],
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "kind": {
          "enum": [
            "relational-database",
            "document-database",
            "key-value-database",
            "cache",
            "search-index",
            "queue",
            "stream",
            "object-storage",
            "data-warehouse",
            "custom"
          ]
        },
        "providerClass": {
          "enum": [
            "postgresql",
            "sql-server",
            "mysql",
            "sqlite",
            "mongodb",
            "dynamodb",
            "cosmos-db",
            "redis",
            "elasticsearch",
            "opensearch",
            "sqs",
            "service-bus",
            "kafka",
            "blob-storage",
            "s3",
            "bigquery",
            "snowflake",
            "custom"
          ]
        },
        "ownership": {
          "$ref": "#/$defs/ownership"
        },
        "dataClassification": {
          "enum": ["public", "internal", "restricted", "sensitive"]
        },
        "schemaVersion": {
          "$ref": "#/$defs/semver"
        },
        "migrationPolicy": {
          "$ref": "#/$defs/migrationPolicy"
        },
        "operationalPolicy": {
          "$ref": "#/$defs/operationalPolicy"
        },
        "indexes": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/index"
          }
        },
        "partitioning": {
          "$ref": "#/$defs/partitioning"
        },
        "retention": {
          "$ref": "#/$defs/retention"
        }
      }
    },
    "ownership": {
      "type": "object",
      "additionalProperties": false,
      "required": ["domainId", "boundedContextId", "ownerTeam", "shared"],
      "properties": {
        "domainId": {
          "$ref": "#/$defs/id"
        },
        "boundedContextId": {
          "$ref": "#/$defs/id"
        },
        "ownerTeam": {
          "$ref": "#/$defs/id"
        },
        "shared": {
          "type": "boolean"
        },
        "allowedConsumers": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/id"
          },
          "uniqueItems": true
        },
        "separationRisk": {
          "enum": ["low", "medium", "high"]
        }
      }
    },
    "migrationPolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": ["strategy", "driftDetection", "rollbackRequired", "compatibilityReviewRequired", "evidenceRefs"],
      "properties": {
        "strategy": {
          "enum": ["none", "expand-contract", "online", "offline-window", "destructive", "provider-migration"]
        },
        "driftDetection": {
          "enum": ["before-deploy", "scheduled", "manual", "none"]
        },
        "rollbackRequired": {
          "type": "boolean"
        },
        "compatibilityReviewRequired": {
          "type": "boolean"
        },
        "evidenceRefs": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/path"
          },
          "uniqueItems": true
        }
      }
    },
    "operationalPolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": ["backupRequired", "restoreTestRequired", "encryptionRequired", "monitoringRequired", "recovery"],
      "properties": {
        "backupRequired": {
          "type": "boolean"
        },
        "restoreTestRequired": {
          "type": "boolean"
        },
        "encryptionRequired": {
          "type": "boolean"
        },
        "monitoringRequired": {
          "type": "boolean"
        },
        "recovery": {
          "$ref": "#/$defs/recovery"
        }
      }
    },
    "recovery": {
      "type": "object",
      "additionalProperties": false,
      "required": ["rpoMinutes", "rtoMinutes"],
      "properties": {
        "rpoMinutes": {
          "type": "integer",
          "minimum": 0,
          "maximum": 10080
        },
        "rtoMinutes": {
          "type": "integer",
          "minimum": 0,
          "maximum": 10080
        }
      }
    },
    "index": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "fields", "purpose", "evidenceRef"],
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "fields": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "string",
            "pattern": "^[A-Za-z0-9_.-]+$"
          }
        },
        "purpose": {
          "enum": ["lookup", "uniqueness", "ordering", "search", "join", "custom"]
        },
        "evidenceRef": {
          "$ref": "#/$defs/path"
        }
      }
    },
    "partitioning": {
      "type": "object",
      "additionalProperties": false,
      "required": ["strategy"],
      "properties": {
        "strategy": {
          "enum": ["none", "by-tenant", "by-domain", "by-time", "by-key", "custom"]
        },
        "key": {
          "type": "string",
          "pattern": "^[A-Za-z0-9_.-]+$"
        }
      }
    },
    "retention": {
      "type": "object",
      "additionalProperties": false,
      "required": ["policyRef", "days", "deletionEvidenceRequired"],
      "properties": {
        "policyRef": {
          "$ref": "#/$defs/id"
        },
        "days": {
          "type": "integer",
          "minimum": 1,
          "maximum": 36500
        },
        "deletionEvidenceRequired": {
          "type": "boolean"
        }
      }
    },
    "accessContract": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "resourceId", "layer", "accessMode", "contractKind", "sourceMapRequired", "evidenceRequired"],
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "resourceId": {
          "$ref": "#/$defs/id"
        },
        "layer": {
          "enum": ["domain", "application", "infrastructure", "workflows", "host", "external"]
        },
        "accessMode": {
          "enum": ["read", "write", "read-write", "publish", "consume", "index", "custom"]
        },
        "contractKind": {
          "enum": ["port", "repository", "query-model", "command-model", "adapter", "outbox", "inbox", "projection", "custom"]
        },
        "directSdkAccess": {
          "type": "boolean",
          "default": false
        },
        "transactionBoundary": {
          "enum": ["single-resource", "unit-of-work", "eventual-consistency", "none"]
        },
        "sourceMapRequired": {
          "type": "boolean"
        },
        "evidenceRequired": {
          "type": "boolean"
        }
      }
    },
    "evidencePolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": ["required", "evidenceRefs"],
      "properties": {
        "required": {
          "const": true
        },
        "evidenceRefs": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/path"
          },
          "uniqueItems": true
        }
      }
    }
  }
}
