{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Hypervisor Swarm Integration Schema",
  "description": "Schema for hypervisor swarm integration configuration",
  "type": "object",
  "properties": {
    "apiVersion": {
      "type": "string",
      "pattern": "^v[0-9]+$",
      "default": "v1"
    },
    "kind": {
      "type": "string",
      "enum": ["HypervisorSwarmIntegration"],
      "default": "HypervisorSwarmIntegration"
    },
    "metadata": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1
        },
        "namespace": {
          "type": "string",
          "minLength": 1
        },
        "description": {
          "type": "string"
        },
        "created": {
          "type": "string",
          "format": "date-time"
        },
        "updated": {
          "type": "string",
          "format": "date-time"
        }
      },
      "required": ["name", "namespace"]
    },
    "spec": {
      "type": "object",
      "properties": {
        "swarm": {
          "type": "object",
          "properties": {
            "manager_url": {
              "type": "string",
              "format": "uri",
              "pattern": "^http://.*"
            },
            "manager_token": {
              "type": "string",
              "pattern": "^SWMTKN-.*"
            },
            "health_enabled": {
              "type": "boolean",
              "default": true
            },
            "join_token": {
              "type": "string",
              "minLength": 10
            },
            "docker_host": {
              "type": "string",
              "format": "uri"
            },
            "context_name": {
              "type": "string",
              "minLength": 1
            },
            "expose_join_token": {
              "type": "boolean",
              "default": false
            }
          },
          "required": ["manager_url", "manager_token", "join_token", "docker_host", "context_name"]
        },
        "lxc": {
          "type": "object",
          "properties": {
            "container_name": {
              "type": "string",
              "minLength": 1
            },
            "host": {
              "type": "string",
              "minLength": 1
            },
            "network": {
              "type": "object",
              "properties": {
                "type": {
                  "type": "string",
                  "enum": ["bridged", "macvlan", "ipvlan"]
                },
                "bridge": {
                  "type": "string"
                },
                "ipv4_address": {
                  "type": "string",
                  "format": "ipv4"
                },
                "ipv4_gateway": {
                  "type": "string",
                  "format": "ipv4"
                }
              },
              "required": ["type", "ipv4_address", "ipv4_gateway"]
            },
            "resources": {
              "type": "object",
              "properties": {
                "cpu": {
                  "type": "integer",
                  "minimum": 1
                },
                "memory": {
                  "type": "string",
                  "pattern": "^[0-9]+(GB|MB)$"
                },
                "storage": {
                  "type": "string",
                  "pattern": "^[0-9]+(GB|MB)$"
                }
              },
              "required": ["cpu", "memory", "storage"]
            }
          },
          "required": ["container_name", "host", "network", "resources"]
        },
        "modules": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "name": {
                "type": "string",
                "minLength": 1
              },
              "type": {
                "type": "string",
                "minLength": 1
              },
              "endpoint": {
                "type": "string",
                "format": "uri"
              },
              "database": {
                "type": "object",
                "properties": {
                  "type": {
                    "type": "string",
                    "enum": ["mongodb", "postgresql", "mysql"]
                  },
                  "container": {
                    "type": "string",
                    "minLength": 1
                  },
                  "port": {
                    "type": "integer",
                    "minimum": 1024,
                    "maximum": 65535
                  },
                  "auth": {
                    "type": "object",
                    "properties": {
                      "enabled": {
                        "type": "boolean",
                        "default": false
                      },
                      "username": {
                        "type": "string"
                      },
                      "password": {
                        "type": "string"
                      }
                    }
                  }
                },
                "required": ["type", "container", "port"]
              },
              "auth": {
                "type": "object",
                "properties": {
                  "sso_enabled": {
                    "type": "boolean",
                    "default": false
                  },
                  "provider": {
                    "type": "string",
                    "enum": ["keycloak", "auth0", "okta"]
                  },
                  "realm": {
                    "type": "string"
                  },
                  "client_id": {
                    "type": "string"
                  }
                }
              }
            },
            "required": ["name", "type", "endpoint"]
          }
        },
        "network": {
          "type": "object",
          "properties": {
            "vpn": {
              "type": "object",
              "properties": {
                "enabled": {
                  "type": "boolean",
                  "default": false
                },
                "type": {
                  "type": "string",
                  "enum": ["wireguard", "openvpn", "ipsec"]
                },
                "config": {
                  "type": "object",
                  "properties": {
                    "interface": {
                      "type": "string"
                    },
                    "port": {
                      "type": "integer",
                      "minimum": 1,
                      "maximum": 65535
                    },
                    "private_key": {
                      "type": "string"
                    },
                    "peers": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "public_key": {
                            "type": "string"
                          },
                          "endpoint": {
                            "type": "string"
                          },
                          "allowed_ips": {
                            "type": "string"
                          }
                        },
                        "required": ["public_key", "endpoint", "allowed_ips"]
                      }
                    }
                  }
                }
              },
              "required": ["enabled", "type"]
            },
            "overlay": {
              "type": "object",
              "properties": {
                "name": {
                  "type": "string"
                },
                "driver": {
                  "type": "string",
                  "enum": ["overlay", "bridge", "host"]
                },
                "subnet": {
                  "type": "string",
                  "format": "ipv4"
                },
                "encrypt": {
                  "type": "boolean",
                  "default": false
                }
              },
              "required": ["name", "driver", "subnet"]
            }
          },
          "required": ["vpn", "overlay"]
        },
        "security": {
          "type": "object",
          "properties": {
            "sso": {
              "type": "object",
              "properties": {
                "enabled": {
                  "type": "boolean",
                  "default": false
                },
                "provider": {
                  "type": "string",
                  "enum": ["keycloak", "auth0", "okta"]
                },
                "realm": {
                  "type": "string"
                },
                "url": {
                  "type": "string",
                  "format": "uri"
                },
                "admin_username": {
                  "type": "string"
                },
                "admin_password": {
                  "type": "string"
                }
              },
              "required": ["enabled", "provider", "realm", "url"]
            },
            "certificates": {
              "type": "object",
              "properties": {
                "ca_cert": {
                  "type": "string"
                },
                "server_cert": {
                  "type": "string"
                },
                "server_key": {
                  "type": "string"
                }
              },
              "required": ["ca_cert", "server_cert", "server_key"]
            },
            "authentication": {
              "type": "object",
              "properties": {
                "jwt": {
                  "type": "object",
                  "properties": {
                    "enabled": {
                      "type": "boolean",
                      "default": false
                    },
                    "secret": {
                      "type": "string"
                    },
                    "expiration": {
                      "type": "string"
                    }
                  },
                  "required": ["enabled", "secret"]
                },
                "api_keys": {
                  "type": "object",
                  "properties": {
                    "enabled": {
                      "type": "boolean",
                      "default": false
                    },
                    "master_key": {
                      "type": "string"
                    }
                  },
                  "required": ["enabled", "master_key"]
                }
              }
            }
          },
          "required": ["sso", "certificates", "authentication"]
        },
        "data_models": {
          "type": "object",
          "properties": {
            "meta_heuristic": {
              "type": "object",
              "properties": {
                "enabled": {
                  "type": "boolean",
                  "default": false
                },
                "ingestors": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "properties": {
                      "name": {
                        "type": "string"
                      },
                      "type": {
                        "type": "string"
                      },
                      "source": {
                        "type": "string"
                      },
                      "target": {
                        "type": "string"
                      },
                      "sync_interval": {
                        "type": "string"
                      }
                    },
                    "required": ["name", "type", "source", "target"]
                  }
                },
                "schema_mapping": {
                  "type": "object",
                  "additionalProperties": {
                    "type": "string"
                  }
                }
              },
              "required": ["enabled"]
            }
          },
          "required": ["meta_heuristic"]
        },
        "monitoring": {
          "type": "object",
          "properties": {
            "infrastructure": {
              "type": "object",
              "properties": {
                "enabled": {
                  "type": "boolean",
                  "default": false
                },
                "tools": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "properties": {
                      "name": {
                        "type": "string",
                        "enum": ["prometheus", "grafana", "zabbix"]
                      },
                      "port": {
                        "type": "integer",
                        "minimum": 1024,
                        "maximum": 65535
                      },
                      "scrape_interval": {
                        "type": "string"
                      },
                      "admin_password": {
                        "type": "string"
                      }
                    },
                    "required": ["name", "port"]
                  }
                }
              },
              "required": ["enabled"]
            },
            "hypergraph": {
              "type": "object",
              "properties": {
                "enabled": {
                  "type": "boolean",
                  "default": false
                },
                "metrics": {
                  "type": "array",
                  "items": {
                    "type": "string"
                  }
                }
              },
              "required": ["enabled"]
            }
          },
          "required": ["infrastructure", "hypergraph"]
        },
        "public_endpoint": {
          "type": "object",
          "properties": {
            "enabled": {
              "type": "boolean",
              "default": false
            },
            "host": {
              "type": "string",
              "format": "hostname"
            },
            "port": {
              "type": "integer",
              "minimum": 1,
              "maximum": 65535
            },
            "ssl": {
              "type": "object",
              "properties": {
                "enabled": {
                  "type": "boolean",
                  "default": false
                },
                "cert": {
                  "type": "string"
                },
                "key": {
                  "type": "string"
                }
              },
              "required": ["enabled"]
            },
            "hub": {
              "type": "object",
              "properties": {
                "enabled": {
                  "type": "boolean",
                  "default": false
                },
                "path": {
                  "type": "string"
                },
                "authentication": {
                  "type": "string",
                  "enum": ["jwt", "api_key", "none"]
                }
              },
              "required": ["enabled"]
            },
            "install_scripts": {
              "type": "object",
              "properties": {
                "endpoint": {
                  "type": "string"
                },
                "auth_required": {
                  "type": "boolean",
                  "default": false
                },
                "allowed_ips": {
                  "type": "array",
                  "items": {
                    "type": "string",
                    "format": "ipv4"
                  }
                }
              },
              "required": ["endpoint"]
            }
          },
          "required": ["enabled", "host", "port"]
        },
        "automation": {
          "type": "object",
          "properties": {
            "config_generation": {
              "type": "object",
              "properties": {
                "enabled": {
                  "type": "boolean",
                  "default": false
                },
                "output_dir": {
                  "type": "string"
                },
                "templates": {
                  "type": "array",
                  "items": {
                    "type": "string"
                  }
                }
              },
              "required": ["enabled"]
            },
            "token_generation": {
              "type": "object",
              "properties": {
                "enabled": {
                  "type": "boolean",
                  "default": false
                },
                "types": {
                  "type": "array",
                  "items": {
                    "type": "string",
                    "enum": ["swarm_join", "api_access", "database_auth", "vpn_access"]
                  }
                },
                "expiration": {
                  "type": "string"
                }
              },
              "required": ["enabled"]
            },
            "handshake": {
              "type": "object",
              "properties": {
                "enabled": {
                  "type": "boolean",
                  "default": false
                },
                "timeout": {
                  "type": "string"
                },
                "retries": {
                  "type": "integer",
                  "minimum": 1,
                  "maximum": 10
                },
                "ping_interval": {
                  "type": "string"
                }
              },
              "required": ["enabled"]
            },
            "vpn_fallback": {
              "type": "object",
              "properties": {
                "enabled": {
                  "type": "boolean",
                  "default": false
                },
                "threshold": {
                  "type": "string"
                }
              },
              "required": ["enabled"]
            }
          },
          "required": ["config_generation", "token_generation", "handshake", "vpn_fallback"]
        },
        "integration": {
          "type": "object",
          "properties": {
            "factory_app": {
              "type": "object",
              "properties": {
                "enabled": {
                  "type": "boolean",
                  "default": false
                },
                "endpoint": {
                  "type": "string",
                  "format": "uri"
                },
                "api_version": {
                  "type": "string"
                }
              },
              "required": ["enabled", "endpoint"]
            },
            "exosys": {
              "type": "object",
              "properties": {
                "enabled": {
                  "type": "boolean",
                  "default": false
                },
                "endpoint": {
                  "type": "string",
                  "format": "uri"
                },
                "api_version": {
                  "type": "string"
                }
              },
              "required": ["enabled", "endpoint"]
            },
            "hypergraph_webapp": {
              "type": "object",
              "properties": {
                "enabled": {
                  "type": "boolean",
                  "default": false
                },
                "endpoint": {
                  "type": "string",
                  "format": "uri"
                },
                "api_version": {
                  "type": "string"
                }
              },
              "required": ["enabled", "endpoint"]
            }
          },
          "required": ["factory_app", "exosys", "hypergraph_webapp"]
        }
      },
      "required": [
        "swarm",
        "lxc",
        "modules",
        "network",
        "security",
        "data_models",
        "monitoring",
        "public_endpoint",
        "automation",
        "integration"
      ]
    },
    "status": {
      "type": "object",
      "properties": {
        "phase": {
          "type": "string",
          "enum": ["pending", "initializing", "running", "failed", "completed"]
        },
        "last_updated": {
          "type": "string",
          "format": "date-time"
        },
        "conditions": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "type": {
                "type": "string"
              },
              "status": {
                "type": "string",
                "enum": ["True", "False", "Unknown"]
              },
              "reason": {
                "type": "string"
              },
              "message": {
                "type": "string"
              }
            },
            "required": ["type", "status"]
          }
        }
      },
      "required": ["phase", "last_updated"]
    }
  },
  "required": ["apiVersion", "kind", "metadata", "spec", "status"]
}