#!/bin/bash
#
# Fix CORS issues for nginx deployment
#

set -e

echo "=========================================="
echo "CORS Configuration Fix"
echo "=========================================="
echo ""

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

# 1. Update nginx config
echo -e "${YELLOW}[1/3] Updating nginx configuration...${NC}"
if [ -f "/etc/nginx/sites-available/aumentum" ]; then
    # Backup existing config
    sudo cp /etc/nginx/sites-available/aumentum /etc/nginx/sites-available/aumentum.backup.$(date +%Y%m%d_%H%M%S)
    echo -e "${GREEN}✓ Backup created${NC}"
    
    # Copy new config
    sudo cp "$(dirname "$0")/../nginx/aumentum.conf" /etc/nginx/sites-available/aumentum
    echo -e "${GREEN}✓ Nginx config updated${NC}"
    
    # Test nginx config
    if sudo nginx -t; then
        echo -e "${GREEN}✓ Nginx configuration is valid${NC}"
        sudo systemctl reload nginx
        echo -e "${GREEN}✓ Nginx reloaded${NC}"
    else
        echo -e "${RED}❌ Nginx configuration error${NC}"
        echo "Restoring backup..."
        sudo cp /etc/nginx/sites-available/aumentum.backup.* /etc/nginx/sites-available/aumentum
        exit 1
    fi
else
    echo -e "${YELLOW}⚠ Nginx config not found at /etc/nginx/sites-available/aumentum${NC}"
    echo "Creating new config..."
    sudo cp "$(dirname "$0")/../nginx/aumentum.conf" /etc/nginx/sites-available/aumentum
    sudo ln -sf /etc/nginx/sites-available/aumentum /etc/nginx/sites-enabled/aumentum
    sudo nginx -t && sudo systemctl reload nginx
    echo -e "${GREEN}✓ Nginx config created and enabled${NC}"
fi
echo ""

# 2. Restart backend API to apply CORS changes
echo -e "${YELLOW}[2/3] Restarting backend API...${NC}"
if systemctl is-active --quiet plagis-aumentum-api; then
    sudo systemctl restart plagis-aumentum-api
    sleep 2
    if systemctl is-active --quiet plagis-aumentum-api; then
        echo -e "${GREEN}✓ Backend API restarted${NC}"
    else
        echo -e "${RED}❌ Backend API failed to restart${NC}"
        sudo systemctl status plagis-aumentum-api --no-pager | head -20
    fi
else
    echo -e "${YELLOW}⚠ Backend API is not running${NC}"
fi
echo ""

# 3. Rebuild frontend to apply API URL changes
echo -e "${YELLOW}[3/3] Rebuilding frontend...${NC}"
cd "$(dirname "$0")/../plagis-nextjs"
if [ -f "package.json" ]; then
    npm run build
    echo -e "${GREEN}✓ Frontend rebuilt${NC}"
    
    # Restart frontend
    if systemctl is-active --quiet plagis-nextjs; then
        sudo systemctl restart plagis-nextjs
        echo -e "${GREEN}✓ Frontend restarted${NC}"
    fi
else
    echo -e "${RED}❌ Frontend directory not found${NC}"
fi
echo ""

echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}CORS Configuration Complete!${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
echo "Changes made:"
echo "1. ✅ Updated nginx config with CORS headers"
echo "2. ✅ Updated backend API CORS settings"
echo "3. ✅ Updated frontend to use nginx proxy path"
echo ""
echo "Test the application:"
echo "  http://10.10.10.127:7000"
echo ""
echo "If you still see CORS errors:"
echo "1. Check browser console for specific error"
echo "2. Verify nginx is running: sudo systemctl status nginx"
echo "3. Check nginx logs: sudo tail -f /var/log/nginx/error.log"
echo "4. Check backend logs: sudo tail -f /var/log/plagis/api.log"
echo ""

