#!/bin/bash

# AITBC Services Deployment to Container
# Copy to deploy-to-container.sh and adjust variables for your environment

set -e

# === CONFIGURE THESE ===
CONTAINER_NAME="aitbc"
CONTAINER_IP="YOUR_CONTAINER_IP"
PROJECT_DIR="/path/to/your/aitbc"
SSH_ALIAS="your-ssh-alias"  # or user@host

echo "🚀 Deploying AITBC services to container: $CONTAINER_NAME"
echo "Container IP: $CONTAINER_IP"
echo ""

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

# 1. Push website
echo -e "${YELLOW}Deploying website...${NC}"
scp -r "$PROJECT_DIR/website/index.html" "$PROJECT_DIR/website/404.html" \
    "$SSH_ALIAS:/var/www/html/"
scp -r "$PROJECT_DIR/website/docs/"*.html "$SSH_ALIAS:/var/www/html/docs/"
scp "$PROJECT_DIR/website/docs/css/docs.css" "$SSH_ALIAS:/var/www/html/docs/css/"
scp "$PROJECT_DIR/website/docs/js/theme.js" "$SSH_ALIAS:/var/www/html/docs/js/"
echo -e "${GREEN}✓ Website deployed${NC}"

# 2. Deploy coordinator API
echo -e "${YELLOW}Deploying coordinator API...${NC}"
ssh "$SSH_ALIAS" "cd /opt/aitbc && pip install -e apps/coordinator-api/"
ssh "$SSH_ALIAS" "systemctl restart aitbc-coordinator"
echo -e "${GREEN}✓ Coordinator deployed${NC}"

# 3. Deploy blockchain node
echo -e "${YELLOW}Deploying blockchain node...${NC}"
ssh "$SSH_ALIAS" "cd /opt/aitbc && pip install -e apps/blockchain-node/"
ssh "$SSH_ALIAS" "systemctl restart aitbc-blockchain"
echo -e "${GREEN}✓ Blockchain node deployed${NC}"

# 4. Verify
echo ""
echo -e "${YELLOW}Verifying services...${NC}"
ssh "$SSH_ALIAS" "curl -s http://127.0.0.1:8000/v1/health | head -1"
ssh "$SSH_ALIAS" "curl -s http://127.0.0.1:9080/rpc/health | head -1"
echo -e "${GREEN}✓ All services running${NC}"
