#!/bin/bash

# Winx Launcher Script
# This script helps with setting up and launching the Winx Code Agent

# Default settings
WINX_HOME="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
WINX_BIN="$WINX_HOME/target/release/winx-code-agent"
WINXD_BIN="$WINX_HOME/target/release/winxd"
WINX_GUARDIAN_BIN="$WINX_HOME/target/release/winx-guardian"
WINX_VERSION=$(awk -F '"' '/^version = / { print $2; exit }' "$WINX_HOME/Cargo.toml")
CLAUDE_CONFIG="$HOME/Library/Application Support/Claude/claude_desktop_config.json"
LOG_LEVEL="info"
DEBUG_MODE=false
DO_BUILD=false
DO_INSTALL=false

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

# Print banner
echo -e "${BLUE}"
echo "✨ Ｗｉｎｘ Ａｇｅｎｔ Ｌａｕｎｃｈｅｒ ✨"
echo "Version: $WINX_VERSION"
echo -e "${NC}"

# Parse command line arguments
while [[ $# -gt 0 ]]; do
  case $1 in
    --debug)
      LOG_LEVEL="debug"
      shift
      ;;
    --debug-mode)
      # Backward-compatible alias for the CLI's actual --debug flag.
      DEBUG_MODE=true
      LOG_LEVEL="debug"
      shift
      ;;
    --verbose)
      LOG_LEVEL="info"
      shift
      ;;
    --build)
      DO_BUILD=true
      shift
      ;;
    --install)
      DO_INSTALL=true
      shift
      ;;
    --help)
      echo "Usage: $0 [options]"
      echo "Options:"
      echo "  --debug       Enable debug logging"
      echo "  --debug-mode  Alias for --debug"
      echo "  --verbose     Enable verbose logging"
      echo "  --build       Build the agent before launching"
      echo "  --install     Install/update the agent in Claude config"
      echo "  --help        Show this help message"
      exit 0
      ;;
    *)
      echo "Unknown option: $1"
      echo "Use --help for usage information"
      exit 1
      ;;
  esac
done

# Check if winx exists
if [ ! -d "$WINX_HOME" ]; then
  echo -e "${RED}Winx directory not found at $WINX_HOME${NC}"
  exit 1
fi

# Build if requested
if [ "$DO_BUILD" = true ]; then
  echo -e "${YELLOW}Building Winx agent...${NC}"
  cd "$WINX_HOME" || exit 1
  if ! cargo build --release --locked --bins; then
    echo -e "${RED}Build failed!${NC}"
    exit 1
  fi
  
  echo -e "${GREEN}Build successful!${NC}"
fi

for binary in "$WINX_BIN" "$WINXD_BIN" "$WINX_GUARDIAN_BIN"; do
  if [ ! -x "$binary" ]; then
    echo -e "${RED}Required binary not found or not executable: $binary${NC}"
    echo "Run $0 --build first."
    exit 1
  fi
done

# Install in Claude config if requested
if [ "$DO_INSTALL" = true ]; then
  echo -e "${YELLOW}Installing Winx in Claude configuration...${NC}"
  
  # Check if Claude config exists
  if [ ! -f "$CLAUDE_CONFIG" ]; then
    # Create default config
    mkdir -p "$(dirname "$CLAUDE_CONFIG")"
    echo '{
  "mcpServers": {
    "winx": {
      "command": "'$WINX_BIN'",
      "args": [],
      "env": {
        "RUST_LOG": "'$LOG_LEVEL'"
      }
    }
  }
}' > "$CLAUDE_CONFIG"
    echo -e "${GREEN}Created new Claude configuration${NC}"
  else
    # Update existing config
    temp_file=$(mktemp)
    cat "$CLAUDE_CONFIG" | jq '.mcpServers.winx = {
      "command": "'$WINX_BIN'", 
      "args": [], 
      "env": {"RUST_LOG": "'$LOG_LEVEL'"}
    }' > "$temp_file"
    
    if [ $? -eq 0 ]; then
      mv "$temp_file" "$CLAUDE_CONFIG"
      echo -e "${GREEN}Updated Claude configuration${NC}"
    else
      echo -e "${RED}Failed to update Claude configuration${NC}"
      rm -f "$temp_file"
      exit 1
    fi
  fi
fi

# Check if Claude is running
if pgrep -x "Claude" > /dev/null; then
  echo -e "${YELLOW}Note: Claude is currently running. You may need to restart it for changes to take effect.${NC}"
fi

# Print status
echo -e "${GREEN}Winx agent is ready!${NC}"
echo "Adapter: $WINX_BIN"
echo "Control daemon: $WINXD_BIN"
echo "Session guardian: $WINX_GUARDIAN_BIN"
echo "Config: $CLAUDE_CONFIG"
echo "Log level: $LOG_LEVEL"
echo "Debug mode: $DEBUG_MODE"

# Start the adapter. The daemon runtime discovers winxd/winx-guardian beside it.
if [ "$DEBUG_MODE" = true ] || [ "$LOG_LEVEL" = "debug" ]; then
  echo -e "${YELLOW}Starting Winx in debug mode...${NC}"
  "$WINX_BIN" --debug
else
  echo -e "${YELLOW}Starting Winx...${NC}"
  "$WINX_BIN" --verbose
fi
