{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://hunter-harness.local/contracts/build-profile-v3.schema.json",
  "title": "Hunter Harness Build Profile v3",
  "type": "object",
  "required": [
    "schemaVersion",
    "commands",
    "verificationInputs",
    "verificationGraph"
  ],
  "properties": {
    "schemaVersion": {
      "const": 3
    },
    "commands": {
      "type": "object",
      "additionalProperties": {
        "type": "object",
        "required": [
          "command",
          "argvTemplate",
          "scope",
          "inputs",
          "coverage",
          "source"
        ],
        "properties": {
          "command": {
            "type": "string",
            "minLength": 1
          },
          "argvTemplate": {
            "type": "array",
            "items": {
              "type": "string"
            },
            "minItems": 1
          },
          "scope": {
            "type": "string",
            "minLength": 1
          },
          "inputs": {
            "type": "array",
            "items": {
              "type": "string",
              "minLength": 1
            }
          },
          "coverage": {
            "type": "string",
            "minLength": 1
          },
          "source": {
            "enum": [
              "detected",
              "user"
            ]
          }
        }
      }
    },
    "verificationInputs": {
      "type": "object",
      "additionalProperties": {
        "type": "array",
        "items": {
          "type": "string",
          "minLength": 1
        }
      }
    },
    "verificationGraph": {
      "type": "object",
      "required": [
        "schemaVersion",
        "source",
        "candidateTarget",
        "targets"
      ],
      "properties": {
        "schemaVersion": {
          "const": 1
        },
        "source": {
          "enum": [
            "detected",
            "user"
          ]
        },
        "candidateTarget": {
          "type": [
            "string",
            "null"
          ]
        },
        "targets": {
          "type": "object",
          "additionalProperties": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "commandKey",
              "dependsOn",
              "requiredCoverage",
              "candidate",
              "requiredCapabilities"
            ],
            "properties": {
              "id": {
                "type": "string",
                "pattern": "^[A-Za-z][A-Za-z0-9_-]*$"
              },
              "commandKey": {
                "type": "string",
                "pattern": "^[A-Za-z][A-Za-z0-9_-]*$"
              },
              "dependsOn": {
                "type": "array",
                "items": {
                  "type": "string",
                  "pattern": "^[A-Za-z][A-Za-z0-9_-]*$"
                },
                "uniqueItems": true
              },
              "requiredCoverage": {
                "enum": [
                  "incremental",
                  "module-am",
                  "module",
                  "full"
                ]
              },
              "argvTemplate": {
                "type": "array",
                "items": {
                  "type": "string"
                },
                "minItems": 1
              },
              "inputs": {
                "type": "array",
                "items": {
                  "type": "string",
                  "minLength": 1
                }
              },
              "coverage": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "coverageLevel": {
                "enum": [
                  "incremental",
                  "module",
                  "module-am",
                  "full"
                ]
              },
              "candidate": {
                "type": "boolean"
              },
              "requiredCapabilities": {
                "type": "array",
                "items": {
                  "type": "string",
                  "minLength": 1
                },
                "uniqueItems": true
              },
              "resourceLocks": {
                "type": "array",
                "items": {
                  "type": "string",
                  "minLength": 1
                },
                "uniqueItems": true
              },
              "estimatedDurationSeconds": {
                "type": "number",
                "minimum": 0
              },
              "requiresFrozenIdentity": {
                "type": "boolean"
              },
              "reusePolicy": {
                "enum": [
                  "never",
                  "identity-exact",
                  "ledger-exact"
                ]
              }
            }
          }
        }
      }
    },
    "serviceStart": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "healthUrl",
        "startTimeoutSec",
        "inputFiles",
        "source",
        "profile",
        "overlayPath"
      ],
      "properties": {
        "argvTemplate": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "command": {
          "type": "string",
          "minLength": 1
        },
        "healthUrl": {
          "type": "string"
        },
        "healthFile": {
          "type": "string"
        },
        "startTimeoutSec": {
          "type": "number",
          "exclusiveMinimum": 0
        },
        "inputFiles": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          }
        },
        "source": {
          "enum": [
            "detected",
            "user"
          ]
        },
        "profile": {
          "type": "string"
        },
        "overlayPath": {
          "type": "string"
        },
        "legacyWarning": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "reasonCode",
            "platform",
            "message"
          ],
          "properties": {
            "reasonCode": {
              "const": "PROFILE_MIGRATION_UNSAFE"
            },
            "platform": {
              "enum": [
                "windows",
                "posix"
              ]
            },
            "message": {
              "type": "string",
              "minLength": 1
            }
          }
        }
      },
      "oneOf": [
        {
          "required": [
            "argvTemplate"
          ],
          "not": {
            "required": [
              "command"
            ]
          }
        },
        {
          "required": [
            "command",
            "legacyWarning"
          ],
          "not": {
            "required": [
              "argvTemplate"
            ]
          }
        }
      ]
    }
  }
}
