#!/bin/sh
set -e

echo "=== MonacoUSA Portal Debug Startup ==="
echo "Timestamp: $(date)"
echo "Node Version: $(node --version)"
echo "NPM Version: $(npm --version)"
echo "Working Directory: $(pwd)"
echo "User: $(whoami)"
echo "UID: $(id -u)"
echo "GID: $(id -g)"

echo ""
echo "=== Environment Variables ==="
echo "NODE_ENV: $NODE_ENV"
echo "NUXT_HOST: $NUXT_HOST"
echo "NUXT_PORT: $NUXT_PORT"
echo "NITRO_HOST: $NITRO_HOST"
echo "NITRO_PORT: $NITRO_PORT"

# Check if Keycloak variables are set
if [ -n "$NUXT_KEYCLOAK_ISSUER" ]; then
    echo "NUXT_KEYCLOAK_ISSUER: $NUXT_KEYCLOAK_ISSUER"
    echo "NUXT_KEYCLOAK_CLIENT_ID: $NUXT_KEYCLOAK_CLIENT_ID"
    echo "NUXT_KEYCLOAK_CLIENT_SECRET: [SET]"
    echo "NUXT_KEYCLOAK_CALLBACK_URL: $NUXT_KEYCLOAK_CALLBACK_URL"
else
    echo "⚠️  Keycloak variables not set"
fi

# Check if NocoDB variables are set
if [ -n "$NUXT_NOCODB_URL" ]; then
    echo "NUXT_NOCODB_URL: $NUXT_NOCODB_URL"
    echo "NUXT_NOCODB_TOKEN: [SET]"
    echo "NUXT_NOCODB_BASE_ID: $NUXT_NOCODB_BASE_ID"
else
    echo "⚠️  NocoDB variables not set"
fi

# Check session secrets
if [ -n "$NUXT_SESSION_SECRET" ]; then
    echo "NUXT_SESSION_SECRET: [SET - ${#NUXT_SESSION_SECRET} chars]"
else
    echo "❌ NUXT_SESSION_SECRET: NOT SET"
fi

if [ -n "$NUXT_ENCRYPTION_KEY" ]; then
    echo "NUXT_ENCRYPTION_KEY: [SET - ${#NUXT_ENCRYPTION_KEY} chars]"
else
    echo "❌ NUXT_ENCRYPTION_KEY: NOT SET"
fi

echo ""
echo "=== File System Check ==="
echo "Contents of /app:"
ls -la /app/

if [ -d "/app/.output" ]; then
    echo ""
    echo "Contents of /app/.output:"
    ls -la /app/.output/
    
    if [ -f "/app/.output/server/index.mjs" ]; then
        echo "✅ Server file exists: /app/.output/server/index.mjs"
        echo "Server file size: $(stat -c%s /app/.output/server/index.mjs) bytes"
    else
        echo "❌ Server file missing: /app/.output/server/index.mjs"
    fi
else
    echo "❌ .output directory missing!"
fi

echo ""
echo "=== Network Check ==="
echo "Checking if port $NUXT_PORT is available..."
if netstat -tuln | grep ":$NUXT_PORT "; then
    echo "⚠️  Port $NUXT_PORT is already in use"
else
    echo "✅ Port $NUXT_PORT is available"
fi

echo ""
echo "=== Service Connectivity Check ==="

# Test Keycloak connectivity
if [ -n "$NUXT_KEYCLOAK_ISSUER" ]; then
    echo "Testing Keycloak connectivity..."
    if wget -q --spider --timeout=10 "$NUXT_KEYCLOAK_ISSUER" 2>/dev/null; then
        echo "✅ Keycloak is reachable: $NUXT_KEYCLOAK_ISSUER"
    else
        echo "❌ Keycloak is NOT reachable: $NUXT_KEYCLOAK_ISSUER"
    fi
fi

# Test NocoDB connectivity
if [ -n "$NUXT_NOCODB_URL" ]; then
    echo "Testing NocoDB connectivity..."
    if wget -q --spider --timeout=10 "$NUXT_NOCODB_URL" 2>/dev/null; then
        echo "✅ NocoDB is reachable: $NUXT_NOCODB_URL"
    else
        echo "❌ NocoDB is NOT reachable: $NUXT_NOCODB_URL"
    fi
fi

echo ""
echo "=== Starting Application ==="
echo "Command: node .output/server/index.mjs"
echo "Starting at: $(date)"

# Set Node.js to output logs immediately
export NODE_OPTIONS="--max-old-space-size=8192 --trace-warnings"

# Start the application with verbose logging
exec node .output/server/index.mjs
