#!/bin/bash
# SmoothSchedule Production Deployment Script
# Usage: ./deploy.sh [server_user@server_host]
# Example: ./deploy.sh poduck@smoothschedule.com

set -e

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

SERVER=${1:-"poduck@smoothschedule.com"}
PROJECT_DIR="/home/poduck/Desktop/smoothschedule2"

echo -e "${GREEN}==================================="
echo "SmoothSchedule Deployment"
echo "===================================${NC}"
echo "Target server: $SERVER"
echo ""

# Function to print status
print_status() {
    echo -e "${GREEN}>>> $1${NC}"
}

print_warning() {
    echo -e "${YELLOW}>>> $1${NC}"
}

print_error() {
    echo -e "${RED}>>> $1${NC}"
}

# Step 1: Build frontend
print_status "Step 1: Skipping local build (building on server)..."
# cd "$PROJECT_DIR/frontend"
# if [ ! -d "node_modules" ]; then
#     print_warning "Installing frontend dependencies..."
#     npm install
# fi
# npm run build
# print_status "Frontend build complete!"

# Step 2: Prepare deployment package
print_status "Step 2: Preparing deployment package..."
cd "$PROJECT_DIR"
mkdir -p /tmp/smoothschedule-deploy

# Copy backend
rsync -av --exclude='.venv' --exclude='__pycache__' --exclude='*.pyc' \
    --exclude='.git' --exclude='node_modules' \
    "$PROJECT_DIR/smoothschedule/" /tmp/smoothschedule-deploy/backend/

# Copy frontend source
rsync -av --exclude='node_modules' --exclude='dist' --exclude='.git' \
    "$PROJECT_DIR/frontend/" /tmp/smoothschedule-deploy/frontend/

print_status "Deployment package prepared!"

# Step 3: Upload to server
print_status "Step 3: Uploading to server..."
ssh "$SERVER" "mkdir -p ~/smoothschedule"

# Upload backend
rsync -avz --delete /tmp/smoothschedule-deploy/backend/ "$SERVER:~/smoothschedule/"

# Upload nginx config
scp "$PROJECT_DIR/frontend/nginx.conf" "$SERVER:~/smoothschedule/nginx.conf"

# Upload frontend
rsync -avz --delete /tmp/smoothschedule-deploy/frontend/ "$SERVER:~/smoothschedule-frontend/"

print_status "Files uploaded!"

# Step 4: Deploy on server
print_status "Step 4: Deploying on server..."

ssh "$SERVER" 'bash -s' << 'ENDSSH'
set -e

echo ">>> Navigating to project directory..."
cd ~/smoothschedule

echo ">>> Building Docker images..."
docker compose -f docker-compose.production.yml build

echo ">>> Starting containers..."
docker compose -f docker-compose.production.yml up -d

echo ">>> Waiting for containers to start..."
sleep 10

echo ">>> Running database migrations..."
docker compose -f docker-compose.production.yml exec -T django sh -c 'export DATABASE_URL=postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB} && python manage.py migrate'

echo ">>> Collecting static files..."
docker compose -f docker-compose.production.yml exec -T django sh -c 'export DATABASE_URL=postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB} && python manage.py collectstatic --noinput'

echo ">>> Checking container status..."
docker compose -f docker-compose.production.yml ps

echo ">>> Deployment complete!"
ENDSSH

# Cleanup
rm -rf /tmp/smoothschedule-deploy

echo ""
print_status "==================================="
print_status "Deployment Complete!"
print_status "==================================="
echo ""
echo "Your application should now be running at:"
echo "  - https://smoothschedule.com"
echo "  - https://platform.smoothschedule.com"
echo "  - https://*.smoothschedule.com (tenant subdomains)"
echo ""
echo "To view logs:"
echo "  ssh $SERVER 'cd ~/smoothschedule && docker compose -f docker-compose.production.yml logs -f'"
echo ""
echo "To check status:"
echo "  ssh $SERVER 'cd ~/smoothschedule && docker compose -f docker-compose.production.yml ps'"
