#!/usr/bin/env bash
# create-full.sh — pipeline completo para crear un agente funcional
#
# Ejecuta en orden: scaffold → build → register → verify E2EE
# NO arranca el agente — primero personalizar agent.go, config.yaml y prompts/system.md
#
# Uso:
#   ./dev-scripts/agent/create-full.sh <agent-id> "Display Name"
#
# Ejemplo:
#   ./dev-scripts/agent/create-full.sh monitor-bot "Monitor Agent"
#
# Requisitos en .env:
#   MATRIX_ADMIN_TOKEN, MATRIX_HOMESERVER, MATRIX_SERVER_NAME

source "$(dirname "$0")/../_common.sh"
load_env

need_arg "${1:-}"

ID="$1"
DISPLAYNAME="${2:-$ID}"
NORM="$(normalize_id "$ID")"
SCRIPT_DIR="$(dirname "$0")"

echo ""
echo -e "${BLU}═══════════════════════════════════════════════════════${RST}"
echo -e "${BLU}  Creando agente: ${GRN}$ID${BLU} ($DISPLAYNAME)${RST}"
echo -e "${BLU}═══════════════════════════════════════════════════════${RST}"
echo ""

# ── Paso 1: Scaffold ─────────────────────────────────────────────────────
info "Paso 1/4 — Scaffold (agent.go, config.yaml, prompts, launcher)"
echo ""

"$SCRIPT_DIR/new-agent.sh" "$ID" "$DISPLAYNAME"

echo ""

# ── Paso 2: Verificar compilación ─────────────────────────────────────────
info "Paso 2/4 — Verificando compilación..."

if "$GO" build -tags goolm ./... 2>&1; then
  ok "Compilación exitosa"
else
  fail "Error de compilación — revisa agents/$ID/agent.go y cmd/launcher/main.go"
fi

echo ""

# ── Paso 3: Registrar en Matrix ──────────────────────────────────────────
info "Paso 3/4 — Registrando en Matrix..."
echo ""

# Reload .env in case new-agent.sh or previous steps changed it
load_env

"$SCRIPT_DIR/register.sh" "$ID" "$DISPLAYNAME"

echo ""

# ── Paso 4: Verificar E2EE ───────────────────────────────────────────────
info "Paso 4/4 — Verificación E2EE (cross-signing + recovery key)..."
echo ""

# Reload .env to pick up token, password, pickle key from register.sh
load_env

"$SCRIPT_DIR/verify.sh" "$ID"

echo ""

# ── Resumen ──────────────────────────────────────────────────────────────
echo -e "${GRN}═══════════════════════════════════════════════════════${RST}"
echo -e "${GRN}  ✓ Agente $ID creado exitosamente${RST}"
echo -e "${GRN}═══════════════════════════════════════════════════════${RST}"
echo ""
echo -e "  ${BLU}Archivos creados:${RST}"
echo -e "    agents/$ID/agent.go"
echo -e "    agents/$ID/config.yaml"
echo -e "    agents/$ID/prompts/system.md"
echo ""
echo -e "  ${BLU}Variables en .env:${RST}"
echo -e "    MATRIX_TOKEN_${NORM}"
echo -e "    MATRIX_PASSWORD_${NORM}"
echo -e "    PICKLE_KEY_${NORM}"
echo -e "    SSSS_RECOVERY_KEY_${NORM}"
echo ""
echo -e "  ${BLU}Launcher actualizado:${RST}"
echo -e "    cmd/launcher/main.go (import + rulesRegistry)"
echo ""
echo -e "${YLW}Siguiente paso:${RST}"
echo ""
echo -e "  1. Personalizar los archivos del agente:"
echo -e "     ${DIM}agents/$ID/agent.go${RST}            — reglas de decisión"
echo -e "     ${DIM}agents/$ID/config.yaml${RST}         — LLM, tools, personalidad"
echo -e "     ${DIM}agents/$ID/prompts/system.md${RST}   — system prompt"
echo ""
echo -e "  2. Arrancar:"
echo -e "     ${DIM}./dev-scripts/server/start.sh${RST}"
echo ""
