#!/bin/bash
# Quick setup script for deployment configuration

set -e

echo "==================================="
echo "Deployment Skill Setup Wizard"
echo "==================================="
echo ""

# Function to read input with default
read_input() {
    local prompt="$1"
    local default="$2"
    local var_name="$3"
    
    if [ -n "$default" ]; then
        read -p "$prompt [$default]: " input
        eval "$var_name=\"${input:-$default}\""
    else
        read -p "$prompt: " input
        eval "$var_name=\"$input\""
    fi
}

# Target name
read_input "Enter deployment target name (e.g., PRODUCTION, STAGING)" "" TARGET
TARGET=$(echo "$TARGET" | tr '[:lower:]' '[:upper:]')

echo ""
echo "--- Gitea Configuration ---"
read_input "Gitea registry URL (without https://)" "gitea.example.com" GITEA_URL
read_input "Gitea username" "" GITEA_USER
echo "Gitea token (will be hidden): "
read -s GITEA_TOKEN
echo ""
read_input "Gitea namespace (org or username)" "$GITEA_USER" GITEA_NAMESPACE

echo ""
echo "--- Remote Server Configuration ---"
read_input "SSH host alias (from ~/.ssh/config)" "" SSH_HOST
read_input "Stacks directory on remote server" "/srv/docker/stacks" STACKS_DIR

echo ""
echo "--- Webapp Configuration (optional, press Enter to skip) ---"
read_input "Root domain (e.g., sal.giize.com - app served as subdomain)" "" DOMAIN

# Conditional NPM fields
if [ -n "$DOMAIN" ]; then
    read_input "Nginx Proxy Manager URL" "https://npm.example.com" NPM_URL
    read_input "NPM admin email" "admin@example.com" NPM_USER
    echo "NPM password (will be hidden): "
    read -s NPM_PASS
    echo ""
fi

echo ""
echo "--- Network Configuration (optional, press Enter for defaults) ---"
read_input "Proxy network name" "proxy" PROXY_NETWORK
read_input "Ollama network name (for AI/LLM apps)" "ollama" OLLAMA_NETWORK
read_input "Ollama container name (for deriving connection URL)" "ollama" OLLAMA_CONTAINER

# Generate JSON config
if [ -n "$DOMAIN" ]; then
    JSON_CONFIG=$(cat <<EOF
{
  "gitea_registry_url": "$GITEA_URL",
  "gitea_username": "$GITEA_USER",
  "gitea_token": "$GITEA_TOKEN",
  "gitea_namespace": "$GITEA_NAMESPACE",
  "ssh_host": "$SSH_HOST",
  "stacks_dir": "$STACKS_DIR",
  "domain": "$DOMAIN",
  "npm_url": "$NPM_URL",
  "npm_username": "$NPM_USER",
  "npm_password": "$NPM_PASS",
  "proxy_network": "$PROXY_NETWORK",
  "ollama_network": "$OLLAMA_NETWORK",
  "ollama_container_name": "$OLLAMA_CONTAINER"
}
EOF
)
else
    JSON_CONFIG=$(cat <<EOF
{
  "gitea_registry_url": "$GITEA_URL",
  "gitea_username": "$GITEA_USER",
  "gitea_token": "$GITEA_TOKEN",
  "gitea_namespace": "$GITEA_NAMESPACE",
  "ssh_host": "$SSH_HOST",
  "stacks_dir": "$STACKS_DIR",
  "proxy_network": "$PROXY_NETWORK",
  "ollama_network": "$OLLAMA_NETWORK",
  "ollama_container_name": "$OLLAMA_CONTAINER"
}
EOF
)
fi

echo ""
echo "==================================="
echo "Configuration generated!"
echo "==================================="
echo ""
echo "Add this to your shell configuration file (~/.bashrc, ~/.zshrc, etc.):"
echo ""
echo "export DEPLOY_CONFIG_$TARGET='$JSON_CONFIG'"
echo ""
echo "Then restart your terminal or run:"
echo "source ~/.bashrc  # or ~/.zshrc"
echo ""

# Optionally write to file
read -p "Save to file? (y/n): " SAVE_FILE
if [ "$SAVE_FILE" = "y" ] || [ "$SAVE_FILE" = "Y" ]; then
    CONFIG_FILE="deploy_config_${TARGET}.env"
    echo "export DEPLOY_CONFIG_$TARGET='$JSON_CONFIG'" > "$CONFIG_FILE"
    echo ""
    echo "✓ Saved to $CONFIG_FILE"
    echo "Load it with: source $CONFIG_FILE"
fi

echo ""
echo "==================================="
echo "Next Steps:"
echo "==================================="
echo ""
echo "1. Set the environment variable (see above)"
echo "2. Verify SSH access: ssh $SSH_HOST \"echo 'Test OK'\""
echo "3. Create Docker networks on remote server (run once per server):"
echo "   ssh $SSH_HOST \"docker network create $PROXY_NETWORK\""
if [ -n "$OLLAMA_NETWORK" ]; then
    echo "   ssh $SSH_HOST \"docker network create $OLLAMA_NETWORK\"  # For AI/LLM apps"
fi
echo "4. Navigate to your application directory"
echo "5. Tell the agent: 'deploy $TARGET'"
echo ""
echo "The agent will auto-detect your app's configuration and ask for confirmation!"
echo ""
