{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://vertile.ai/schemas/iac.schema.json",
  "title": "Vertile AI IaC Manifest",
  "description": "Portable app infrastructure intent for Vertile AI IaC.",
  "type": "object",
  "required": ["version", "project", "providers", "apps"],
  "additionalProperties": true,
  "properties": {
    "$schema": {
      "type": "string"
    },
    "version": {
      "const": 1
    },
    "project": {
      "oneOf": [
        { "type": "string", "minLength": 1 },
        {
          "type": "object",
          "required": ["name"],
          "additionalProperties": true,
          "properties": {
            "key": { "type": "string", "minLength": 1 },
            "name": { "type": "string", "minLength": 1 }
          }
        }
      ]
    },
    "environments": {
      "oneOf": [
        {
          "type": "array",
          "items": { "type": "string", "minLength": 1 },
          "uniqueItems": true
        },
        {
          "type": "object",
          "additionalProperties": { "$ref": "#/$defs/envEnvironment" }
        }
      ],
      "default": ["development", "preview", "production"]
    },
    "providers": {
      "type": "object",
      "additionalProperties": { "$ref": "#/$defs/providerConfig" },
      "properties": {
        "vercel": {
          "allOf": [
            { "$ref": "#/$defs/providerConfig" },
            {
              "type": "object",
              "properties": {
                "team": { "type": "string" },
                "teamId": { "type": "string" },
                "teamSlug": { "type": "string" },
                "env": {
                  "type": "object",
                  "additionalProperties": true,
                  "properties": {
                    "targets": {
                      "type": "object",
                      "additionalProperties": {
                        "oneOf": [
                          { "type": "string", "minLength": 1 },
                          {
                            "type": "object",
                            "additionalProperties": true,
                            "properties": {
                              "environment": { "type": "string", "minLength": 1 }
                            }
                          }
                        ]
                      }
                    }
                  }
                },
                "protectionBypassForAutomation": {
                  "$ref": "#/$defs/vercelProtectionBypassForAutomation"
                },
                "projectDefaults": { "$ref": "#/$defs/vercelProjectSettings" },
                "projectSettingsDefaults": { "$ref": "#/$defs/vercelProjectSettings" }
              }
            }
          ]
        },
        "aws": {
          "allOf": [
            { "$ref": "#/$defs/providerConfig" },
            {
              "type": "object",
              "properties": {
                "region": { "type": "string" },
                "profile": { "type": "string" },
                "deployments": {
                  "type": "object",
                  "additionalProperties": { "$ref": "#/$defs/providerDeployment" }
                },
                "defaultTags": {
                  "type": "object",
                  "additionalProperties": { "type": ["string", "number", "boolean"] }
                }
              }
            }
          ]
        },
        "digitalocean": {
          "allOf": [
            { "$ref": "#/$defs/providerConfig" },
            {
              "type": "object",
              "properties": {
                "region": { "type": "string" },
                "version": { "type": "string" }
              }
            }
          ]
        },
        "github": {
          "allOf": [
            { "$ref": "#/$defs/providerConfig" },
            {
              "type": "object",
              "properties": {
                "repository": { "type": "string", "minLength": 1 },
                "repo": { "type": "string", "minLength": 1 },
                "token": { "type": "string", "minLength": 1 },
                "actions": {
                  "type": "object",
                  "additionalProperties": true,
                  "properties": {
                    "env": {
                      "type": "array",
                      "items": { "$ref": "#/$defs/githubActionsEnvEntry" }
                    },
                    "variables": {
                      "type": "array",
                      "items": { "type": "string", "minLength": 1 },
                      "uniqueItems": true
                    },
                    "secrets": {
                      "type": "array",
                      "items": { "type": "string", "minLength": 1 },
                      "uniqueItems": true
                    },
                    "environments": {
                      "type": "object",
                      "additionalProperties": {
                        "type": "object",
                        "additionalProperties": true,
                        "properties": {
                          "name": { "type": "string", "minLength": 1 },
                          "githubEnvironment": { "type": "string", "minLength": 1 },
                          "branches": {
                            "type": "array",
                            "items": { "type": "string", "minLength": 1 },
                            "uniqueItems": true
                          },
                          "branch": { "type": "string", "minLength": 1 },
                          "env": {
                            "type": "array",
                            "items": { "$ref": "#/$defs/githubActionsEnvEntry" }
                          },
                          "variables": {
                            "type": "array",
                            "items": { "type": "string", "minLength": 1 },
                            "uniqueItems": true
                          },
                          "secrets": {
                            "type": "array",
                            "items": { "type": "string", "minLength": 1 },
                            "uniqueItems": true
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          ]
        }
      }
    },
    "env": {
      "type": "object",
      "additionalProperties": true,
      "properties": {
        "sourceDir": { "type": "string" },
        "sharedKey": { "type": "string" },
        "metadataFile": { "type": "string" },
        "metadata": {
          "type": "object",
          "additionalProperties": { "$ref": "#/$defs/envMetadataDocument" },
          "properties": {
            "sources": {
              "type": "object",
              "additionalProperties": { "$ref": "#/$defs/envMetadataDocument" }
            }
          }
        },
        "sync": {
          "type": "object",
          "additionalProperties": true,
          "properties": {
            "apps": {
              "type": "array",
              "items": { "type": "string", "minLength": 1 },
              "uniqueItems": true
            },
            "packages": {
              "type": "array",
              "items": { "type": "string", "minLength": 1 },
              "uniqueItems": true
            },
            "sharedKey": { "type": "string" },
            "metadataFile": { "type": "string" },
            "skipInVercel": { "type": "boolean" },
            "directOutputs": { "type": "boolean" },
            "patchVariantsFromExample": { "type": "boolean" },
            "disallowSharedOverrides": { "type": "boolean" },
            "forbidSharedOverrides": { "type": "boolean" },
            "requiredSharedAliases": {
              "type": "array",
              "items": { "type": "string", "minLength": 1 },
              "uniqueItems": true
            }
          }
        }
      }
    },
    "apps": {
      "type": "array",
      "items": { "$ref": "#/$defs/app" }
    },
    "packages": {
      "type": "array",
      "items": { "$ref": "#/$defs/package" }
    },
    "domains": {
      "type": "array",
      "items": { "$ref": "#/$defs/domain" }
    },
    "objectStorage": {
      "type": "array",
      "items": { "$ref": "#/$defs/keyedResource" }
    },
    "databases": {
      "type": "array",
      "items": {
        "allOf": [
          { "$ref": "#/$defs/keyedResource" },
          {
            "type": "object",
            "properties": {
              "engine": { "type": "string" }
            }
          }
        ]
      }
    },
    "queues": {
      "type": "array",
      "items": {
        "allOf": [
          { "$ref": "#/$defs/keyedResource" },
          {
            "type": "object",
            "properties": {
              "kind": { "type": "string" }
            }
          }
        ]
      }
    },
    "sandboxes": {
      "type": "array",
      "items": { "$ref": "#/$defs/keyedResource" }
    },
    "clusters": {
      "type": "array",
      "items": {
        "allOf": [
          { "$ref": "#/$defs/keyedResource" },
          {
            "type": "object",
            "properties": {
              "size": { "type": "integer", "minimum": 1 },
              "nodes": { "type": "integer", "minimum": 1 }
            }
          }
        ]
      }
    }
  },
  "$defs": {
    "providerConfig": {
      "type": "object",
      "additionalProperties": true,
      "properties": {
        "deployments": {
          "type": "object",
          "additionalProperties": { "$ref": "#/$defs/providerDeployment" }
        },
        "resources": {
          "type": "array",
          "items": { "$ref": "#/$defs/providerResource" }
        }
      }
    },
    "providerDeployment": {
      "type": "object",
      "additionalProperties": true,
      "properties": {
        "environment": { "type": "string", "minLength": 1 },
        "team": { "type": "string" },
        "teamId": { "type": "string" },
        "teamSlug": { "type": "string" },
        "region": { "type": "string" },
        "profile": { "type": "string" },
        "version": { "type": "string" },
        "stateKey": { "type": "string" },
        "tags": {
          "type": "object",
          "additionalProperties": { "type": ["string", "number", "boolean"] }
        },
        "env": {
          "type": "object",
          "additionalProperties": true,
          "properties": {
            "destination": { "type": "string" },
            "path": { "type": "string" }
          }
        }
      }
    },
    "envEnvironment": {
      "oneOf": [
        { "type": "string", "minLength": 1 },
        {
          "type": "array",
          "items": { "type": "string", "minLength": 1 }
        },
        {
          "type": "object",
          "additionalProperties": true,
          "properties": {
            "file": { "type": "string" },
            "files": {
              "type": "array",
              "items": { "type": "string", "minLength": 1 }
            },
            "sources": {
              "type": "array",
              "items": { "type": "string", "minLength": 1 }
            },
            "output": { "type": "string" },
            "outputFile": { "type": "string" },
            "strict": { "type": "boolean" }
          }
        }
      ]
    },
    "envMetadataDocument": {
      "type": "object",
      "additionalProperties": true,
      "anyOf": [
        { "required": ["variables"] },
        { "required": ["vars"] },
        { "required": ["env"] }
      ],
      "properties": {
        "variables": {
          "oneOf": [
            {
              "type": "array",
              "items": { "$ref": "#/$defs/envVariable" },
              "uniqueItems": true
            },
            {
              "type": "object",
              "additionalProperties": { "$ref": "#/$defs/envVariableMetadata" }
            }
          ]
        },
        "vars": {
          "oneOf": [
            {
              "type": "array",
              "items": { "$ref": "#/$defs/envVariable" },
              "uniqueItems": true
            },
            {
              "type": "object",
              "additionalProperties": { "$ref": "#/$defs/envVariableMetadata" }
            }
          ]
        },
        "env": {
          "type": "array",
          "items": { "$ref": "#/$defs/envVariable" },
          "uniqueItems": true
        }
      }
    },
    "envVariableMetadata": {
      "type": "object",
      "required": ["example", "encrypted", "browser"],
      "additionalProperties": true,
      "properties": {
        "example": { "type": "string" },
        "value": { "type": "string" },
        "values": { "$ref": "#/$defs/envVariableValues" },
        "encrypted": { "type": "boolean" },
        "browser": { "type": "boolean" },
        "includeInExample": { "type": "boolean" },
        "packages": {
          "oneOf": [
            { "$ref": "#/$defs/envVariablePackage" },
            {
              "type": "array",
              "items": { "$ref": "#/$defs/envVariablePackage" },
              "uniqueItems": true
            }
          ]
        },
        "includeEnv": {
          "oneOf": [
            { "type": "string", "minLength": 1 },
            {
              "type": "array",
              "items": { "type": "string", "minLength": 1 },
              "uniqueItems": true
            }
          ]
        },
        "excludeEnv": {
          "oneOf": [
            { "type": "string", "minLength": 1 },
            {
              "type": "array",
              "items": { "type": "string", "minLength": 1 },
              "uniqueItems": true
            }
          ]
        }
      }
    },
    "envVariable": {
      "type": "object",
      "required": ["key", "example", "encrypted", "browser"],
      "additionalProperties": true,
      "properties": {
        "key": {
          "type": "string",
          "pattern": "^[A-Za-z_][A-Za-z0-9_]*$"
        },
        "example": { "type": "string" },
        "value": { "type": "string" },
        "values": { "$ref": "#/$defs/envVariableValues" },
        "encrypted": { "type": "boolean" },
        "browser": { "type": "boolean" },
        "includeInExample": { "type": "boolean" },
        "packages": {
          "oneOf": [
            { "$ref": "#/$defs/envVariablePackage" },
            {
              "type": "array",
              "items": { "$ref": "#/$defs/envVariablePackage" },
              "uniqueItems": true
            }
          ]
        },
        "includeEnv": {
          "oneOf": [
            { "type": "string", "minLength": 1 },
            {
              "type": "array",
              "items": { "type": "string", "minLength": 1 },
              "uniqueItems": true
            }
          ]
        },
        "excludeEnv": {
          "oneOf": [
            { "type": "string", "minLength": 1 },
            {
              "type": "array",
              "items": { "type": "string", "minLength": 1 },
              "uniqueItems": true
            }
          ]
        }
      }
    },
    "envVariableValues": {
      "type": "object",
      "additionalProperties": { "type": "string" }
    },
    "envVariablePackage": {
      "oneOf": [
        { "type": "string", "minLength": 1 },
        {
          "type": "object",
          "required": ["package"],
          "additionalProperties": true,
          "properties": {
            "package": { "type": "string", "minLength": 1 },
            "key": {
              "type": "string",
              "pattern": "^[A-Za-z_][A-Za-z0-9_]*$"
            },
            "envKey": {
              "type": "string",
              "pattern": "^[A-Za-z_][A-Za-z0-9_]*$"
            },
            "outputKey": {
              "type": "string",
              "pattern": "^[A-Za-z_][A-Za-z0-9_]*$"
            }
          }
        }
      ]
    },
    "providerResource": {
      "type": "object",
      "required": ["type", "name"],
      "additionalProperties": true,
      "properties": {
        "type": { "type": "string", "minLength": 1 },
        "name": { "type": "string", "minLength": 1 },
        "values": {
          "type": "object",
          "additionalProperties": true
        }
      }
    },
    "vercelProjectSettings": {
      "type": "object",
      "additionalProperties": true,
      "properties": {
        "rootDirectory": { "type": ["string", "null"] },
        "nodeVersion": { "type": ["string", "null"] },
        "enableAffectedProjectsDeployments": { "type": ["boolean", "null"] },
        "protectionBypassForAutomation": {
          "oneOf": [
            { "const": false },
            { "$ref": "#/$defs/vercelProtectionBypassForAutomation" }
          ]
        }
      }
    },
    "vercelProtectionBypassForAutomation": {
      "type": "object",
      "additionalProperties": false,
      "oneOf": [
        { "required": ["ensure"] },
        { "required": ["generate"] },
        { "required": ["update"] },
        { "required": ["revoke"] }
      ],
      "properties": {
        "ensure": {
          "type": "object",
          "required": ["secret", "note"],
          "additionalProperties": false,
          "properties": {
            "secret": {
              "type": "string",
              "pattern": "^[a-zA-Z0-9]{32}$"
            },
            "note": {
              "type": "string",
              "minLength": 1,
              "maxLength": 100
            },
            "isEnvVar": { "type": "boolean" }
          }
        },
        "generate": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "secret": {
              "type": "string",
              "pattern": "^[a-zA-Z0-9]{32}$"
            },
            "note": {
              "type": "string",
              "maxLength": 100
            }
          }
        },
        "update": {
          "type": "object",
          "required": ["secret"],
          "additionalProperties": false,
          "properties": {
            "secret": { "type": "string", "minLength": 1 },
            "isEnvVar": { "type": "boolean" },
            "note": {
              "type": "string",
              "maxLength": 100
            }
          }
        },
        "revoke": {
          "type": "object",
          "required": ["secret", "regenerate"],
          "additionalProperties": false,
          "properties": {
            "secret": { "type": "string", "minLength": 1 },
            "regenerate": { "type": "boolean" }
          }
        }
      }
    },
    "app": {
      "type": "object",
      "required": ["key"],
      "additionalProperties": true,
      "properties": {
        "key": { "type": "string", "minLength": 1 },
        "id": { "type": "string" },
        "projectId": { "type": "string" },
        "name": { "type": "string" },
        "framework": { "type": "string" },
        "rootDirectory": { "type": "string" },
        "outputDir": { "type": "string" },
        "sourceKey": { "type": "string" },
        "nodeVersion": { "type": "string" },
        "enableAffectedProjectsDeployments": { "type": "boolean" },
        "protectionBypassForAutomation": {
          "oneOf": [
            { "const": false },
            { "$ref": "#/$defs/vercelProtectionBypassForAutomation" }
          ]
        },
        "domains": {
          "type": "array",
          "items": { "$ref": "#/$defs/domain" }
        },
        "env": {
          "type": "object",
          "additionalProperties": true,
          "properties": {
            "sourceKey": { "type": "string" },
            "outputDir": { "type": "string" },
            "examplePath": { "type": "string" },
            "exampleOutput": { "type": "string" },
            "sharedPrefix": { "type": "string" }
          }
        },
        "providers": {
          "type": "object",
          "additionalProperties": true
        }
      }
    },
    "package": {
      "type": "object",
      "required": ["key", "directory"],
      "additionalProperties": true,
      "properties": {
        "key": { "type": "string", "minLength": 1 },
        "name": { "type": "string", "minLength": 1 },
        "directory": { "type": "string", "minLength": 1 },
        "dir": { "type": "string", "minLength": 1 },
        "path": { "type": "string", "minLength": 1 },
        "rootDirectory": { "type": "string", "minLength": 1 },
        "outputDir": { "type": "string" },
        "sourceKey": { "type": "string" },
        "env": {
          "type": "object",
          "additionalProperties": true,
          "properties": {
            "sourceKey": { "type": "string" },
            "outputDir": { "type": "string" },
            "examplePath": { "type": "string" },
            "exampleOutput": { "type": "string" },
            "sharedPrefix": { "type": "string" }
          }
        }
      }
    },
    "domain": {
      "oneOf": [
        { "type": "string", "minLength": 1 },
        {
          "type": "object",
          "required": ["name"],
          "additionalProperties": true,
          "properties": {
            "name": { "type": "string", "minLength": 1 },
            "app": { "type": "string" },
            "project": { "type": "string" },
            "key": { "type": "string" },
            "gitBranch": { "type": ["string", "null"] },
            "verified": { "type": "boolean" }
          }
        }
      ]
    },
    "keyedResource": {
      "type": "object",
      "required": ["key"],
      "additionalProperties": true,
      "properties": {
        "key": { "type": "string", "minLength": 1 },
        "name": { "type": "string" },
        "providers": {
          "type": "object",
          "additionalProperties": true
        }
      }
    },
    "githubActionsEnvEntry": {
      "oneOf": [
        { "type": "string", "minLength": 1 },
        {
          "type": "object",
          "additionalProperties": true,
          "properties": {
            "source": { "type": "string", "minLength": 1 },
            "from": { "type": "string", "minLength": 1 },
            "key": { "type": "string", "minLength": 1 },
            "name": { "type": "string", "minLength": 1 },
            "env": { "type": "string", "minLength": 1 },
            "secret": { "type": "boolean" }
          }
        }
      ]
    }
  }
}
