#!/bin/bash
set -euo pipefail

MATRIX_SERVER_NAME=${1:-"fleet.example.com"}
ADMIN_USER=${2:-"admin"}
BOT_USERS=("bilbo" "ezra" "allegro" "bezalel" "gemini" "timmy")

echo "=== Fleet Matrix Bootstrap ==="
echo "Server: $MATRIX_SERVER_NAME"

REG_TOKEN=$(openssl rand -hex 32)
echo "$REG_TOKEN" > .registration_token

cat > docker-compose.override.yml << EOF
version: "3.8"
services:
  conduit:
    environment:
      CONDUIT_SERVER_NAME: $MATRIX_SERVER_NAME
      CONDUIT_REGISTRATION_TOKEN: $REG_TOKEN
EOF

ADMIN_PW=$(openssl rand -base64 24)
cat > admin-register.json << EOF
{"username": "$ADMIN_USER", "password": "$ADMIN_PW", "admin": true}
EOF

mkdir -p bot-tokens
for bot in "${BOT_USERS[@]}"; do
    BOT_PW=$(openssl rand -base64 24)
    echo "{"username": "$bot", "password": "$BOT_PW"}" > "bot-tokens/${bot}.json"
done

cat > room-topology.yaml << 'EOF'
spaces:
  fleet-command:
    name: "Fleet Command"
    rooms:
      - {name: "📢 Announcements", encrypted: false}
      - {name: "⚡ Operations", encrypted: true}
      - {name: "🔮 Intelligence", encrypted: true}
      - {name: "🛠️ Infrastructure", encrypted: true}
EOF

echo "Bootstrap complete. Check admin-password.txt and bot-tokens/"
echo "Admin password: $ADMIN_PW"
