#!/bin/bash

# WordLift CLI Packaging Script

set -e

VERSION=${1:-"1.0.0"}
PACKAGE_NAME="wordlift-cli-${VERSION}"
ORIGINAL_DIR="$(pwd)"

echo "📦 Creating WordLift CLI package (version ${VERSION})..."

# Create a temporary directory for packaging
TEMP_DIR=$(mktemp -d)
PACKAGE_DIR="${TEMP_DIR}/${PACKAGE_NAME}"

echo "📁 Copying files to ${PACKAGE_DIR}..."

# Create package directory structure
mkdir -p "${PACKAGE_DIR}"

# Copy essential files (excluding unnecessary ones)
cp -r bin "${PACKAGE_DIR}/"
cp -r input "${PACKAGE_DIR}/"
cp -r .gemini "${PACKAGE_DIR}/"
cp package.json "${PACKAGE_DIR}/"
cp setup.js "${PACKAGE_DIR}/"
cp setup-global.sh "${PACKAGE_DIR}/"
cp install.sh "${PACKAGE_DIR}/"
cp README.md "${PACKAGE_DIR}/"
cp SETUP_GUIDE.md "${PACKAGE_DIR}/"
cp WORDLIFT.md "${PACKAGE_DIR}/"
cp .gitignore "${PACKAGE_DIR}/"

# Copy WordLift ASCII art
if [ -f "wordlift-ascii-art.txt" ]; then
    cp wordlift-ascii-art.txt "${PACKAGE_DIR}/"
else
    echo "⚠️  wordlift-ascii-art.txt not found, skipping..."
fi

# Create empty output directory (don't copy existing files)
mkdir -p "${PACKAGE_DIR}/output"
echo "# Generated content will appear here" > "${PACKAGE_DIR}/output/.gitkeep"

# Create a sample .env file (without the actual API key)
echo "GEMINI_API_KEY=your_api_key_here" > "${PACKAGE_DIR}/.env.example"

# Exclude files we don't want to distribute:
# - .env (contains actual API key)
# - .DS_Store files
# - output folder contents (user's generated files)
# - AsciiArt-*.js files (we use the .txt version)
# - gemini-ascii-art.txt (we use wordlift version)

echo "🧹 Cleaned unnecessary files from package"

# Create installation instructions
cat > "${PACKAGE_DIR}/INSTALL.md" << 'EOF'
# WordLift CLI Installation Instructions

## Quick Installation

1. **Run the installation script:**
   ```bash
   ./install.sh
   ```

2. **Or install manually:**
   ```bash
   # Install Gemini CLI globally
   npm install -g @google/gemini-cli

   # Install dependencies
   npm install

   # Run setup
   npm run configure
   ```

## Configuration

1. **Get a Gemini API key** from [Google AI Studio](https://aistudio.google.com/app/apikey)

2. **Run the setup:**
   ```bash
   npm run configure
   ```

   Or manually create a `.env` file:
   ```bash
   echo "GEMINI_API_KEY=your_actual_api_key_here" > .env
   ```

## Usage

```bash
# Start interactive chat session
./bin/wordlift-cli.js chat

# Generate content directly
./bin/wordlift-cli.js generate "Create an SEO-optimized article about semantic search"

# Show help
./bin/wordlift-cli.js --help
```

For more information, see README.md
EOF

# Create a tarball
echo "🗜️  Creating tarball..."
cd "${TEMP_DIR}"
tar -czf "${PACKAGE_NAME}.tar.gz" "${PACKAGE_NAME}"

# Move to the original directory
mv "${PACKAGE_NAME}.tar.gz" "${ORIGINAL_DIR}/"

# Clean up
rm -rf "${TEMP_DIR}"

echo "✅ Package created: ${PACKAGE_NAME}.tar.gz"
echo ""
echo "📋 To share with your colleague:"
echo "1. Send them the ${PACKAGE_NAME}.tar.gz file"
echo "2. They should extract it: tar -xzf ${PACKAGE_NAME}.tar.gz"
echo "3. They should cd into the directory: cd ${PACKAGE_NAME}"
echo "4. They should run: ./install.sh"
echo ""
echo "🎉 WordLift CLI is ready for distribution!"
