#!/usr/bin/env bash
# Deploy remaining LXC containers for services
# Usage: ./deploy-remaining-containers.sh

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
PROXMOX_HOST="${PROXMOX_HOST:-192.168.11.10}"
PROXMOX_PASS="${PROXMOX_PASS:-L@kers2010}"

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

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }

ssh_proxmox() {
    sshpass -p "$PROXMOX_PASS" ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 root@"$PROXMOX_HOST" "$@"
}

log_info "========================================="
log_info "Deploy Remaining Containers"
log_info "========================================="
log_info ""
log_warn "This script requires root access on Proxmox host"
log_warn "Containers will be deployed using deployment scripts"
log_info ""

# Check which containers exist
log_info "Checking existing containers..."
EXISTING=$(ssh_proxmox "pct list | grep -E '(3500|3501|3502|3503|150|151|5000|5200|6000|6200|6400)' | awk '{print \$1}'" 2>&1)

log_info "Existing containers: $EXISTING"

# Containers to deploy
CONTAINERS=(
    "3502:keeper:Keeper Service"
    "3503:tokenization:Financial Tokenization Service"
    "150:firefly:Hyperledger Firefly"
    "151:cacti:Hyperledger Cacti"
    "5000:blockscout:Blockscout Explorer"
    "5200:prometheus:Prometheus Monitoring"
    "6000:grafana:Grafana Dashboard"
    "6200:loki:Loki Logging"
    "6400:alertmanager:Alertmanager"
)

log_info ""
log_info "Containers to deploy:"
for container in "${CONTAINERS[@]}"; do
    IFS=':' read -r vmid service name <<< "$container"
    if echo "$EXISTING" | grep -q "^$vmid$"; then
        log_info "  - $name (VMID $vmid): ✅ Already exists"
    else
        log_info "  - $name (VMID $vmid): ⏳ Pending"
    fi
done

log_info ""
log_warn "Container deployment requires:"
log_warn "  1. Root access on Proxmox host"
log_warn "  2. Deployment scripts in smom-dbis-138-proxmox/scripts/deployment/"
log_warn "  3. Installation scripts in smom-dbis-138-proxmox/install/"
log_info ""
log_info "To deploy containers, run on Proxmox host:"
log_info "  cd /home/intlc/projects/proxmox"
log_info "  bash smom-dbis-138-proxmox/scripts/deployment/deploy-services.sh"
log_info ""
log_info "Or use the Proxmox web UI to create containers manually"
log_info ""

