#!/usr/bin/with-contenv bashio
# ==============================================================================
# Home Assistant Add-on: MCP Grocy API
#
# This script starts the MCP Grocy API Server.
# ==============================================================================
set -e # Exit immediately if a command exits with a non-zero status.
set -x # Print commands and their arguments as they are executed.

# --- Read configuration from config.yaml ---
export GROCY_BASE_URL=$(bashio::config 'grocy_base_url')
export GROCY_APIKEY_VALUE=$(bashio::config 'grocy_api_key')
export GROCY_ENABLE_SSL_VERIFY=$(bashio::config 'enable_ssl_verify')
export REST_RESPONSE_SIZE_LIMIT=$(bashio::config 'response_size_limit')
export ENABLE_HTTP_SERVER=$(bashio::config 'enable_http_server')
export HTTP_SERVER_PORT=$(bashio::config 'http_server_port')

bashio::log.info "Starting Grocy API Server..."
bashio::log.info "Grocy URL: ${GROCY_BASE_URL}"
bashio::log.info "SSL Verification: ${GROCY_ENABLE_SSL_VERIFY}"
bashio::log.info "Response Size Limit: ${REST_RESPONSE_SIZE_LIMIT}"
bashio::log.info "Streamable HTTP server: ${ENABLE_HTTP_SERVER}"
bashio::log.info "HTTP Server Port: ${HTTP_SERVER_PORT}"

# --- Define application paths ---
# APP_BASE_DIR is the working directory set in Dockerfile, and where app files are copied.
APP_BASE_DIR="/app"

# --- Navigate to application base directory and start the server ---
cd "${APP_BASE_DIR}" || exit 1

bashio::log.info "Current directory: $(pwd)"
bashio::log.info "Listing contents of build directory:"
ls -la build

bashio::log.info "Checking if build/index.js exists..."
if [ ! -f "build/index.js" ]; then
  bashio::log.error "build/index.js not found!"
  exit 1
fi
bashio::log.info "build/index.js found."

# --- Test environment variable visibility for Node.js ---
bashio::log.info "Testing Node.js environment variable visibility..."
node -e "console.log('[NODE_TEST] GROCY_BASE_URL: ' + process.env.GROCY_BASE_URL);"
node -e "console.log('[NODE_TEST] GROCY_APIKEY_VALUE: ' + (process.env.GROCY_APIKEY_VALUE ? 'SET' : 'NOT SET'));"
node -e "console.log('[NODE_TEST] GROCY_ENABLE_SSL_VERIFY: ' + process.env.GROCY_ENABLE_SSL_VERIFY);"
node -e "console.log('[NODE_TEST] REST_RESPONSE_SIZE_LIMIT: ' + process.env.REST_RESPONSE_SIZE_LIMIT);"
node -e "console.log('[NODE_TEST] ENABLE_HTTP_SERVER: ' + process.env.ENABLE_HTTP_SERVER);"
node -e "console.log('[NODE_TEST] HTTP_SERVER_PORT: ' + process.env.HTTP_SERVER_PORT);"
bashio::log.info "Finished testing Node.js environment variable visibility."

bashio::log.info "Executing Node.js application: node build/index.js"

# Start the application
exec node build/index.js