#!/usr/bin/env bash
# chelsty-runtime.sh - Bootstrap script for CHELSTY edge node runtime

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
RUNTIME_DIR="/opt/homelab"
CHELSTY_CONFIG="$REPO_ROOT/hosts/chelsty/runtime"

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

log() { echo -e "${GREEN}[INFO]${NC} $1"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
error() { echo -e "${RED}[ERROR]${NC} $1"; exit 1; }

log "Starting CHELSTY runtime bootstrap..."

# 1. Validate Docker availability
if ! command -v docker &> /dev/null; then
    error "Docker is not installed. Please install Docker first."
fi

# 2. Validate compose plugin
if ! docker compose version &> /dev/null; then
    error "Docker Compose plugin is not installed."
fi

log "Docker and Compose plugin verified."

# 3. Create runtime directories
log "Creating runtime directories in $RUNTIME_DIR..."
sudo mkdir -p "$RUNTIME_DIR/data/mosquitto/config" \
             "$RUNTIME_DIR/data/mosquitto/data" \
             "$RUNTIME_DIR/data/mosquitto/log" \
             "$RUNTIME_DIR/data/zigbee2mqtt/data" \
             "$RUNTIME_DIR/config/mosquitto" \
             "$RUNTIME_DIR/config/zigbee2mqtt"

# 4. Copy runtime templates if absent
log "Deploying runtime configurations..."
# Mosquitto
if [ ! -f "$RUNTIME_DIR/config/mosquitto/mosquitto.conf" ]; then
    sudo cp "$CHELSTY_CONFIG/mosquitto/mosquitto.conf" "$RUNTIME_DIR/config/mosquitto/"
    log "Copied mosquitto.conf"
fi

if [ ! -f "$RUNTIME_DIR/config/mosquitto/docker-compose.override.yml" ]; then
    sudo cp "$CHELSTY_CONFIG/mosquitto/docker-compose.override.yml" "$RUNTIME_DIR/config/mosquitto/"
fi

# Zigbee2MQTT
if [ ! -f "$RUNTIME_DIR/config/zigbee2mqtt/configuration.yaml" ]; then
    sudo cp "$CHELSTY_CONFIG/zigbee2mqtt/configuration.yaml" "$RUNTIME_DIR/config/zigbee2mqtt/"
    log "Copied zigbee2mqtt configuration.yaml"
fi

if [ ! -f "$RUNTIME_DIR/config/zigbee2mqtt/docker-compose.override.yml" ]; then
    sudo cp "$CHELSTY_CONFIG/zigbee2mqtt/docker-compose.override.yml" "$RUNTIME_DIR/config/zigbee2mqtt/"
fi

# 5. Create missing .env files from examples
log "Checking for environment files..."
if [ ! -f "$RUNTIME_DIR/config/zigbee2mqtt/.env" ]; then
    warn "Creating template .env for Zigbee2MQTT. PLEASE EDIT IT!"
    echo "MQTT_USER=admin" | sudo tee "$RUNTIME_DIR/config/zigbee2mqtt/.env" > /dev/null
    echo "MQTT_PASSWORD=password" | sudo tee -a "$RUNTIME_DIR/config/zigbee2mqtt/.env" > /dev/null
fi

# 6. Ensure password file exists for Mosquitto
if [ ! -f "$RUNTIME_DIR/data/mosquitto/config/password.txt" ]; then
    log "Creating empty mosquitto password file..."
    sudo touch "$RUNTIME_DIR/data/mosquitto/config/password.txt"
    warn "Mosquitto password file is empty. Use 'mosquitto_passwd' to add users."
fi

log "Bootstrap complete!"

echo -e "\n${YELLOW}Next-step instructions:${NC}"
echo "1. Edit $RUNTIME_DIR/config/zigbee2mqtt/.env with real credentials."
echo "2. Add Mosquitto user: sudo mosquitto_passwd -b $RUNTIME_DIR/data/mosquitto/config/password.txt <user> <password>"
echo "3. Deploy services using the homelab deployment framework:"
echo "   ./scripts/deploy/deploy-node.sh chelsty"
echo "4. Verify Zigbee2MQTT logs to ensure it connects to SLZB-06U."
echo "5. Check Home Assistant (separate VM) for MQTT discovery."
