#!/bin/bash
# vPods Graph Cluster Initialization Script
# Creates the .mcpzero directory structure and initializes components

set -e
echo "Initializing vPods Graph Cluster environment..."

# Create base directory structure
mkdir -p .mcpzero/lock
mkdir -p .mcpzero/contracts
mkdir -p .mcpzero/terraform
mkdir -p .mcpzero/db/graph
mkdir -p .mcpzero/db/shards
mkdir -p .mcpzero/db/indices

echo "Created .mcpzero directory structure"

# Create initial lock files
cat > .mcpzero/lock/knowledge.lock << EOF
{
  "metadata": {
    "version": "1.0.0",
    "timestamp": "$(date -u +"%Y-%m-%dT%H:%M:%SZ")",
    "signature": "ed25519:$(openssl rand -hex 16)"
  },
  "content": {
    "knowledge_type": "base",
    "offline_first": true,
    "permanent_local_switch": true
  },
  "verification": {
    "hash": "sha3-512:$(openssl rand -hex 32)",
    "previous_hash": null,
    "merkle_root": null
  }
}
EOF

cat > .mcpzero/lock/communication.lock << EOF
{
  "metadata": {
    "version": "1.0.0",
    "timestamp": "$(date -u +"%Y-%m-%dT%H:%M:%SZ")",
    "signature": "ed25519:$(openssl rand -hex 16)"
  },
  "content": {
    "protocol": "mcp-zero",
    "offline_first": true,
    "retry_limit": 1
  },
  "verification": {
    "hash": "sha3-512:$(openssl rand -hex 32)",
    "previous_hash": null,
    "merkle_root": null
  }
}
EOF

echo "Created initial lock files"

# Copy ethics contract
cp mlops/contracts/ethics.contract .mcpzero/contracts/

# Make lock files read-only
chmod 444 .mcpzero/lock/*.lock
chmod 444 .mcpzero/contracts/*.contract

echo "vPods environment initialized successfully"
