{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:richtexteditor:migration-report:2",
  "title": "RichTextEditor migration report schema v2",
  "oneOf": [
    { "$ref": "#/$defs/singleReport" },
    { "$ref": "#/$defs/batchReport" }
  ],
  "$defs": {
    "generator": {
      "type": "object",
      "required": ["name", "version"],
      "properties": {
        "name": { "const": "@richscripts/richtexteditor" },
        "version": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": false
    },
    "converterModule": {
      "oneOf": [
        { "type": "null" },
        {
          "type": "object",
          "required": ["label", "sha256", "hashVerifiedBeforeExecution"],
          "properties": {
            "label": { "type": "string", "minLength": 1 },
            "sha256": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
            "hashVerifiedBeforeExecution": { "type": "boolean" }
          },
          "additionalProperties": false
        }
      ]
    },
    "finding": {
      "type": "object",
      "required": ["kind", "name", "count", "paths"],
      "properties": {
        "kind": { "enum": ["embed", "node", "mark"] },
        "name": { "type": "string", "minLength": 1 },
        "count": { "type": "integer", "minimum": 1 },
        "paths": { "type": "array", "items": { "type": "string" }, "uniqueItems": true }
      },
      "additionalProperties": false
    },
    "documentReport": {
      "type": "object",
      "required": ["source", "readyForCutover", "config", "html", "mappedCommands", "unmapped", "unsupportedContent", "appliedConverters", "blockers", "warnings", "manualReview"],
      "properties": {
        "id": { "type": "string" },
        "source": { "type": "string", "minLength": 1 },
        "readyForCutover": { "type": "boolean" },
        "config": { "type": "object" },
        "html": { "type": "string" },
        "mappedCommands": { "type": "array", "items": { "type": "string" } },
        "unmapped": { "type": "array", "items": { "type": "string" } },
        "unsupportedContent": { "type": "array", "items": { "$ref": "#/$defs/finding" } },
        "appliedConverters": { "type": "array", "items": { "$ref": "#/$defs/finding" } },
        "blockers": { "type": "array", "items": { "type": "string" } },
        "warnings": { "type": "array", "items": { "type": "string" } },
        "manualReview": { "type": "array", "items": { "type": "string" } }
      },
      "additionalProperties": true
    },
    "singleReport": {
      "allOf": [
        { "$ref": "#/$defs/documentReport" },
        {
          "type": "object",
          "required": ["schemaVersion", "generator", "converterModule"],
          "properties": {
            "schemaVersion": { "const": 2 },
            "generator": { "$ref": "#/$defs/generator" },
            "converterModule": { "$ref": "#/$defs/converterModule" }
          }
        }
      ]
    },
    "unsupportedTotal": {
      "type": "object",
      "required": ["source", "kind", "name", "count", "documents"],
      "properties": {
        "source": { "type": "string" },
        "kind": { "enum": ["embed", "node", "mark"] },
        "name": { "type": "string" },
        "count": { "type": "integer", "minimum": 1 },
        "documents": { "type": "integer", "minimum": 1 }
      },
      "additionalProperties": false
    },
    "batchReport": {
      "type": "object",
      "required": ["schemaVersion", "generator", "converterModule", "readyForCutover", "summary", "reports"],
      "properties": {
        "schemaVersion": { "const": 2 },
        "generator": { "$ref": "#/$defs/generator" },
        "converterModule": { "$ref": "#/$defs/converterModule" },
        "readyForCutover": { "type": "boolean" },
        "summary": {
          "type": "object",
          "required": ["documents", "readyDocuments", "blockedDocuments", "unsupportedTotals", "unsupportedByDocument"],
          "properties": {
            "documents": { "type": "integer", "minimum": 1 },
            "readyDocuments": { "type": "integer", "minimum": 0 },
            "blockedDocuments": { "type": "integer", "minimum": 0 },
            "unsupportedTotals": { "type": "array", "items": { "$ref": "#/$defs/unsupportedTotal" } },
            "unsupportedByDocument": {
              "type": "array",
              "items": {
                "type": "object",
                "required": ["id", "source", "items"],
                "properties": {
                  "id": { "type": "string" },
                  "source": { "type": "string" },
                  "items": { "type": "array", "items": { "$ref": "#/$defs/finding" } }
                },
                "additionalProperties": false
              }
            }
          },
          "additionalProperties": false
        },
        "reports": { "type": "array", "minItems": 1, "items": { "$ref": "#/$defs/documentReport" } }
      },
      "additionalProperties": false
    }
  }
}
