# 🚀 Quick Start Deployment Scripts

## Prerequisites Check
check_prerequisites() {
  echo "Checking prerequisites..."
  
  command -v docker >/dev/null 2>&1 || { echo "❌ Docker is not installed"; exit 1; }
  command -v docker-compose >/dev/null 2>&1 || { echo "❌ Docker Compose is not installed"; exit 1; }
  
  echo "✅ All prerequisites met"
}

## Generate Secrets
generate_secrets() {
  echo "🔑 Generating secrets..."
  
  JWT_SECRET=$(openssl rand -hex 32)
  JWT_REFRESH_SECRET=$(openssl rand -hex 32)
  CSRF_SECRET=$(openssl rand -hex 32)
  SESSION_SECRET=$(openssl rand -hex 32)
  DB_PASSWORD=$(openssl rand -base64 32 | tr -d "=+/" | cut -c1-25)
  REDIS_PASSWORD=$(openssl rand -base64 32 | tr -d "=+/" | cut -c1-25)
  
  echo "Generated secrets - Save these securely!"
  echo "JWT_SECRET=$JWT_SECRET"
  echo "JWT_REFRESH_SECRET=$JWT_REFRESH_SECRET"
  echo "CSRF_SECRET=$CSRF_SECRET"
  echo "SESSION_SECRET=$SESSION_SECRET"
  echo "DB_PASSWORD=$DB_PASSWORD"
  echo "REDIS_PASSWORD=$REDIS_PASSWORD"
}

## Quick Deploy
quick_deploy() {
  echo "🚀 Starting quick deployment..."
  
  # Copy environment files
  cp .env.docker .env
  cp backend/.env.production.example backend/.env.production
  
  # Build and start services
  docker-compose build
  docker-compose up -d
  
  # Wait for services to be ready
  echo "⏳ Waiting for services to start..."
  sleep 10
  
  # Check health
  echo "🔍 Checking health..."
  curl -f http://localhost/health || echo "❌ Frontend health check failed"
  curl -f http://localhost:5000/health || echo "❌ Backend health check failed"
  
  echo "✅ Deployment complete!"
  echo "Frontend: http://localhost"
  echo "Backend: http://localhost:5000"
}

## Usage
case "$1" in
  check)
    check_prerequisites
    ;;
  secrets)
    generate_secrets
    ;;
  deploy)
    check_prerequisites
    quick_deploy
    ;;
  *)
    echo "Usage: $0 {check|secrets|deploy}"
    echo "  check   - Check prerequisites"
    echo "  secrets - Generate random secrets"
    echo "  deploy  - Quick deployment"
    exit 1
esac
