#!/bin/bash

# QR Master - Quick Setup Script
# This script automates the initial setup process

set -e

echo "🚀 QR Master - Quick Setup"
echo "================================"
echo ""

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

# Check if Docker is installed
if ! command -v docker &> /dev/null; then
    echo -e "${RED}❌ Docker is not installed. Please install Docker first.${NC}"
    exit 1
fi

# Check if Docker Compose is installed
if ! command -v docker-compose &> /dev/null; then
    echo -e "${RED}❌ Docker Compose is not installed. Please install Docker Compose first.${NC}"
    exit 1
fi

echo -e "${GREEN}✓${NC} Docker is installed"
echo -e "${GREEN}✓${NC} Docker Compose is installed"
echo ""

# Check if .env exists
if [ ! -f .env ]; then
    echo "📝 Creating .env file from template..."
    cp env.example .env
    
    # Generate secrets
    if command -v openssl &> /dev/null; then
        NEXTAUTH_SECRET=$(openssl rand -base64 32)
        IP_SALT=$(openssl rand -base64 32)
        
        # Update .env with generated secrets
        sed -i.bak "s|NEXTAUTH_SECRET=.*|NEXTAUTH_SECRET=$NEXTAUTH_SECRET|" .env
        sed -i.bak "s|IP_SALT=.*|IP_SALT=$IP_SALT|" .env
        rm .env.bak 2>/dev/null || true
        
        echo -e "${GREEN}✓${NC} Generated secure secrets"
    else
        echo -e "${YELLOW}⚠${NC}  OpenSSL not found. Please manually update NEXTAUTH_SECRET and IP_SALT in .env"
    fi
else
    echo -e "${GREEN}✓${NC} .env file already exists"
fi

echo ""

# Ask user what mode they want
echo "Choose setup mode:"
echo "1) Development (database only in Docker, app on host)"
echo "2) Production (full stack in Docker)"
read -p "Enter choice [1-2]: " choice

echo ""

case $choice in
    1)
        echo "🔧 Setting up development environment..."
        echo ""
        
        # Start database services
        echo "Starting PostgreSQL and Redis..."
        docker-compose -f docker-compose.dev.yml up -d
        
        # Wait for database to be ready
        echo "Waiting for database to be ready..."
        sleep 5
        
        # Install dependencies
        echo "Installing dependencies..."
        npm install
        
        # Run migrations
        echo "Running database migrations..."
        npm run db:migrate
        
        # Seed database
        echo "Seeding database with demo data..."
        npm run db:seed
        
        echo ""
        echo -e "${GREEN}✅ Development environment ready!${NC}"
        echo ""
        echo "To start the application:"
        echo "  npm run dev"
        echo ""
        echo "Access points:"
        echo "  - App: http://localhost:3050"
        echo "  - Database UI: http://localhost:8080"
        echo "  - Database: localhost:5435"
        echo "  - Redis: localhost:6379"
        ;;
    2)
        echo "🚀 Setting up production environment..."
        echo ""
        
        # Build and start all services
        echo "Building and starting all services..."
        docker-compose up -d --build
        
        # Wait for services to be ready
        echo "Waiting for services to be ready..."
        sleep 10
        
        # Run migrations
        echo "Running database migrations..."
        docker-compose exec web npx prisma migrate deploy
        
        # Seed database
        echo "Seeding database with demo data..."
        docker-compose exec web npm run db:seed
        
        echo ""
        echo -e "${GREEN}✅ Production environment ready!${NC}"
        echo ""
        echo "Access points:"
        echo "  - App: http://localhost:3050"
        echo "  - Database: localhost:5435"
        echo "  - Redis: localhost:6379"
        echo ""
        echo "To view logs:"
        echo "  docker-compose logs -f"
        ;;
    *)
        echo -e "${RED}Invalid choice. Exiting.${NC}"
        exit 1
        ;;
esac

echo ""
echo "📚 Documentation:"
echo "  - Quick start: README.md"
echo "  - Docker guide: DOCKER_SETUP.md"
echo "  - Migration guide: MIGRATION_FROM_SUPABASE.md"
echo ""
echo "🎉 Setup complete! Happy coding!"

