#!/bin/bash

# Institutional Trader - Proxmox Deployment Script
# This script automates the deployment process on a Proxmox Ubuntu VM/Container
# Run as root: sudo bash deploy.sh

set -e  # Exit on error

echo "🚀 Institutional Trader - Proxmox Deployment Script"
echo "=================================================="

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

# Check if running as root
if [ "$EUID" -ne 0 ]; then 
    echo -e "${RED}Please run as root (use sudo)${NC}"
    exit 1
fi

# Configuration
APP_DIR="/opt/institutional_trader"
BACKEND_DIR="$APP_DIR/backend"
FRONTEND_DIR="$APP_DIR/frontend"
PYTHON_SERVICE_DIR="$BACKEND_DIR/python_service"

# Step 1: Update system
echo -e "\n${GREEN}[1/8] Updating system packages...${NC}"
apt update && apt upgrade -y

# Step 2: Install system dependencies
echo -e "\n${GREEN}[2/8] Installing system dependencies...${NC}"
apt install -y curl wget git build-essential

# Step 3: Install Node.js
echo -e "\n${GREEN}[3/8] Installing Node.js 20.x...${NC}"
if ! command -v node &> /dev/null; then
    curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
    apt install -y nodejs
else
    echo "Node.js already installed: $(node --version)"
fi

# Step 4: Install Python and dependencies
echo -e "\n${GREEN}[4/8] Installing Python 3 and pip...${NC}"
apt install -y python3 python3-pip python3-venv
pip3 install uvicorn

# Step 5: Install Nginx
echo -e "\n${GREEN}[5/8] Installing Nginx...${NC}"
apt install -y nginx

# Step 6: Install PostgreSQL client (optional, for local DB)
echo -e "\n${GREEN}[6/8] Installing PostgreSQL client...${NC}"
apt install -y postgresql-client

# Step 7: Setup application directory
echo -e "\n${GREEN}[7/8] Setting up application directory...${NC}"
if [ ! -d "$APP_DIR" ]; then
    echo -e "${YELLOW}Application directory not found at $APP_DIR${NC}"
    echo -e "${YELLOW}Please ensure you've cloned/uploaded the application files${NC}"
    echo -e "${YELLOW}You can:${NC}"
    echo -e "  1. Clone from Git: git clone <repo-url> $APP_DIR"
    echo -e "  2. Or upload files manually to $APP_DIR"
    read -p "Press Enter when files are in place..."
fi

# Step 8: Install application dependencies
echo -e "\n${GREEN}[8/8] Installing application dependencies...${NC}"

# Backend dependencies
if [ -d "$BACKEND_DIR" ]; then
    echo "Installing backend dependencies..."
    cd "$BACKEND_DIR"
    npm install --production
else
    echo -e "${RED}Backend directory not found!${NC}"
fi

# Python service dependencies
if [ -d "$PYTHON_SERVICE_DIR" ]; then
    echo "Setting up Python service..."
    cd "$PYTHON_SERVICE_DIR"
    if [ ! -d "venv" ]; then
        python3 -m venv venv
    fi
    source venv/bin/activate
    pip install -r requirements.txt
    deactivate
else
    echo -e "${YELLOW}Python service directory not found (optional)${NC}"
fi

# Frontend dependencies and build
if [ -d "$FRONTEND_DIR" ]; then
    echo "Installing frontend dependencies..."
    cd "$FRONTEND_DIR"
    npm install
    echo "Building frontend..."
    npm run build
else
    echo -e "${RED}Frontend directory not found!${NC}"
fi

# Summary
echo -e "\n${GREEN}=================================================="
echo "✅ Basic setup complete!"
echo "==================================================${NC}"
echo ""
echo "Next steps:"
echo "1. Configure environment variables:"
echo "   - Backend: $BACKEND_DIR/.env"
echo "   - Frontend: $FRONTEND_DIR/.env.production"
echo ""
echo "2. Set up database (if using local PostgreSQL):"
echo "   - See PROXMOX_DEPLOYMENT.md section 5"
echo ""
echo "3. Create systemd services:"
echo "   - See PROXMOX_DEPLOYMENT.md section 6"
echo ""
echo "4. Configure Nginx:"
echo "   - See PROXMOX_DEPLOYMENT.md section 7"
echo ""
echo "5. Set up SSL certificates:"
echo "   - See PROXMOX_DEPLOYMENT.md section 8"
echo ""
echo "6. Set up database access for friend:"
echo "   - See PROXMOX_DEPLOYMENT.md section 9"
echo "   - SQL script: $BACKEND_DIR/database/setup_friend_access.sql"
echo ""
echo -e "${YELLOW}For detailed instructions, see PROXMOX_DEPLOYMENT.md${NC}"

