#!/usr/bin/env bash
set -euo pipefail

# Sofia — AI Thumbnail Designer for OpenClaw
# Installation script

WORKSPACE="${HOME}/.openclaw/workspace"

# Parse args
while [[ $# -gt 0 ]]; do
  case "$1" in
    --workspace) WORKSPACE="$2"; shift 2 ;;
    *) echo "Unknown option: $1"; exit 1 ;;
  esac
done

echo "🎨 Installing Sofia skill to: $WORKSPACE"
echo ""

# 1. Create directories
echo "📁 Creating directories..."
mkdir -p "$WORKSPACE/skills/sofia"
mkdir -p "$WORKSPACE/skills/thumbnail-agent/best-examples"
mkdir -p "$WORKSPACE/skills/nano-banana-pro/scripts"
mkdir -p "$WORKSPACE/assets/ben-photos"
mkdir -p "$WORKSPACE/assets/logos"/{ai-tools,automation,social,dev-tools,google,misc}

# 2. Copy skills
echo "📋 Copying skills..."
cp -r skills/sofia/* "$WORKSPACE/skills/sofia/"
cp -r skills/thumbnail-agent/* "$WORKSPACE/skills/thumbnail-agent/"

# 3. Copy assets
echo "🖼️  Copying assets (photos + logos)..."
cp assets/ben-photos/* "$WORKSPACE/assets/ben-photos/" 2>/dev/null || true
cp -r assets/logos/* "$WORKSPACE/assets/logos/" 2>/dev/null || true

# 4. Copy generation script
echo "🔧 Copying image generation script..."
cp scripts/generate_image.py "$WORKSPACE/skills/nano-banana-pro/scripts/generate_image.py"

# 5. Check GEMINI_API_KEY
echo ""
if [[ -n "${GEMINI_API_KEY:-}" ]]; then
  echo "✅ GEMINI_API_KEY found in environment"
elif [[ -f "${HOME}/.openclaw/.env" ]] && grep -q "GEMINI_API_KEY" "${HOME}/.openclaw/.env"; then
  echo "✅ GEMINI_API_KEY found in ~/.openclaw/.env"
else
  echo "⚠️  GEMINI_API_KEY not found!"
  echo "   Get a free key at: https://aistudio.google.com/apikey"
  echo "   Then add to ~/.openclaw/.env:"
  echo "   GEMINI_API_KEY=your-key-here"
fi

# 6. Check uv
if command -v uv &>/dev/null; then
  echo "✅ uv is installed ($(uv --version 2>/dev/null || echo 'unknown version'))"
else
  echo "⚠️  uv not found! Install it:"
  echo "   curl -LsSf https://astral.sh/uv/install.sh | sh"
fi

# 7. Quick test
echo ""
echo "🧪 Running quick test..."
if [[ -n "${GEMINI_API_KEY:-}" ]] || { [[ -f "${HOME}/.openclaw/.env" ]] && grep -q "GEMINI_API_KEY" "${HOME}/.openclaw/.env"; }; then
  if command -v uv &>/dev/null; then
    # Source .env if needed
    if [[ -z "${GEMINI_API_KEY:-}" ]] && [[ -f "${HOME}/.openclaw/.env" ]]; then
      export $(grep "GEMINI_API_KEY" "${HOME}/.openclaw/.env" | xargs)
    fi
    TEST_OUTPUT="/tmp/sofia-test-$(date +%s).png"
    if uv run --with google-genai "$WORKSPACE/skills/nano-banana-pro/scripts/generate_image.py" \
      --prompt "Simple test: blue circle on white background" \
      --output "$TEST_OUTPUT" 2>/dev/null; then
      echo "✅ Test passed! Generated: $TEST_OUTPUT"
      rm -f "$TEST_OUTPUT"
    else
      echo "⚠️  Test generation failed — check your GEMINI_API_KEY and try manually"
    fi
  else
    echo "⏭️  Skipping test (uv not installed)"
  fi
else
  echo "⏭️  Skipping test (GEMINI_API_KEY not set)"
fi

# 8. Summary
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "🎉 Sofia installed successfully!"
echo ""
echo "Next steps:"
echo "  1. Make sure GEMINI_API_KEY is set"
echo "  2. In OpenClaw, Sofia is available as a sub-agent skill"
echo "  3. Customize assets/ben-photos/ with your own photos"
echo "  4. Edit skills/thumbnail-agent/style-guide.md for your brand"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"