#!/bin/bash
# MyRoadTrip - Quick Installation Script

set -e

echo "🚀 MyRoadTrip - Quick Installation"
echo "======================================"
echo ""

# Check prerequisites
echo "📋 Checking prerequisites..."

# Check Node.js
if ! command -v node &> /dev/null; then
    echo "❌ Node.js is not installed. Please install Node.js 20+"
    exit 1
fi

NODE_VERSION=$(node -v | cut -d'v' -f2 | cut -d'.' -f1)
if [ "$NODE_VERSION" -lt 20 ]; then
    echo "❌ Node.js version must be 20+. Current: $(node -v)"
    exit 1
fi
echo "✅ Node.js $(node -v)"

# Check pnpm
if ! command -v pnpm &> /dev/null; then
    echo "⚠️  pnpm not found. Installing pnpm..."
    npm install -g pnpm@9
fi
echo "✅ pnpm $(pnpm -v)"

# Check Redis
if ! command -v redis-cli &> /dev/null; then
    echo "⚠️  Redis not found. Please install Redis:"
    echo "   macOS: brew install redis && brew services start redis"
    echo "   Ubuntu: sudo apt install redis-server"
    echo "   Docker: docker run -d -p 6379:6379 redis:7-alpine"
else
    if redis-cli ping &> /dev/null; then
        echo "✅ Redis is running"
    else
        echo "⚠️  Redis is installed but not running. Start it with:"
        echo "   macOS: brew services start redis"
        echo "   Linux: sudo systemctl start redis"
    fi
fi

echo ""
echo "📦 Installing dependencies..."
pnpm install

echo ""
echo "🔐 Setting up environment..."
if [ ! -f .env ]; then
    cp .env.example .env
    echo "✅ Created .env file"
    echo ""
    echo "⚠️  IMPORTANT: Edit .env and fill in your credentials:"
    echo "   - FIREBASE_PROJECT_ID, FIREBASE_CLIENT_EMAIL, FIREBASE_PRIVATE_KEY"
    echo "   - OPENAI_KEY"
    echo "   - VITE_GOOGLE_MAPS_API_KEY or VITE_MAPBOX_TOKEN"
    echo ""
else
    echo "✅ .env file already exists"
fi

echo ""
echo "🏗️  Building packages..."
pnpm build || echo "⚠️  Build failed. Make sure to configure .env first."

echo ""
echo "======================================"
echo "✅ Installation complete!"
echo ""
echo "Next steps:"
echo "1. Edit .env with your credentials"
echo "2. Run: pnpm dev"
echo "3. Open: http://localhost:5173"
echo ""
echo "Documentation:"
echo "- Setup guide: docs/SETUP.md"
echo "- Architecture: docs/REFERENCE.md"
