#!/bin/bash
# Multi-Tenant Dobby Setup Script
# Run this on the Vultr VPS to initialize the directory structure
set -e

BASE_DIR="/mnt/data/dobby"

echo "=== Multi-Tenant Dobby Setup ==="
echo "Base directory: $BASE_DIR"

# Create tenant directories
for i in 1 2 3; do
    echo "Creating directories for tenant${i}..."
    mkdir -p "$BASE_DIR/tenant${i}/data/results"
    mkdir -p "$BASE_DIR/tenant${i}/data/logs"
    mkdir -p "$BASE_DIR/tenant${i}/claude"
done

# Clone dobby repo if not present
if [ ! -d "$BASE_DIR/repo" ]; then
    echo "Cloning dobby repository..."
    git clone https://github.com/simonucl/dobby.git "$BASE_DIR/repo"
else
    echo "Updating dobby repository..."
    cd "$BASE_DIR/repo" && git pull
fi

cd "$BASE_DIR/repo/deploy/multi-tenant"

# Create env files from examples if they don't exist
for i in 1 2 3; do
    if [ ! -f "tenant${i}.env" ]; then
        cp "tenant${i}.env.example" "tenant${i}.env"
        echo "Created tenant${i}.env (fill in your tokens)"
    else
        echo "tenant${i}.env already exists, skipping"
    fi
done

echo ""
echo "=== Setup Complete ==="
echo ""
echo "Next steps:"
echo "  1. Edit tenant env files with Slack tokens and API keys:"
echo "     nano $BASE_DIR/repo/deploy/multi-tenant/tenant1.env"
echo "     nano $BASE_DIR/repo/deploy/multi-tenant/tenant2.env"
echo "     nano $BASE_DIR/repo/deploy/multi-tenant/tenant3.env"
echo ""
echo "  2. Build and start containers:"
echo "     cd $BASE_DIR/repo/deploy/multi-tenant"
echo "     docker compose build"
echo "     docker compose up -d"
echo ""
echo "  3. Authenticate Claude Code in each container:"
echo "     docker exec -it dobby-tenant1 claude"
echo "     docker exec -it dobby-tenant2 claude"
echo "     docker exec -it dobby-tenant3 claude"
echo ""
echo "  4. Set up Cloudflare Tunnel (see cloudflared-config.yml.example)"
echo ""
echo "  5. Connect spokes from local machines:"
echo "     dobby agent --hub https://tenant1.dobby.example.com --id my-mac"
