#!/bin/bash

# DeafGain Website - Automated Deployment Script
# Handles VPN connection and Docker deployment to production server

set -e  # Exit on error

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

# Configuration
VPN_CONFIG="$HOME/wg1.conf"
PRODUCTION_SERVER="10.4.0.205"
SSH_USER="chaulmark"
PROJECT_PATH="/home/chaulmark/websites/deafgain.org"

echo -e "${BLUE}=================================================${NC}"
echo -e "${BLUE}🚀 DeafGain Website Deployment${NC}"
echo -e "${BLUE}=================================================${NC}"
echo ""

# Function to check if VPN is connected
is_vpn_connected() {
    sudo wg show interfaces 2>/dev/null | grep -q "wg1"
}

# Function to connect VPN
connect_vpn() {
    echo -e "${YELLOW}🔄 Connecting to VPN...${NC}"
    
    if ! [ -f "$VPN_CONFIG" ]; then
        echo -e "${RED}❌ VPN config not found: $VPN_CONFIG${NC}"
        exit 1
    fi
    
    # Run wg-quick and capture output
    result=$(sudo wg-quick up "$VPN_CONFIG" 2>&1) || true
    
    # Check if already exists (already connected)
    if echo "$result" | grep -q "already exists"; then
        echo -e "${GREEN}✅ VPN already connected${NC}"
        return 0
    fi
    
    # Check if connection was successful (look for success indicators)
    if echo "$result" | grep -q "Backgrounding route monitor" || is_vpn_connected; then
        echo -e "${GREEN}✅ VPN connected successfully${NC}"
        return 0
    else
        echo -e "${RED}❌ Failed to connect VPN:${NC}"
        echo "$result"
        exit 1
    fi
}

# Function to test connectivity
test_connectivity() {
    echo -e "${YELLOW}🔍 Testing connectivity to production server...${NC}"
    
    if ping -c 1 -W 2 $PRODUCTION_SERVER >/dev/null 2>&1; then
        echo -e "${GREEN}✅ Production server ($PRODUCTION_SERVER) is reachable${NC}"
        return 0
    else
        echo -e "${RED}❌ Cannot reach production server ($PRODUCTION_SERVER)${NC}"
        return 1
    fi
}

# Function to deploy
deploy_to_production() {
    echo -e "${YELLOW}🚀 Deploying to production server...${NC}"
    echo ""
    
    ssh $SSH_USER@$PRODUCTION_SERVER "cd $PROJECT_PATH && echo '📥 Pulling latest changes...' && git pull && echo '' && echo '🔨 Rebuilding and restarting Docker containers...' && docker compose up --build -d && echo '' && echo '✅ Deployment complete!'"
    
    if [ $? -eq 0 ]; then
        echo ""
        echo -e "${GREEN}=================================================${NC}"
        echo -e "${GREEN}✅ Deployment Successful!${NC}"
        echo -e "${GREEN}=================================================${NC}"
        echo -e "${GREEN}🌐 Website: https://deafgain.org/${NC}"
        return 0
    else
        echo ""
        echo -e "${RED}=================================================${NC}"
        echo -e "${RED}❌ Deployment Failed${NC}"
        echo -e "${RED}=================================================${NC}"
        return 1
    fi
}

# Main deployment flow
main() {
    # Step 1: Check/Connect VPN
    if is_vpn_connected; then
        echo -e "${GREEN}✅ VPN already connected${NC}"
    else
        connect_vpn
    fi
    
    echo ""
    
    # Step 2: Test connectivity
    if ! test_connectivity; then
        echo -e "${RED}❌ Cannot proceed with deployment - server unreachable${NC}"
        exit 1
    fi
    
    echo ""
    
    # Step 3: Deploy
    deploy_to_production
}

# Run main function
main
