#!/bin/sh
set -e

# Docker entrypoint script for MonacoUSA Portal
echo "Starting MonacoUSA Portal..."

# Check if .env file exists in volume
if [ -f "/app/data/.env" ]; then
    echo "Using .env file from volume..."
    cp /app/data/.env /app/.env
else
    echo "Warning: No .env file found in volume. Using environment variables only."
fi

# Validate required environment variables
if [ -z "$NUXT_KEYCLOAK_ISSUER" ] && [ ! -f "/app/.env" ]; then
    echo "Error: NUXT_KEYCLOAK_ISSUER is required"
    exit 1
fi

if [ -z "$NUXT_NOCODB_URL" ] && [ ! -f "/app/.env" ]; then
    echo "Error: NUXT_NOCODB_URL is required"
    exit 1
fi

# Wait for dependencies to be ready (optional)
if [ -n "$WAIT_FOR_SERVICES" ]; then
    echo "Waiting for services to be ready..."
    sleep 10
fi

# Start the application
echo "Starting Nuxt application on port 3000..."
exec node .output/server/index.mjs
