{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://schemas.archal.ai/environment-sample/v1",
  "title": "Archal environment sample metadata",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema",
    "schemaVersion",
    "id",
    "environmentId",
    "slug",
    "displayName",
    "purpose",
    "category",
    "artifact",
    "stateContract",
    "entityCounts",
    "capabilities",
    "expectedFirstRead",
    "representativeMutation",
    "expectedDiff",
    "qualification",
    "provenance"
  ],
  "properties": {
    "schema": { "const": "archal.environment-sample" },
    "schemaVersion": { "const": 1 },
    "id": {
      "type": "string",
      "pattern": "^[a-z0-9]+(?:-[a-z0-9]+)*\\.[a-z0-9]+(?:-[a-z0-9]+)*\\.v1$"
    },
    "environmentId": {
      "enum": [
        "apify",
        "calcom",
        "clickup",
        "customerio",
        "datadog",
        "discord",
        "github",
        "gitlab",
        "google-workspace",
        "hubspot",
        "jira",
        "linear",
        "ownerrez",
        "pricelabs",
        "ramp",
        "sentry",
        "slack",
        "stripe",
        "supabase",
        "tavily",
        "unipile",
        "webflow"
      ]
    },
    "slug": { "type": "string", "pattern": "^[a-z0-9]+(?:-[a-z0-9]+)*$" },
    "displayName": { "type": "string", "minLength": 1 },
    "purpose": { "type": "string", "minLength": 1 },
    "category": {
      "enum": ["minimal", "small-team", "populated", "edge-case", "workflow"]
    },
    "artifact": {
      "type": "object",
      "additionalProperties": false,
      "required": ["path", "format", "mediaType", "sha256", "bytes"],
      "properties": {
        "path": { "type": "string", "pattern": "^samples/states/" },
        "format": { "enum": ["json", "sql"] },
        "mediaType": { "enum": ["application/json", "text/sql"] },
        "sha256": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
        "bytes": { "type": "integer", "minimum": 1, "maximum": 2097152 }
      }
    },
    "stateContract": {
      "type": "object",
      "additionalProperties": false,
      "required": ["schema", "version", "format"],
      "properties": {
        "schema": { "type": "string", "minLength": 1 },
        "version": { "type": "integer", "minimum": 1 },
        "format": { "enum": ["json", "sql"] }
      }
    },
    "entityCounts": {
      "type": "object",
      "additionalProperties": false,
      "required": ["total", "collections"],
      "properties": {
        "total": { "type": "integer", "minimum": 0 },
        "collections": {
          "type": "object",
          "additionalProperties": { "type": "integer", "minimum": 0 }
        }
      }
    },
    "capabilities": {
      "type": "object",
      "additionalProperties": false,
      "required": ["required", "supportedSurface", "limitations"],
      "properties": {
        "required": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": { "type": "string", "minLength": 1 }
        },
        "supportedSurface": { "type": "string", "minLength": 1 },
        "limitations": {
          "type": "array",
          "items": { "type": "string", "minLength": 1 }
        }
      }
    },
    "expectedFirstRead": { "$ref": "#/$defs/operation" },
    "representativeMutation": { "$ref": "#/$defs/operation" },
    "expectedDiff": {
      "type": "object",
      "additionalProperties": false,
      "required": ["changedPathsInclude", "exact", "notes"],
      "properties": {
        "changedPathsInclude": {
          "type": "array",
          "uniqueItems": true,
          "items": { "type": "string", "minLength": 1 }
        },
        "exact": { "type": "boolean" },
        "notes": { "type": "string", "minLength": 1 }
      }
    },
    "qualification": {
      "type": "object",
      "additionalProperties": false,
      "required": ["candidate", "validation", "certification", "reset", "isolation"],
      "properties": {
        "candidate": {
          "type": "object",
          "additionalProperties": false,
          "required": ["status", "basis"],
          "properties": {
            "status": { "const": "passed" },
            "basis": { "const": "catalog-admission" }
          }
        },
        "validation": {
          "type": "object",
          "additionalProperties": false,
          "required": ["status", "validator", "checks"],
          "properties": {
            "status": { "enum": ["passed", "failed", "unknown"] },
            "validator": { "const": "archal.environment-samples.v1" },
            "checks": {
              "type": "array",
              "minItems": 1,
              "uniqueItems": true,
              "items": { "type": "string", "minLength": 1 }
            }
          }
        },
        "certification": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "status",
            "lastHostedVerificationAt",
            "evidencePath",
            "sourceCommit",
            "runtimeBundleDigest"
          ],
          "properties": {
            "status": { "enum": ["passed", "failed", "unknown", "stale"] },
            "lastHostedVerificationAt": { "type": ["string", "null"], "format": "date-time" },
            "evidencePath": { "type": ["string", "null"] },
            "sourceCommit": { "type": ["string", "null"] },
            "runtimeBundleDigest": { "type": ["string", "null"] }
          }
        },
        "reset": { "$ref": "#/$defs/boundedVerification" },
        "isolation": {
          "type": "object",
          "additionalProperties": false,
          "required": ["status", "lastVerifiedAt"],
          "properties": {
            "status": { "enum": ["passed", "failed", "unknown"] },
            "lastVerifiedAt": { "type": ["string", "null"], "format": "date-time" }
          }
        }
      }
    },
    "provenance": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "kind",
        "sourcePath",
        "sourceSha256",
        "authoredBy",
        "transformationNotes",
        "sanitizationNotes"
      ],
      "properties": {
        "kind": { "enum": ["historical-fixture", "derived-fixture", "authored"] },
        "sourcePath": { "type": ["string", "null"] },
        "sourceSha256": {
          "oneOf": [{ "type": "null" }, { "type": "string", "pattern": "^[a-f0-9]{64}$" }]
        },
        "authoredBy": { "const": "archal" },
        "transformationNotes": {
          "type": "array",
          "minItems": 1,
          "items": { "type": "string", "minLength": 1 }
        },
        "sanitizationNotes": {
          "type": "array",
          "minItems": 1,
          "items": { "type": "string", "minLength": 1 }
        }
      }
    }
  },
  "$defs": {
    "operation": {
      "oneOf": [{ "$ref": "#/$defs/httpOperation" }, { "$ref": "#/$defs/mcpOperation" }]
    },
    "httpOperation": {
      "type": "object",
      "additionalProperties": false,
      "required": ["transport", "operationId", "method", "path", "body", "expectedStatus"],
      "properties": {
        "transport": { "const": "provider-rest" },
        "operationId": { "type": "string", "minLength": 1 },
        "method": { "enum": ["GET", "POST", "PUT", "PATCH", "DELETE"] },
        "path": { "type": "string", "pattern": "^/" },
        "body": { "type": ["object", "null"] },
        "expectedStatus": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": { "type": "integer", "minimum": 100, "maximum": 599 }
        }
      }
    },
    "mcpOperation": {
      "type": "object",
      "additionalProperties": false,
      "required": ["transport", "operationId", "toolName", "input", "expectedOutcome"],
      "properties": {
        "transport": { "const": "provider-mcp" },
        "operationId": { "type": "string", "minLength": 1 },
        "toolName": { "type": "string", "minLength": 1 },
        "input": { "type": "object" },
        "expectedOutcome": {
          "oneOf": [
            {
              "type": "object",
              "additionalProperties": false,
              "required": ["status"],
              "properties": { "status": { "const": "success" } }
            },
            {
              "type": "object",
              "additionalProperties": false,
              "required": ["status", "code"],
              "properties": {
                "status": { "const": "error" },
                "code": { "type": ["string", "number", "null"] }
              }
            }
          ]
        }
      }
    },
    "boundedVerification": {
      "type": "object",
      "additionalProperties": false,
      "required": ["expected", "status", "lastVerifiedAt"],
      "properties": {
        "expected": {
          "enum": ["restores-loaded-sample", "restores-original-session-seed"]
        },
        "status": { "enum": ["passed", "failed", "unknown"] },
        "lastVerifiedAt": { "type": ["string", "null"], "format": "date-time" }
      }
    }
  }
}
