{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://www.deixei.com/aes/v0.1.0/deployer_manifest.json",
  "title": "Deployer Manifest",
  "description": "Input artefact for `ops deploy plan|preflight|apply|verify|rollback`. Declares what is being deployed, where, how, and the gates that must be satisfied before each phase. One artefact per manifest; multi-artefact orchestration is above the schema.",

  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version",
    "manifest_id",
    "artefact",
    "target",
    "backend",
    "preflight",
    "apply",
    "verify",
    "rollback",
    "metadata"
  ],
  "properties": {
    "schema_version": {
      "type": "string",
      "pattern": "^\\d+\\.\\d+\\.\\d+$"
    },
    "manifest_id": {
      "type": "string",
      "minLength": 1
    },

    "artefact": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "version", "kind"],
      "properties": {
        "name":    { "type": "string", "minLength": 1 },
        "version": { "type": "string", "minLength": 1 },
        "kind": {
          "type": "string",
          "enum": ["container_image", "helm_chart", "terraform_module", "bicep_module", "function_app", "static_files", "custom"]
        },
        "source":  { "type": "string" },
        "digest": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "sha256": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
            "sha512": { "type": "string", "pattern": "^[a-f0-9]{128}$" }
          }
        }
      }
    },

    "target": {
      "type": "object",
      "additionalProperties": false,
      "required": ["environment", "systems"],
      "properties": {
        "environment": { "type": "string", "minLength": 1 },
        "systems":     { "type": "array", "items": { "type": "string" }, "minItems": 1 },
        "regions":     { "type": "array", "items": { "type": "string" } }
      }
    },

    "backend": {
      "type": "object",
      "additionalProperties": false,
      "required": ["type", "config"],
      "properties": {
        "type": {
          "type": "string",
          "enum": ["helm", "kubernetes", "terraform", "opentofu", "bicep", "azure_cli", "aws_cli", "gcp_cli", "docker", "ssh", "custom"]
        },
        "config": {
          "type": "object",
          "description": "Untyped at AES level; constrained by config_schema_ref if present."
        },
        "config_schema_ref": { "type": "string", "format": "uri" }
      }
    },

    "preflight": {
      "type": "object",
      "additionalProperties": false,
      "required": ["checks", "required_chk_guidance", "required_cab_decision"],
      "properties": {
        "checks": {
          "type": "array",
          "items": { "$ref": "#/$defs/check" }
        },
        "required_chk_guidance": {
          "type": "array",
          "items": { "type": "string" }
        },
        "required_cab_decision": {
          "type": ["string", "null"],
          "description": "CAB change_id reference, or null. If null, cab_exemption_rationale is required."
        },
        "cab_exemption_rationale": {
          "type": "string",
          "description": "Required when required_cab_decision is null. Must cite the policy that allows the exemption."
        }
      },
      "allOf": [
        {
          "if":   { "properties": { "required_cab_decision": { "type": "null" } } },
          "then": { "required": ["cab_exemption_rationale"], "properties": { "cab_exemption_rationale": { "minLength": 1 } } }
        }
      ]
    },

    "apply": {
      "type": "object",
      "additionalProperties": false,
      "required": ["intent", "program", "args", "timeout"],
      "properties": {
        "intent":  { "type": "string", "minLength": 1 },
        "program": { "type": "string", "minLength": 1 },
        "args":    { "type": "array", "items": { "type": "string" } },
        "env":     {
          "type": "object",
          "additionalProperties": { "type": "string" }
        },
        "timeout": { "type": "string", "description": "ISO 8601 duration." }
      }
    },

    "verify": {
      "type": "object",
      "additionalProperties": false,
      "required": ["wait_window", "health_checks"],
      "properties": {
        "wait_window":   { "type": "string", "description": "ISO 8601 duration." },
        "health_checks": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/check" }
        }
      }
    },

    "rollback": {
      "type": "object",
      "additionalProperties": false,
      "required": ["intent", "program", "args", "timeout", "verification"],
      "properties": {
        "intent":  { "type": "string", "minLength": 1 },
        "program": { "type": "string", "minLength": 1 },
        "args":    { "type": "array", "items": { "type": "string" } },
        "timeout": { "type": "string" },
        "verification": {
          "type": "array",
          "items": { "$ref": "#/$defs/check" }
        },
        "automatic_triggers": {
          "type": "array",
          "items": { "$ref": "#/$defs/automaticTrigger" }
        }
      }
    },

    "promotion": {
      "type": "object",
      "additionalProperties": false,
      "required": ["from_environment", "to_environment", "approval_required"],
      "properties": {
        "from_environment":  { "type": "string", "minLength": 1 },
        "to_environment":    { "type": "string", "minLength": 1 },
        "approval_required": { "type": "boolean" }
      }
    },

    "metadata": {
      "type": "object",
      "additionalProperties": false,
      "required": ["owner_team", "agent_wrangler_upn"],
      "properties": {
        "owner_team":         { "type": "string", "minLength": 1 },
        "agent_wrangler_upn": { "type": "string", "minLength": 1 },
        "tags":               { "type": "array", "items": { "type": "string" } }
      }
    }
  },

  "$defs": {

    "check": {
      "description": "Discriminated union on `kind`. Used by preflight.checks, verify.health_checks, and rollback.verification.",
      "type": "object",
      "required": ["id", "description", "kind"],
      "oneOf": [
        { "$ref": "#/$defs/checkHttp" },
        { "$ref": "#/$defs/checkCommand" },
        { "$ref": "#/$defs/checkMetric" }
      ]
    },
    "checkHttp": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "description", "kind", "url", "expected_status", "timeout"],
      "properties": {
        "id":                     { "type": "string", "minLength": 1 },
        "description":            { "type": "string", "minLength": 1 },
        "kind":                   { "const": "http" },
        "url":                    { "type": "string", "format": "uri" },
        "expected_status":        { "type": "integer", "minimum": 100, "maximum": 599 },
        "expected_body_fragment": { "type": "string" },
        "timeout":                { "type": "string" },
        "required":               { "type": "boolean", "default": true }
      }
    },
    "checkCommand": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "description", "kind", "program", "expected_exit_code", "timeout"],
      "properties": {
        "id":                 { "type": "string", "minLength": 1 },
        "description":        { "type": "string", "minLength": 1 },
        "kind":               { "const": "command" },
        "program":            { "type": "string", "minLength": 1 },
        "args":               { "type": "array", "items": { "type": "string" } },
        "expected_exit_code": { "type": "integer" },
        "timeout":            { "type": "string" },
        "required":           { "type": "boolean", "default": true }
      }
    },
    "checkMetric": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "description", "kind", "query_kind", "query", "expected_condition", "evaluation_window"],
      "properties": {
        "id":                 { "type": "string", "minLength": 1 },
        "description":        { "type": "string", "minLength": 1 },
        "kind":               { "const": "metric" },
        "query_kind":         { "type": "string", "enum": ["kql", "promql", "other"] },
        "query":              { "type": "string", "minLength": 1 },
        "expected_condition": { "type": "string", "minLength": 1 },
        "evaluation_window":  { "type": "string" },
        "required":           { "type": "boolean", "default": true }
      }
    },

    "automaticTrigger": {
      "type": "object",
      "required": ["id", "description", "condition_kind"],
      "oneOf": [
        { "$ref": "#/$defs/automaticTriggerMetric" }
      ]
    },
    "automaticTriggerMetric": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "description", "condition_kind", "query_kind", "query", "evaluation_window"],
      "properties": {
        "id":                { "type": "string", "minLength": 1 },
        "description":       { "type": "string", "minLength": 1 },
        "condition_kind":    { "const": "metric" },
        "query_kind":        { "type": "string", "enum": ["kql", "promql", "other"] },
        "query":             { "type": "string", "minLength": 1 },
        "evaluation_window": { "type": "string" }
      }
    }
  }
}
