{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "dd-flow/code-stage-report@2",
  "title": "Code stage report data",
  "type": "object",
  "additionalProperties": true,
  "allOf": [
    {
      "if": {
        "properties": { "schema_id": { "const": "dd-flow/code-stage-report@2" } },
        "required": ["schema_id"]
      },
      "then": { "required": ["flow_flags"] }
    }
  ],
  "required": [
    "schema_id",
    "run",
    "stage",
    "project",
    "subject",
    "overall",
    "breadcrumbs",
    "implemented_goals",
    "acceptance_scenarios",
    "changed_files",
    "checks",
    "review",
    "defs"
  ],
  "properties": {
    "schema_id": { "enum": ["dd-flow/code-stage-report@1", "dd-flow/code-stage-report@2"] },
    "run": { "$ref": "#/definitions/run" },
    "stage": { "$ref": "#/definitions/stage" },
    "project": { "$ref": "#/definitions/namedEntity" },
    "subject": { "$ref": "#/definitions/namedEntity" },
    "flow_flags": { "$ref": "#/definitions/flowFlags" },
    "overall": { "$ref": "#/definitions/overall" },
    "breadcrumbs": {
      "type": "array",
      "items": { "$ref": "#/definitions/breadcrumb" }
    },
    "implemented_goals": {
      "type": "array",
      "items": { "$ref": "#/definitions/textBlock" }
    },
    "acceptance_scenarios": {
      "type": "array",
      "items": { "$ref": "#/definitions/scenario" }
    },
    "changed_files": {
      "type": "array",
      "items": { "$ref": "#/definitions/fileRef" }
    },
    "policy_context": { "$ref": "#/definitions/policyContext" },
    "sdlc_contours": { "$ref": "#/definitions/sdlcContours" },
    "knowledge_candidates": {
      "type": "object",
      "additionalProperties": true,
      "properties": {
        "source_candidates": { "type": "string" },
        "total": { "type": "integer", "minimum": 0 },
        "implemented": { "type": "integer", "minimum": 0 },
        "documented_during_code": { "type": "integer", "minimum": 0 },
        "already_documented": { "type": "integer", "minimum": 0 },
        "task_local_only": { "type": "integer", "minimum": 0 },
        "deferred_as_DEF": { "type": "integer", "minimum": 0 },
        "needs_user_confirmation": { "type": "integer", "minimum": 0 },
        "not_applicable": { "type": "integer", "minimum": 0 },
        "summary": { "type": "string" }
      }
    },
    "checks": {
      "type": "array",
      "items": { "$ref": "#/definitions/check" }
    },
    "review": {
      "type": "object",
      "additionalProperties": true,
      "required": ["verdict", "findings"],
      "properties": {
        "verdict": { "type": "string", "minLength": 1 },
        "findings": {
          "type": "array",
          "items": { "$ref": "#/definitions/finding" }
        }
      }
    },
    "review_gates": {
      "type": "array",
      "items": { "$ref": "#/definitions/reviewGate" }
    },
    "design_aspect_traceability": {
      "type": "object",
      "additionalProperties": true,
      "properties": {
        "source": { "type": "string" },
        "verdict": { "type": "string" },
        "summary": { "type": "string" },
        "implemented_seeds": { "type": "array" },
        "deviations": { "type": "array" },
        "gaps": { "type": "array" }
      }
    },
    "testing_system": {
      "type": "object",
      "additionalProperties": true,
      "properties": {
        "source": { "type": "string" },
        "verdict": { "type": "string" },
        "summary": { "type": "string" },
        "commands": { "type": "array" },
        "datasets": { "type": "array" },
        "gaps": { "type": "array" }
      }
    },
    "aspect_coverage": { "$ref": "#/definitions/aspectCoverage" },
    "defs": {
      "type": "array",
      "items": { "$ref": "#/definitions/deferral" }
    }
  },
  "definitions": {
    "run": {
      "type": "object",
      "additionalProperties": true,
      "required": ["run_id"],
      "anyOf": [
        {"required": ["run_state"]},
        {"required": ["run_index"]}
      ],
      "properties": {
        "run_id": { "type": "string", "pattern": "^RUN-[0-9]{3}-[a-z0-9]+(?:-[a-z0-9]+)*$" },
        "run_state": { "type": "string", "minLength": 1 },
        "run_index": { "type": "string", "minLength": 1 }
      }
    },
    "stage": {
      "type": "object",
      "additionalProperties": true,
      "required": ["name", "dir", "status"],
      "properties": {
        "name": { "type": "string", "minLength": 1 },
        "dir": { "type": "string", "pattern": "^[0-9]{2}-[a-z0-9]+(?:-[a-z0-9]+)*$" },
        "status": { "type": "string", "minLength": 1 }
      }
    },
    "namedEntity": {
      "type": "object",
      "additionalProperties": true,
      "required": ["id", "title"],
      "properties": {
        "id": { "type": "string", "minLength": 1 },
        "title": { "type": "string", "minLength": 1 }
      }
    },
    "overall": {
      "type": "object",
      "additionalProperties": true,
      "required": ["verdict", "summary", "next_action"],
      "properties": {
        "verdict": { "type": "string", "minLength": 1 },
        "summary": { "type": "string", "minLength": 1 },
        "next_action": { "type": "string", "minLength": 1 }
      }
    },
    "breadcrumb": {
      "type": "object",
      "additionalProperties": false,
      "required": ["label", "href", "status"],
      "properties": {
        "label": { "type": "string", "minLength": 1 },
        "href": { "type": "string" },
        "status": { "type": "string", "minLength": 1 }
      }
    },
    "textBlock": {
      "type": "object",
      "additionalProperties": true,
      "required": ["title", "summary"],
      "properties": {
        "title": { "type": "string", "minLength": 1 },
        "summary": { "type": "string", "minLength": 1 }
      }
    },
    "scenario": {
      "type": "object",
      "additionalProperties": true,
      "required": ["id", "title", "verdict", "steps", "evidence"],
      "properties": {
        "id": { "type": "string", "minLength": 1 },
        "title": { "type": "string", "minLength": 1 },
        "verdict": { "type": "string", "minLength": 1 },
        "steps": {
          "type": "array",
          "items": { "$ref": "#/definitions/textBlock" }
        },
        "evidence": {
          "type": "array",
          "items": { "$ref": "#/definitions/fileRef" }
        }
      }
    },
    "fileRef": {
      "type": "object",
      "additionalProperties": true,
      "required": ["path", "label"],
      "properties": {
        "path": { "type": "string", "minLength": 1 },
        "label": { "type": "string", "minLength": 1 }
      }
    },
    "check": {
      "type": "object",
      "additionalProperties": true,
      "required": ["name", "status"],
      "properties": {
        "name": { "type": "string", "minLength": 1 },
        "status": { "type": "string", "minLength": 1 }
      }
    },
    "finding": {
      "type": "object",
      "additionalProperties": true,
      "required": ["severity", "title", "status"],
      "properties": {
        "severity": { "type": "string", "minLength": 1 },
        "title": { "type": "string", "minLength": 1 },
        "status": { "type": "string", "minLength": 1 }
      }
    },
    "deferral": {
      "type": "object",
      "additionalProperties": true,
      "required": ["id", "status", "summary"],
      "properties": {
        "id": { "type": "string", "minLength": 1 },
        "status": { "type": "string", "minLength": 1 },
        "summary": { "type": "string", "minLength": 1 }
      }
    },
    "reviewGate": {
      "type": "object",
      "additionalProperties": true,
      "required": ["id", "label", "stage", "applicability", "verdict", "summary"],
      "properties": {
        "id": { "type": "string", "minLength": 1 },
        "label": { "type": "string", "minLength": 1 },
        "stage": { "type": "string", "minLength": 1 },
        "applicability": { "type": "string", "minLength": 1 },
        "verdict": { "type": "string", "minLength": 1 },
        "summary": { "type": "string", "minLength": 1 },
        "evidence": {
          "type": "array",
          "items": { "type": "string", "minLength": 1 }
        }
      }
    },
    "aspectCoverage": {
      "type": "object",
      "additionalProperties": true,
      "required": ["source", "verdict", "summary"],
      "properties": {
        "source": { "type": "string", "minLength": 1 },
        "verdict": { "type": "string", "minLength": 1 },
        "summary": { "type": "string", "minLength": 1 },
        "counts": {
          "type": "object",
          "additionalProperties": true,
          "properties": {
            "applicable": { "type": "integer", "minimum": 0 },
            "not_applicable": { "type": "integer", "minimum": 0 },
            "unknown": { "type": "integer", "minimum": 0 },
            "covered": { "type": "integer", "minimum": 0 },
            "blocked": { "type": "integer", "minimum": 0 },
            "deferred": { "type": "integer", "minimum": 0 }
          }
        },
        "artifacts": {
          "type": "array",
          "items": { "type": "string", "minLength": 1 }
        }
      }
    },
    "sdlcContours": {
      "type": "object",
      "additionalProperties": true,
      "properties": {
        "summary": { "type": "string" },
        "contours": {
          "type": "array",
          "items": { "$ref": "#/definitions/sdlcContour" }
        },
        "policy_migration": { "type": "string" },
        "next_delivery_action": { "type": "string" }
      }
    },
    "sdlcContour": {
      "type": "object",
      "additionalProperties": true,
      "required": ["name", "applicability_status", "gate_status", "summary"],
      "properties": {
        "name": { "type": "string", "minLength": 1 },
        "applicability_status": { "type": "string", "minLength": 1 },
        "verification_state": { "type": "string" },
        "closure_state": { "type": "string" },
        "gate_status": { "type": "string", "minLength": 1 },
        "target_stage": { "type": "string" },
        "delivery_target": { "type": "string" },
        "summary": { "type": "string", "minLength": 1 },
        "reason": { "type": "string" },
        "evidence": { "type": "string" },
        "next_action": { "type": "string" }
      }
    },
    "policyContext": {
      "type": "object",
      "additionalProperties": true,
      "required": ["sources", "git", "checks", "delivery", "gaps"],
      "properties": {
        "sources": { "type": "object", "additionalProperties": true },
        "git": {
          "type": "object",
          "additionalProperties": true,
          "properties": {
            "workspace_route": { "type": "string" },
            "delivery_strategy": {
              "type": "string",
              "enum": ["direct_commit", "direct_commit_push", "feature_merge", "pull_request", "merge_queue", "squash_merge", "rebase_ff", "release_branch", "external_handoff", "local_only", "no_git"]
            },
            "fixation_required": { "type": "boolean" },
            "result_evidence_required": { "type": "array", "items": { "type": "string" } },
            "actual_workspace_route": { "type": "string" },
            "route_match": { "type": "boolean" }
          }
        },
        "checks": { "type": "object", "additionalProperties": true },
        "delivery": { "type": "object", "additionalProperties": true },
        "gaps": { "type": "object", "additionalProperties": true }
      }
    },
    "flowFlags": {
      "type": "object",
      "additionalProperties": false,
      "required": ["flow_kind", "snapshot_revision", "resolution_status", "values", "snapshot_checksum"],
      "properties": {
        "contract": { "type": "object", "additionalProperties": true },
        "flow_kind": { "type": "string", "minLength": 1 },
        "preset": { "type": "object", "additionalProperties": true },
        "snapshot_revision": { "type": "integer", "minimum": 1 },
        "resolution_status": { "type": "string", "enum": ["valid", "legacy_incomplete", "reconciliation_required"] },
        "values": { "type": "object", "additionalProperties": { "$ref": "#/definitions/flagValue" } },
        "snapshot_checksum": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
        "floors_applied": { "type": "array", "items": { "type": "string" } },
        "resolved_at": { "type": "string", "format": "date-time" }
      }
    },
    "flagValue": {
      "type": "object",
      "additionalProperties": false,
      "required": ["value", "value_type", "owner", "source", "rationale", "resolved_at"],
      "properties": {
        "value": {},
        "value_type": { "type": "string", "minLength": 1 },
        "owner": { "type": "string", "minLength": 1 },
        "source": {
          "type": "object",
          "additionalProperties": false,
          "required": ["kind", "ref"],
          "properties": {
            "kind": { "type": "string", "minLength": 1 },
            "ref": { "type": "string", "minLength": 1 },
            "revision": { "type": ["integer", "null"] }
          }
        },
        "rationale": { "type": "string", "minLength": 1 },
        "resolved_at": { "type": "string", "format": "date-time" }
      }
    }
  }
}
