#!/bin/bash

# Setup script for Unified Tech Stack Service with Database Integration
# This script helps configure the environment and run database migrations

echo "🚀 Setting up Unified Tech Stack Service with Database Integration"
echo "=================================================================="

# Check if .env file exists
if [ ! -f .env ]; then
    echo "📝 Creating .env file from template..."
    cp env.example .env
    echo "✅ .env file created"
else
    echo "📝 .env file already exists"
fi

echo ""
echo "🔧 Environment Configuration Required:"
echo "======================================"
echo ""
echo "1. Claude AI API Key:"
echo "   - Get your API key from: https://console.anthropic.com/"
echo "   - Add it to .env file as: CLAUDE_API_KEY=your_key_here"
echo ""
echo "2. Database Configuration:"
echo "   - POSTGRES_HOST=localhost"
echo "   - POSTGRES_PORT=5432"
echo "   - POSTGRES_DB=dev_pipeline"
echo "   - POSTGRES_USER=pipeline_admin"
echo "   - POSTGRES_PASSWORD=secure_pipeline_2024"
echo ""
echo "3. Service URLs (if different from defaults):"
echo "   - TEMPLATE_MANAGER_URL=http://localhost:8009"
echo "   - TECH_STACK_SELECTOR_URL=http://localhost:8002"
echo "   - USER_AUTH_URL=http://localhost:8011"
echo ""
echo "4. Optional Configuration:"
echo "   - PORT=8013 (default)"
echo "   - REQUEST_TIMEOUT=30000"
echo "   - CACHE_TTL=300000"
echo ""

# Check if Claude API key is configured
if grep -q "CLAUDE_API_KEY=your_claude_api_key_here" .env; then
    echo "⚠️  WARNING: Claude API key not configured!"
    echo "   Please edit .env file and set your CLAUDE_API_KEY"
    echo "   Without this key, Claude AI recommendations will not work"
    echo ""
else
    echo "✅ Claude API key appears to be configured"
fi

# Check if database configuration is present
if grep -q "POSTGRES_HOST=localhost" .env; then
    echo "✅ Database configuration appears to be present"
else
    echo "⚠️  WARNING: Database configuration may be missing!"
    echo "   Please ensure PostgreSQL connection details are in .env file"
    echo ""
fi

echo "🗄️ Database Migration:"
echo "======================"
echo ""
echo "To create the unified tech stack recommendations table:"
echo ""
echo "1. Connect to your PostgreSQL database:"
echo "   psql -h localhost -U pipeline_admin -d dev_pipeline"
echo ""
echo "2. Run the migration script:"
echo "   \\i src/migrations/001_unified_tech_stack_recommendations.sql"
echo ""
echo "   Or copy and paste the SQL from the migration file"
echo ""
echo "3. Ensure the user-auth service tables exist:"
echo "   The migration references the 'users' table from user-auth service"
echo "   Make sure user-auth service has been set up first"
echo ""

echo "📋 Next Steps:"
echo "=============="
echo "1. Edit .env file with your actual API keys and database config"
echo "2. Run database migration (see above)"
echo "3. Install dependencies: npm install"
echo "4. Start the service: npm start"
echo "5. Test the service: node test-comprehensive-integration.js"
echo ""
echo "🔗 Service will be available at: http://localhost:8013"
echo "📊 Health check: http://localhost:8013/health"
echo "🤖 Comprehensive recommendations: http://localhost:8013/api/unified/comprehensive-recommendations"
echo "👤 User recommendations: http://localhost:8013/api/unified/user/recommendations (requires auth)"
echo "📊 User stats: http://localhost:8013/api/unified/user/stats (requires auth)"
echo "🗄️ Cached recommendations: http://localhost:8013/api/unified/cached-recommendations/{templateId}"
echo "🧹 Admin cleanup: http://localhost:8013/api/unified/admin/cleanup-expired"
echo ""
echo "🔐 Authentication:"
echo "   Include 'Authorization: Bearer <token>' header for user-specific endpoints"
echo "   Get token from user-auth service: http://localhost:8011/api/auth/login"
