#!/bin/bash

# Estate Platform - Quick Start Script
# Automates the setup process

set -e  # Exit on error

echo "=========================================="
echo "Estate Platform - Automated Setup"
echo "=========================================="
echo ""

# Colors for output
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Step counter
STEP=1

step() {
    echo -e "${BLUE}[Step $STEP]${NC} $1"
    ((STEP++))
}

success() {
    echo -e "${GREEN}✓ $1${NC}"
    echo ""
}

# Check prerequisites
step "Checking prerequisites..."
if ! command -v docker &> /dev/null; then
    echo "❌ Docker is not installed. Please install Docker first."
    exit 1
fi
if ! command -v node &> /dev/null; then
    echo "❌ Node.js is not installed. Please install Node.js 18+ first."
    exit 1
fi
success "Prerequisites check complete"

# Copy environment file
step "Setting up environment..."
if [ ! -f ".env.local" ]; then
    cp .env.example .env.local
    success "Created .env.local from .env.example"
else
    success ".env.local already exists"
fi

# Install dependencies
step "Installing Node.js dependencies..."
npm install --silent
success "Dependencies installed"

# Start Docker services
step "Starting Docker services..."
docker-compose up -d
success "Docker services started"

# Wait for services to be ready
step "Waiting for services to be healthy..."
echo "  Checking PostgreSQL..."
until docker-compose exec -T postgres pg_isready -U postgres > /dev/null 2>&1; do
    echo "    Waiting for PostgreSQL..."
    sleep 2
done
success "PostgreSQL is ready"

echo "  Checking Redis..."
until docker-compose exec -T redis redis-cli ping > /dev/null 2>&1; do
    echo "    Waiting for Redis..."
    sleep 2
done
success "Redis is ready"

# Run database migrations
step "Running database migrations..."
docker-compose exec -T web npm run db:migrate
success "Database migrations complete"

# Optional: Seed database
read -p "Seed database with sample data? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    step "Seeding database..."
    docker-compose exec -T web npm run db:seed
    success "Database seeded"
else
    success "Skipping database seed"
fi

echo "=========================================="
echo "Setup Complete!"
echo "=========================================="
echo ""
echo "Your Estate Platform is ready to use!"
echo ""
echo "Access Information:"
echo "  - Application: http://localhost:3000"
echo "  - Admin Setup: http://localhost:3000/admin/setup"
echo "  - Redis CLI: redis-cli"
echo ""
echo "Default Admin Credentials:"
echo "  - Email: admin@estate-platform.com"
echo "  - Password: Admin123!"
echo ""
echo "⚠️  Change the admin password on first login!"
echo ""
echo "Useful Commands:"
echo "  - View logs:     docker-compose logs -f"
echo "  - Stop services: docker-compose down"
echo "  - Database UI:   npm run db:studio"
echo "  - Redis monitor: redis-cli MONITOR"
echo ""
echo "Documentation:"
echo "  - Complete Setup: docs/COMPLETE_SETUP_GUIDE.md"
echo "  - Redis Setup:    docs/REDIS_SETUP.md"
echo "  - OAuth Setup:    docs/BETTERAUTH_SETUP_GUIDE.md"
echo ""
echo "Next steps:"
echo "1. Configure OAuth providers in /admin/setup"
echo "2. Customize webinar categories"
echo "3. Setup email notifications"
echo "4. Configure Stripe (optional)"
echo ""
