{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://unpkg.com/dsh-bio-workflows@0.7.0/schema/bio-workflow-result.schema.json",
  "title": "BioWorkflowResult v1",
  "$comment": "Use validateBioWorkflowResultSemantics from dsh-bio-workflows/execution for cross-group aggregate limits and FastQC count/reference consistency.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "status",
    "generatedAt",
    "workflow",
    "planDigest",
    "artifacts",
    "summaries",
    "diagnostics"
  ],
  "properties": {
    "schemaVersion": { "const": "1" },
    "status": { "const": "completed" },
    "generatedAt": { "type": "string", "format": "date-time" },
    "workflow": { "$ref": "#/$defs/workflow" },
    "planDigest": { "$ref": "#/$defs/digest" },
    "artifacts": {
      "type": "array",
      "maxItems": 1024,
      "description": "Artifact groups; all nested items are additionally limited to 1024 in total by the semantic validator.",
      "x-maxTotalItems": 1024,
      "items": { "$ref": "#/$defs/artifactGroup" }
    },
    "summaries": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "fastqc": { "$ref": "#/$defs/fastqcSummary" }
      }
    },
    "diagnostics": {
      "type": "array",
      "maxItems": 32,
      "items": { "$ref": "#/$defs/diagnostic" }
    }
  },
  "$defs": {
    "identifier": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9]*(?:[._-][a-z0-9]+)*$"
    },
    "semver": {
      "type": "string",
      "pattern": "^(?:0|[1-9][0-9]*)\\.(?:0|[1-9][0-9]*)\\.(?:0|[1-9][0-9]*)(?:-[0-9A-Za-z.-]+)?(?:\\+[0-9A-Za-z.-]+)?$"
    },
    "digest": {
      "type": "string",
      "pattern": "^sha256:[a-f0-9]{64}$"
    },
    "byteCount": {
      "type": "string",
      "pattern": "^(?:0|[1-9][0-9]*)$"
    },
    "workflow": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "version", "bundleDigest"],
      "properties": {
        "id": { "$ref": "#/$defs/identifier" },
        "version": { "$ref": "#/$defs/semver" },
        "bundleDigest": { "$ref": "#/$defs/digest" }
      }
    },
    "artifactGroup": {
      "type": "object",
      "additionalProperties": false,
      "required": ["outputId", "type", "cardinality", "items"],
      "properties": {
        "outputId": { "$ref": "#/$defs/identifier" },
        "type": { "const": "file" },
        "cardinality": { "enum": ["one", "many"] },
        "items": {
          "type": "array",
          "maxItems": 1024,
          "items": { "$ref": "#/$defs/artifact" }
        }
      }
    },
    "artifact": {
      "type": "object",
      "additionalProperties": false,
      "required": ["ordinal", "path", "relativePath", "sizeBytes", "sha256"],
      "properties": {
        "ordinal": { "type": "integer", "minimum": 0, "maximum": 1023 },
        "path": { "type": "string", "minLength": 1, "maxLength": 4096 },
        "relativePath": {
          "type": "string",
          "minLength": 1,
          "maxLength": 4096,
          "pattern": "^(?!/)(?!.*(?:^|/)\\.\\.(?:/|$)).+$"
        },
        "sizeBytes": { "$ref": "#/$defs/byteCount" },
        "sha256": { "$ref": "#/$defs/digest" }
      }
    },
    "artifactReference": {
      "type": "object",
      "additionalProperties": false,
      "required": ["outputId", "ordinal"],
      "properties": {
        "outputId": { "$ref": "#/$defs/identifier" },
        "ordinal": { "type": "integer", "minimum": 0, "maximum": 1023 }
      }
    },
    "statusCounts": {
      "type": "object",
      "additionalProperties": false,
      "required": ["pass", "warn", "fail"],
      "properties": {
        "pass": { "type": "integer", "minimum": 0 },
        "warn": { "type": "integer", "minimum": 0 },
        "fail": { "type": "integer", "minimum": 0 }
      }
    },
    "fastqcModule": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "status"],
      "properties": {
        "name": { "type": "string", "minLength": 1, "maxLength": 256 },
        "status": { "enum": ["pass", "warn", "fail"] }
      }
    },
    "fastqcReport": {
      "type": "object",
      "additionalProperties": false,
      "required": ["artifact", "sample", "overallStatus", "counts", "modules"],
      "properties": {
        "artifact": { "$ref": "#/$defs/artifactReference" },
        "sample": { "type": "string", "minLength": 1, "maxLength": 512 },
        "overallStatus": { "enum": ["pass", "warn", "fail"] },
        "counts": { "$ref": "#/$defs/statusCounts" },
        "modules": {
          "type": "array",
          "minItems": 1,
          "maxItems": 512,
          "items": { "$ref": "#/$defs/fastqcModule" }
        }
      }
    },
    "fastqcSummary": {
      "type": "object",
      "additionalProperties": false,
      "required": ["schemaVersion", "reportCount", "moduleCounts", "reports"],
      "properties": {
        "schemaVersion": { "const": "1" },
        "reportCount": { "type": "integer", "minimum": 1, "maximum": 1024 },
        "moduleCounts": { "$ref": "#/$defs/statusCounts" },
        "reports": {
          "type": "array",
          "minItems": 1,
          "maxItems": 1024,
          "items": { "$ref": "#/$defs/fastqcReport" }
        }
      }
    },
    "diagnostic": {
      "type": "object",
      "additionalProperties": false,
      "required": ["code", "message"],
      "properties": {
        "code": { "$ref": "#/$defs/identifier" },
        "message": { "type": "string", "minLength": 1, "maxLength": 512 }
      }
    }
  }
}
