#!/bin/bash

# Quick fix script for services slug page issues
# Fixes:
# 1. Backend ALLOWED_HOSTS (adds gnxsoft.com if missing)
# 2. Frontend standalone mode startup
# 3. Restarts services

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

echo -e "${BLUE}=========================================="
echo "Fixing Services Slug Page Issues"
echo "==========================================${NC}"
echo ""

BACKEND_DIR="/var/www/GNX-WEB/backEnd"
FRONTEND_DIR="/var/www/GNX-WEB/frontEnd"
BACKEND_ENV="$BACKEND_DIR/.env"

# 1. Fix ALLOWED_HOSTS in backend .env
echo -e "${BLUE}[1/3] Fixing backend ALLOWED_HOSTS...${NC}"
if [ -f "$BACKEND_ENV" ]; then
    # Check if gnxsoft.com is in ALLOWED_HOSTS
    if grep -q "^ALLOWED_HOSTS=" "$BACKEND_ENV"; then
        current_hosts=$(grep "^ALLOWED_HOSTS=" "$BACKEND_ENV" | cut -d'=' -f2-)
        
        if echo "$current_hosts" | grep -q "gnxsoft.com"; then
            echo -e "${GREEN}✓ gnxsoft.com already in ALLOWED_HOSTS${NC}"
        else
            echo -e "${YELLOW}Adding gnxsoft.com to ALLOWED_HOSTS...${NC}"
            # Add gnxsoft.com if not present
            if [[ "$current_hosts" == *"gnxsoft.com"* ]]; then
                echo -e "${GREEN}✓ Already present${NC}"
            else
                # Remove any trailing spaces and add gnxsoft.com
                new_hosts="${current_hosts},gnxsoft.com,www.gnxsoft.com"
                sed -i "s|^ALLOWED_HOSTS=.*|ALLOWED_HOSTS=$new_hosts|" "$BACKEND_ENV"
                echo -e "${GREEN}✓ Added gnxsoft.com and www.gnxsoft.com to ALLOWED_HOSTS${NC}"
            fi
        fi
    else
        echo -e "${YELLOW}ALLOWED_HOSTS not found. Adding...${NC}"
        echo "ALLOWED_HOSTS=gnxsoft.com,www.gnxsoft.com,localhost,127.0.0.1" >> "$BACKEND_ENV"
        echo -e "${GREEN}✓ Added ALLOWED_HOSTS${NC}"
    fi
else
    echo -e "${RED}✗ Backend .env file not found at $BACKEND_ENV${NC}"
    exit 1
fi

echo ""

# 2. Fix frontend startup (standalone mode)
echo -e "${BLUE}[2/3] Fixing frontend startup for standalone mode...${NC}"
cd "$FRONTEND_DIR"

# Check if standalone mode is enabled
if grep -q '"output":\s*"standalone"' next.config.js 2>/dev/null || grep -q "output:.*'standalone'" next.config.js 2>/dev/null; then
    echo -e "${GREEN}✓ Standalone mode detected${NC}"
    
    # Check if standalone server exists
    if [ ! -f ".next/standalone/server.js" ]; then
        echo -e "${YELLOW}Standalone server not found. Rebuilding frontend...${NC}"
        NODE_ENV=production npm run build
    else
        echo -e "${GREEN}✓ Standalone server exists${NC}"
    fi
    
    # Stop existing frontend if running
    if pm2 list | grep -q "gnxsoft-frontend"; then
        echo -e "${YELLOW}Stopping existing frontend...${NC}"
        pm2 delete gnxsoft-frontend 2>/dev/null || true
        sleep 2
    fi
    
    # Start with standalone server
    echo -e "${BLUE}Starting frontend with standalone server...${NC}"
    PORT=1087 NODE_ENV=production pm2 start node \
        --name "gnxsoft-frontend" \
        --cwd "$FRONTEND_DIR" \
        -- \
        ".next/standalone/server.js"
    
    echo -e "${GREEN}✓ Frontend started in standalone mode${NC}"
else
    echo -e "${YELLOW}⚠ Standalone mode not detected. Using standard startup...${NC}"
    
    # Stop existing frontend if running
    if pm2 list | grep -q "gnxsoft-frontend"; then
        echo -e "${YELLOW}Stopping existing frontend...${NC}"
        pm2 delete gnxsoft-frontend 2>/dev/null || true
        sleep 2
    fi
    
    # Start with npm start
    PORT=1087 NODE_ENV=production pm2 start npm \
        --name "gnxsoft-frontend" \
        -- start
    
    echo -e "${GREEN}✓ Frontend started in standard mode${NC}"
fi

echo ""

# 3. Restart backend to apply ALLOWED_HOSTS changes
echo -e "${BLUE}[3/3] Restarting backend to apply changes...${NC}"
if pm2 list | grep -q "gnxsoft-backend"; then
    pm2 restart gnxsoft-backend
    echo -e "${GREEN}✓ Backend restarted${NC}"
else
    echo -e "${YELLOW}⚠ Backend not running in PM2${NC}"
fi

echo ""

# Save PM2 configuration
pm2 save

echo -e "${GREEN}=========================================="
echo "Fix Complete!"
echo "==========================================${NC}"
echo ""
echo -e "${BLUE}Summary of changes:${NC}"
echo "  1. ✓ Backend ALLOWED_HOSTS updated"
echo "  2. ✓ Frontend restarted in standalone mode"
echo "  3. ✓ Backend restarted"
echo ""
echo -e "${BLUE}Verification:${NC}"
echo "  - Check frontend port: lsof -Pi :1087 -sTCP:LISTEN"
echo "  - Check backend port: lsof -Pi :1086 -sTCP:LISTEN"
echo "  - Test service page: curl -I https://gnxsoft.com/services/YOUR-SLUG"
echo "  - View logs: pm2 logs gnxsoft-frontend --lines 20"
echo ""

