{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://schemas.aiwg.io/deploy/flow-domain-static-site-plan.v1.schema.json",
  "title": "Flow domain static site deployment plan",
  "type": "object",
  "additionalProperties": false,
  "required": ["apiVersion", "kind", "metadata", "spec"],
  "properties": {
    "apiVersion": { "const": "flow.aiwg.io/v1alpha1" },
    "kind": { "const": "FlowDomainStaticDeploymentPlan" },
    "metadata": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "issue", "approval"],
      "properties": {
        "name": { "type": "string", "minLength": 1 },
        "issue": { "const": "#2125" },
        "approval": {
          "type": "object",
          "additionalProperties": false,
          "required": ["status", "authority", "authorizedAt"],
          "properties": {
            "status": { "const": "approved" },
            "authority": { "type": "string", "minLength": 1 },
            "authorizedAt": { "type": "string", "format": "date" }
          }
        }
      }
    },
    "spec": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "canonicalHostname",
        "hostnamePolicy",
        "repositories",
        "sites",
        "isolatedContainerPolicy",
        "configuration",
        "operatorHandoff"
      ],
      "properties": {
        "canonicalHostname": { "type": "string", "format": "hostname" },
        "hostnamePolicy": {
          "type": "object",
          "additionalProperties": false,
          "required": ["zone", "maximumLabels", "graphContentPath", "fourthLevelDnsAllowed"],
          "properties": {
            "zone": { "const": "aiwg.io" },
            "maximumLabels": { "const": 3 },
            "graphContentPath": { "const": "/graph/" },
            "fourthLevelDnsAllowed": { "const": false }
          }
        },
        "repositories": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/repository" }
        },
        "sites": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/site" }
        },
        "isolatedContainerPolicy": {
          "type": "object",
          "additionalProperties": false,
          "required": ["selectedSites", "portRange", "portRegistry", "templateRoot"],
          "properties": {
            "selectedSites": { "type": "array", "items": { "type": "string" } },
            "portRange": {
              "type": "object",
              "additionalProperties": false,
              "required": ["minimum", "maximum"],
              "properties": {
                "minimum": { "type": "integer", "minimum": 1024, "maximum": 65535 },
                "maximum": { "type": "integer", "minimum": 1024, "maximum": 65535 }
              }
            },
            "portRegistry": {
              "type": "array",
              "items": {
                "type": "object",
                "additionalProperties": false,
                "required": ["site", "localhostPort", "tunnelOrigin"],
                "properties": {
                  "site": { "type": "string" },
                  "localhostPort": { "type": "integer", "minimum": 1024, "maximum": 65535 },
                  "tunnelOrigin": { "type": "string", "pattern": "^http://127\\.0\\.0\\.1:[0-9]+$" }
                }
              }
            },
            "templateRoot": { "type": "string", "minLength": 1 }
          }
        },
        "configuration": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "requiredSecretNames",
            "optionalCachePurgeSecretNames",
            "requiredVariableNames",
            "secretValuesNeverStored"
          ],
          "properties": {
            "requiredSecretNames": { "type": "array", "items": { "type": "string" }, "minItems": 1 },
            "optionalCachePurgeSecretNames": { "type": "array", "items": { "type": "string" }, "minItems": 2 },
            "requiredVariableNames": { "type": "array", "items": { "type": "string" }, "minItems": 1 },
            "secretValuesNeverStored": { "const": true }
          }
        },
        "operatorHandoff": {
          "type": "object",
          "additionalProperties": false,
          "required": ["dnsRoutes", "tunnelRoutes", "originPorts", "changeOrder"],
          "properties": {
            "dnsRoutes": { "type": "array", "minItems": 1, "items": { "$ref": "#/$defs/dnsRoute" } },
            "tunnelRoutes": { "type": "array", "minItems": 1, "items": { "$ref": "#/$defs/tunnelRoute" } },
            "originPorts": {
              "type": "array",
              "minItems": 1,
              "items": {
                "type": "object",
                "additionalProperties": false,
                "required": ["hostname", "binding", "port", "owner"],
                "properties": {
                  "hostname": { "type": "string", "format": "hostname" },
                  "binding": { "enum": ["shared-host", "localhost"] },
                  "port": { "type": "integer", "minimum": 1, "maximum": 65535 },
                  "owner": { "type": "string", "minLength": 1 }
                }
              }
            },
            "changeOrder": { "type": "array", "minItems": 4, "items": { "type": "string", "minLength": 1 } }
          }
        }
      }
    }
  },
  "$defs": {
    "repository": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "owner", "name", "visibility", "state", "initialization", "build", "workflowTarget", "workflowTemplate"],
      "properties": {
        "id": { "type": "string", "minLength": 1 },
        "owner": { "const": "roctinam" },
        "name": { "type": "string", "minLength": 1 },
        "visibility": { "const": "private" },
        "state": { "enum": ["approved-not-created", "existing"] },
        "initialization": {
          "type": "object",
          "additionalProperties": false,
          "required": ["template", "initialContent"],
          "properties": {
            "template": { "enum": ["static-docs", "generated-landing-page"] },
            "initialContent": { "type": "array", "minItems": 1, "items": { "type": "string" } }
          }
        },
        "build": {
          "type": "object",
          "additionalProperties": false,
          "required": ["installCommand", "command", "outputDirectory"],
          "properties": {
            "installCommand": { "type": "string", "minLength": 1 },
            "command": { "type": "string", "minLength": 1 },
            "outputDirectory": { "type": "string", "minLength": 1 }
          }
        },
        "workflowTarget": { "const": ".gitea/workflows/deploy.yml" },
        "workflowTemplate": { "type": "string", "minLength": 1 }
      }
    },
    "site": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "hostname", "repository", "paths", "deployment", "dns", "tunnel"],
      "properties": {
        "id": { "type": "string", "minLength": 1 },
        "hostname": { "type": "string", "format": "hostname" },
        "repository": { "type": "string", "minLength": 1 },
        "paths": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["path", "purpose"],
            "properties": {
              "path": { "type": "string", "pattern": "^/" },
              "purpose": { "type": "string", "minLength": 1 }
            }
          }
        },
        "deployment": {
          "type": "object",
          "additionalProperties": false,
          "required": ["pattern", "hostPath", "containerPath", "origin", "caddyTemplate", "composeTemplate"],
          "properties": {
            "pattern": { "enum": ["shared-serve-static", "per-site-container"] },
            "hostPath": { "type": "string", "pattern": "^/home/roctinam/production-deploy/" },
            "containerPath": { "type": "string", "pattern": "^/srv/" },
            "origin": {
              "type": "object",
              "additionalProperties": false,
              "required": ["scheme", "host", "port", "service"],
              "properties": {
                "scheme": { "const": "http" },
                "host": { "enum": ["127.0.0.1", "localhost"] },
                "port": { "type": "integer", "minimum": 1, "maximum": 65535 },
                "service": { "type": "string", "minLength": 1 }
              }
            },
            "caddyTemplate": { "type": "string", "minLength": 1 },
            "composeTemplate": { "type": "string", "minLength": 1 }
          }
        },
        "dns": { "$ref": "#/$defs/dnsRoute" },
        "tunnel": { "$ref": "#/$defs/tunnelRoute" }
      }
    },
    "dnsRoute": {
      "type": "object",
      "additionalProperties": false,
      "required": ["hostname", "recordType", "targetSource", "proxied"],
      "properties": {
        "hostname": { "type": "string", "format": "hostname" },
        "recordType": { "const": "CNAME" },
        "targetSource": { "const": "approved Cloudflare tunnel CNAME target" },
        "proxied": { "const": true }
      }
    },
    "tunnelRoute": {
      "type": "object",
      "additionalProperties": false,
      "required": ["hostname", "origin", "accessPolicyReview"],
      "properties": {
        "hostname": { "type": "string", "format": "hostname" },
        "origin": { "type": "string", "pattern": "^http://(127\\.0\\.0\\.1|localhost):[0-9]+$" },
        "accessPolicyReview": { "const": "required-before-change" }
      }
    }
  }
}
