#!/usr/bin/env bash
# Frontend Fix Script - Run this on Proxmox Host

set -e

VMID=10130
FRONTEND_DIR="/opt/dbis-core/frontend"

echo "========================================="
echo "Frontend Deployment Fix"
echo "========================================="
echo ""

# Check if pct command exists (Proxmox host)
if ! command -v pct &> /dev/null; then
    echo "❌ ERROR: This script must be run on the Proxmox host"
    echo "   The 'pct' command is required to access containers"
    echo ""
    echo "Please SSH into your Proxmox host and run:"
    echo "  cd /home/intlc/projects/proxmox/dbis_core"
    echo "  ./scripts/run-frontend-fix.sh"
    exit 1
fi

# Check container exists
if ! pct status $VMID &>/dev/null; then
    echo "❌ ERROR: Container $VMID not found"
    exit 1
fi

echo "✅ Container $VMID found"
echo ""

# Check if container is running
CONTAINER_STATUS=$(pct status $VMID 2>/dev/null | awk '{print $2}' || echo "stopped")
if [ "$CONTAINER_STATUS" != "running" ]; then
    echo "Starting container $VMID..."
    pct start $VMID
    sleep 5
fi

echo "Container is running"
echo ""

# Step 1: Check if frontend directory exists
echo "Step 1: Checking frontend directory..."
if pct exec $VMID -- test -d "$FRONTEND_DIR"; then
    echo "✅ Frontend directory exists: $FRONTEND_DIR"
else
    echo "❌ Frontend directory not found: $FRONTEND_DIR"
    echo "   Please check the deployment configuration"
    exit 1
fi
echo ""

# Step 2: Install dependencies
echo "Step 2: Installing dependencies..."
pct exec $VMID -- bash -c "cd $FRONTEND_DIR && npm install" 2>&1 | grep -vE "(perl: warning|locale:)" || {
    echo "⚠️  npm install had warnings, continuing..."
}
echo "✅ Dependencies installed"
echo ""

# Step 3: Build frontend
echo "Step 3: Building frontend application..."
if pct exec $VMID -- bash -c "cd $FRONTEND_DIR && npm run build" 2>&1 | grep -vE "(perl: warning|locale:)"; then
    echo "✅ Build completed"
else
    echo "❌ Build failed - check errors above"
    exit 1
fi
echo ""

# Step 4: Verify build
echo "Step 4: Verifying build..."
if pct exec $VMID -- test -f "$FRONTEND_DIR/dist/index.html"; then
    echo "✅ index.html exists"
    JS_COUNT=$(pct exec $VMID -- bash -c "ls -1 $FRONTEND_DIR/dist/*.js 2>/dev/null | wc -l" || echo "0")
    echo "✅ Found $JS_COUNT JavaScript files"
else
    echo "❌ Build verification failed - index.html not found"
    exit 1
fi
echo ""

# Step 5: Restart nginx
echo "Step 5: Restarting nginx..."
if pct exec $VMID -- systemctl restart nginx 2>&1 | grep -vE "(perl: warning|locale:)"; then
    echo "✅ Nginx restarted"
else
    echo "⚠️  Nginx restart had issues, checking status..."
    pct exec $VMID -- systemctl status nginx --no-pager -l | head -10 || true
fi

# Verify nginx is running
if pct exec $VMID -- systemctl is-active --quiet nginx; then
    echo "✅ Nginx is running"
else
    echo "❌ Nginx is not running"
    exit 1
fi
echo ""

# Step 6: Verify nginx configuration
echo "Step 6: Verifying nginx configuration..."
NGINX_ROOT=$(pct exec $VMID -- bash -c "grep 'root' /etc/nginx/sites-available/dbis-frontend 2>/dev/null | head -1 | awk '{print \$2}' | tr -d ';'" || echo "")
if [ -n "$NGINX_ROOT" ]; then
    echo "✅ Nginx root directory: $NGINX_ROOT"
    if [ "$NGINX_ROOT" != "$FRONTEND_DIR/dist" ]; then
        echo "⚠️  WARNING: Nginx root doesn't match expected path"
        echo "   Expected: $FRONTEND_DIR/dist"
        echo "   Found: $NGINX_ROOT"
    fi
else
    echo "⚠️  Could not read nginx configuration"
fi
echo ""

echo "========================================="
echo "✅ Frontend deployment fixed!"
echo "========================================="
echo ""
echo "Frontend should now be accessible at:"
echo "  http://192.168.11.130"
echo ""
echo "Next steps:"
echo "  1. Clear your browser cache (Ctrl+Shift+R)"
echo "  2. Refresh the page"
echo "  3. You should see the React app, not the placeholder"
echo ""
