#!/bin/bash

# PHI|OS Uniphil.ch Nginx Setup Only
# Sets up just the nginx configuration without SSL or service startup

set -e

# Configuration
DOMAIN="uniphil.ch"
NGINX_AVAILABLE="/etc/nginx/sites-available/${DOMAIN}.conf"
NGINX_ENABLED="/etc/nginx/sites-enabled/${DOMAIN}.conf"
WEB_ROOT="/rhiz/PHI|OS/app/web"

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

echo -e "${YELLOW}[PHI|OS] Starting Uniphil.ch nginx setup...${NC}"

# Check if running as root
if [ "$(id -u)" -ne 0 ]; then
    echo -e "${RED}[ERROR] This script must be run as root${NC}"
    exit 1
fi

# Create nginx configuration
echo -e "${YELLOW}[NGINX] Creating configuration for ${DOMAIN}...${NC}"
cat > "${NGINX_AVAILABLE}" << 'EONGINX'
server {
    listen 80;
    server_name uniphil.ch www.uniphil.ch;
    
    # Proxy all requests to the PHI|OS container running on port 3300
    location / {
        proxy_pass http://localhost:3300;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
    
    # API proxy to PHI|OS services (port 3001)
    location /api/ {
        proxy_pass http://localhost:3001/api/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
    
    # Websocket support for realtime features
    location /ws/ {
        proxy_pass http://localhost:3001/ws/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
    }
}
EONGINX

echo -e "${GREEN}[NGINX] Configuration created${NC}"

# Enable the site
echo -e "${YELLOW}[NGINX] Enabling site...${NC}"
if [ -f "${NGINX_ENABLED}" ]; then
    rm "${NGINX_ENABLED}"
fi
ln -s "${NGINX_AVAILABLE}" "${NGINX_ENABLED}"
echo -e "${GREEN}[NGINX] Site enabled${NC}"

# Test nginx configuration
echo -e "${YELLOW}[NGINX] Testing configuration...${NC}"
nginx -t
echo -e "${GREEN}[NGINX] Configuration test passed${NC}"

# Reload nginx
echo -e "${YELLOW}[NGINX] Reloading nginx...${NC}"
systemctl reload nginx
echo -e "${GREEN}[NGINX] Nginx reloaded${NC}"

echo -e "${GREEN}"
echo -e "╔════════════════════════════════════════════════════════════╗"
echo -e "║        PHI|OS Uniphil.ch Nginx Setup Complete             ║"
echo -e "║                                                           ║"
echo -e "║  Domain: http://uniphil.ch                                ║"
echo -e "║  Web Root: /rhiz/PHI|OS/app/web                           ║"
echo -e "║  Nginx Config: /etc/nginx/sites-available/uniphil.ch.conf ║"
echo -e "║                                                           ║"
echo -e "║  Next steps:                                              ║"
echo -e "║  1. Set up SSL certificates (recommended)                  ║"
echo -e "║  2. Start PHI|OS services                                ║"
echo -e "║  3. Configure DNS for uniphil.ch                         ║"
echo -e "║                                                           ║"
echo -e "╚════════════════════════════════════════════════════════════╝"
echo -e "${NC}"

exit 0
