#!/bin/bash
# Firefrost Gaming - Whitelist Manager Deployment Script
# Run this script on Billing VPS (38.68.14.188)

set -e  # Exit on error

echo "======================================"
echo "Firefrost Gaming - Whitelist Manager"
echo "Deployment Script v1.0"
echo "======================================"
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}ERROR: This script must be run as root${NC}"
    exit 1
fi

echo -e "${GREEN}Step 1: Installing system dependencies...${NC}"
apt update
apt install -y python3 python3-pip python3-venv nginx certbot python3-certbot-nginx

echo -e "${GREEN}Step 2: Creating application directory...${NC}"
mkdir -p /opt/whitelist-manager/templates
cd /opt/whitelist-manager

echo -e "${YELLOW}Step 3: Copy application files to /opt/whitelist-manager/${NC}"
echo "Please copy the following files:"
echo "  - app.py"
echo "  - requirements.txt"
echo "  - .env"
echo "  - templates/index.html"
echo ""
read -p "Press Enter once files are copied..."

echo -e "${GREEN}Step 4: Installing Python dependencies...${NC}"
pip3 install -r requirements.txt --break-system-packages

echo -e "${GREEN}Step 5: Testing application...${NC}"
# Quick test that app can import dependencies
python3 -c "import flask, flask_httpauth, requests, dotenv" && echo "Dependencies OK" || {
    echo -e "${RED}ERROR: Python dependencies failed${NC}"
    exit 1
}

echo -e "${GREEN}Step 6: Setting up systemd service...${NC}"
# Copy service file
cp whitelist-manager.service /etc/systemd/system/
systemctl daemon-reload
systemctl enable whitelist-manager
systemctl start whitelist-manager

# Check service status
sleep 2
if systemctl is-active --quiet whitelist-manager; then
    echo -e "${GREEN}✓ Whitelist Manager service is running${NC}"
else
    echo -e "${RED}✗ Whitelist Manager service failed to start${NC}"
    echo "Check logs with: journalctl -u whitelist-manager -n 50"
    exit 1
fi

echo -e "${GREEN}Step 7: Configuring Nginx...${NC}"
cp nginx.conf /etc/nginx/sites-available/whitelist.firefrostgaming.com
ln -sf /etc/nginx/sites-available/whitelist.firefrostgaming.com /etc/nginx/sites-enabled/

# Test Nginx configuration
nginx -t || {
    echo -e "${RED}ERROR: Nginx configuration test failed${NC}"
    exit 1
}

# Reload Nginx
systemctl reload nginx
echo -e "${GREEN}✓ Nginx configured${NC}"

echo -e "${YELLOW}Step 8: DNS Configuration${NC}"
echo "Before obtaining SSL certificate, ensure DNS is configured:"
echo "  Record: whitelist.firefrostgaming.com"
echo "  Type: A"
echo "  Value: 64.50.188.14"
echo ""
read -p "Press Enter once DNS is configured and propagated..."

echo -e "${GREEN}Step 9: Obtaining SSL certificate...${NC}"
certbot --nginx -d whitelist.firefrostgaming.com --non-interactive --agree-tos --email mkrause612@gmail.com

echo ""
echo -e "${GREEN}======================================"
echo "Deployment Complete!"
echo "======================================${NC}"
echo ""
echo "Whitelist Manager is now running at:"
echo "  https://whitelist.firefrostgaming.com"
echo ""
echo "Login credentials:"
echo "  Username: mkrause612"
echo "  Password: Butter2018!!"
echo ""
echo "Useful commands:"
echo "  Service status:  systemctl status whitelist-manager"
echo "  View logs:       journalctl -u whitelist-manager -f"
echo "  Restart:         systemctl restart whitelist-manager"
echo "  Nginx logs:      tail -f /var/log/nginx/whitelist.*.log"
echo ""
echo -e "${YELLOW}🔥❄️ Fire + Frost + Foundation = Where Love Builds Legacy 💙${NC}"
