#!/bin/bash
#
# Church Music Database - Nginx Setup Script
# Configures Nginx reverse proxy for houseofprayer.ddns.net
#
# Usage: sudo ./setup-nginx.sh
#

set -e

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

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

echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}Church Music Database - Nginx Setup${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""

PROJECT_DIR="/media/pts/Website/Church_HOP_MusicData"
NGINX_CONF="$PROJECT_DIR/nginx-http.conf"
SITES_AVAILABLE="/etc/nginx/sites-available/church-music"
SITES_ENABLED="/etc/nginx/sites-enabled/church-music"

# Step 1: Check if services are running
echo -e "${YELLOW}[1/7] Checking if backend and frontend services are running...${NC}"
if ! systemctl is-active --quiet church-music-backend; then
    echo -e "${RED}Error: Backend service is not running!${NC}"
    echo "Start it with: sudo systemctl start church-music-backend"
    exit 1
fi
if ! systemctl is-active --quiet church-music-frontend; then
    echo -e "${RED}Error: Frontend service is not running!${NC}"
    echo "Start it with: sudo systemctl start church-music-frontend"
    exit 1
fi
echo -e "${GREEN}✓ Backend and frontend services are running${NC}"
echo ""

# Step 2: Backup existing nginx config (if exists)
echo -e "${YELLOW}[2/7] Backing up existing nginx configuration...${NC}"
if [ -f "$SITES_AVAILABLE" ]; then
    cp "$SITES_AVAILABLE" "$SITES_AVAILABLE.backup.$(date +%Y%m%d_%H%M%S)"
    echo -e "${GREEN}✓ Backup created${NC}"
else
    echo -e "${GREEN}✓ No existing config to backup${NC}"
fi
echo ""

# Step 3: Remove default nginx site
echo -e "${YELLOW}[3/7] Removing default nginx site...${NC}"
if [ -L "/etc/nginx/sites-enabled/default" ]; then
    rm /etc/nginx/sites-enabled/default
    echo -e "${GREEN}✓ Default site removed${NC}"
else
    echo -e "${GREEN}✓ Default site not present${NC}"
fi
echo ""

# Step 4: Copy nginx configuration
echo -e "${YELLOW}[4/7] Installing nginx configuration...${NC}"
cp "$NGINX_CONF" "$SITES_AVAILABLE"
chmod 644 "$SITES_AVAILABLE"
echo -e "${GREEN}✓ Configuration copied to $SITES_AVAILABLE${NC}"
echo ""

# Step 5: Create symbolic link
echo -e "${YELLOW}[5/7] Enabling site configuration...${NC}"
if [ -L "$SITES_ENABLED" ]; then
    rm "$SITES_ENABLED"
fi
ln -s "$SITES_AVAILABLE" "$SITES_ENABLED"
echo -e "${GREEN}✓ Site enabled${NC}"
echo ""

# Step 6: Test nginx configuration
echo -e "${YELLOW}[6/7] Testing nginx configuration...${NC}"
if nginx -t; then
    echo -e "${GREEN}✓ Nginx configuration is valid${NC}"
else
    echo -e "${RED}✗ Nginx configuration has errors!${NC}"
    exit 1
fi
echo ""

# Step 7: Restart nginx
echo -e "${YELLOW}[7/7] Restarting nginx...${NC}"
systemctl restart nginx
systemctl enable nginx
sleep 2
echo -e "${GREEN}✓ Nginx restarted and enabled${NC}"
echo ""

# Verify nginx is running
if systemctl is-active --quiet nginx; then
    echo -e "${GREEN}========================================${NC}"
    echo -e "${GREEN}Nginx Setup Complete!${NC}"
    echo -e "${GREEN}========================================${NC}"
    echo ""
    echo -e "${GREEN}✓ Your site is now accessible at:${NC}"
    echo -e "${GREEN}  http://houseofprayer.ddns.net${NC}"
    echo ""
    echo -e "${YELLOW}Testing endpoints:${NC}"
    echo ""
    
    # Test health endpoint
    if curl -s http://localhost/api/health > /dev/null; then
        echo -e "${GREEN}✓ Backend API: Working${NC}"
    else
        echo -e "${RED}✗ Backend API: Not responding${NC}"
    fi
    
    if curl -s http://localhost/ > /dev/null; then
        echo -e "${GREEN}✓ Frontend: Working${NC}"
    else
        echo -e "${RED}✗ Frontend: Not responding${NC}"
    fi
    
    echo ""
    echo -e "${YELLOW}Important:${NC}"
    echo "1. Make sure port 80 is forwarded on your router to this server"
    echo "2. Your DNS houseofprayer.ddns.net should point to your public IP"
    echo "3. Test from outside: http://houseofprayer.ddns.net"
    echo ""
    echo -e "${YELLOW}Commands:${NC}"
    echo "  sudo systemctl status nginx"
    echo "  sudo systemctl restart nginx"
    echo "  sudo nginx -t  # Test configuration"
    echo "  sudo tail -f /var/log/nginx/church-music-access.log"
    echo "  sudo tail -f /var/log/nginx/church-music-error.log"
    echo ""
else
    echo -e "${RED}✗ Nginx failed to start!${NC}"
    echo "Check logs: sudo journalctl -u nginx -n 50"
    exit 1
fi
