#!/bin/bash

# TestArena Deployment Script
# Run this script with sudo: sudo ./deploy.sh

set -e

echo "🚀 Starting TestArena Deployment..."

# 1. Install Dependencies
echo "📦 Installing dependencies..."
apt-get update
apt-get install -y nginx python3-pip python3-venv

# 2. Set up Python Virtual Environment
echo "🐍 Setting up Python environment..."
python3 -m venv venv
source venv/bin/activate
pip install fastapi uvicorn sqlalchemy

# 3. Configure Nginx
echo "🌐 Configuring Nginx..."
cp nginx/testarena.conf /etc/nginx/sites-available/testarena
ln -sf /etc/nginx/sites-available/testarena /etc/nginx/sites-enabled/
rm -f /etc/nginx/sites-enabled/default

# 4. Create Data Directory
echo "📁 Creating data directory..."
mkdir -p /home/asf/testarena
chown -R asf:asf /home/asf/testarena
chmod -R 755 /home/asf/testarena

# 5. Restart Nginx
echo "🔄 Restarting Nginx..."
nginx -t
systemctl restart nginx

echo "✅ Deployment complete!"
echo "--------------------------------------------------"
echo "Dashboard: http://asf-server.duckdns.org:8080/"
echo "Results:   http://asf-server.duckdns.org:8080/results/"
echo "--------------------------------------------------"
echo "To start the app: source venv/bin/activate && uvicorn testarena_app.main:app --host 0.0.0.0 --port 8000"
echo "To start the worker: source venv/bin/activate && python3 -m testarena_app.worker"
