#!/usr/bin/env bash
# =============================================================================
# Bunker Ops — Scaffold a New Instance
# Creates host_vars directory with main.yml + vault.yml for a new instance
#
# Usage: ./scripts/add-instance.sh <hostname> <domain> <ssh-host> [--tier 0|1|2]
# =============================================================================
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
INVENTORY_DIR="${PROJECT_DIR}/inventory/host_vars"

# --- Parse args ---
HOSTNAME="${1:-}"
DOMAIN="${2:-}"
SSH_HOST="${3:-}"
TIER=1

shift 3 2>/dev/null || true
while [[ $# -gt 0 ]]; do
  case "$1" in
    --tier) TIER="$2"; shift 2 ;;
    *) echo "Unknown option: $1"; exit 1 ;;
  esac
done

if [[ -z "$HOSTNAME" ]] || [[ -z "$DOMAIN" ]] || [[ -z "$SSH_HOST" ]]; then
  echo "Usage: $0 <hostname> <domain> <ssh-host> [--tier 0|1|2]"
  echo ""
  echo "Arguments:"
  echo "  hostname   Ansible inventory name (e.g., edmonton-prod)"
  echo "  domain     Instance domain (e.g., betteredmonton.org)"
  echo "  ssh-host   SSH address (e.g., 10.0.1.10 or user@host)"
  echo ""
  echo "Options:"
  echo "  --tier N   Bunker Ops tier (0=standalone, 1=managed, 2=fleet)"
  echo ""
  echo "Examples:"
  echo "  $0 edmonton-prod betteredmonton.org 10.0.1.10"
  echo "  $0 calgary-staging staging.bettercalgary.org deploy@10.0.2.20 --tier 2"
  exit 1
fi

HOST_DIR="${INVENTORY_DIR}/${HOSTNAME}"

if [[ -d "$HOST_DIR" ]]; then
  echo "ERROR: Host directory already exists: ${HOST_DIR}"
  echo "Remove it first if you want to recreate."
  exit 1
fi

# --- Determine Bunker Ops settings ---
BUNKER_OPS_ENABLED="false"
if [[ "$TIER" == "2" ]]; then
  BUNKER_OPS_ENABLED="true"
fi

MONITORING_ENABLED="false"
if [[ "$TIER" -ge 1 ]]; then
  MONITORING_ENABLED="true"
fi

# --- Create main.yml ---
mkdir -p "$HOST_DIR"

cat > "${HOST_DIR}/main.yml" << EOF
---
# Instance: ${HOSTNAME}
# Domain: ${DOMAIN}
# Tier: ${TIER}

cml_domain: ${DOMAIN}
cml_node_env: production

# Feature toggles
cml_enable_media: "false"
cml_listmonk_sync_enabled: "false"
cml_gancio_sync_enabled: "false"
cml_email_test_mode: "true"
cml_monitoring_enabled: ${MONITORING_ENABLED}

# SMTP (update for production email)
cml_smtp_host: mailhog-changemaker
cml_smtp_port: 1025
cml_smtp_user: ""

# Pangolin tunnel (configure after setup)
cml_pangolin_api_url: "https://api.bnkserve.org/v1"
cml_pangolin_org_id: ""

# Bunker Ops
bunker_ops_enabled: ${BUNKER_OPS_ENABLED}
bunker_ops_instance_label: "${DOMAIN}"
bunker_ops_remote_write_url: ""
EOF

echo "Created: ${HOST_DIR}/main.yml"

# --- Generate vault ---
"${SCRIPT_DIR}/bootstrap-vault.sh" "$HOSTNAME" --domain "$DOMAIN"

# --- Append to hosts.yml reminder ---
echo ""
echo "Add to inventory/hosts.yml under changemaker_instances:"
echo ""
echo "        ${HOSTNAME}:"
echo "          ansible_host: ${SSH_HOST}"
echo "          ansible_user: deploy"
echo "          cml_domain: ${DOMAIN}"
echo ""
echo "Then deploy:"
echo "  ansible-playbook playbooks/deploy.yml --limit ${HOSTNAME}"
