#!/bin/bash

# ============================================================
# ServChan DEV Instance Startup Script
# ============================================================
#
# Purpose:
#   Safely run a development/test instance of the bot without
#   affecting the production instance.
#
# Usage:
#   ./dev-start.sh
#
# Configuration (edit these values as needed):
# ------------------------------------------------------------

# === DEV INSTANCE CONFIGURATION ===
# Never commit real tokens.
#
# The application requires ALL secrets via environment variables.
# Example:
#   WEBEX_BOT_TOKEN="dev-..." \
#   SC_CLIENT_ID="..." SC_CLIENT_SECRET="..." \
#   SC_USERNAME="..." SC_PASSWORD="..." \
#   XAI_TOKEN="..." \
#   ./dev-start.sh
#
# The application now respects WEBEX_BOT_TOKEN (and other *_TOKEN / SC_* variables)
# even if they exist in .env or config/config.json.

if [ -z "$WEBEX_BOT_TOKEN" ]; then
  echo "ERROR: WEBEX_BOT_TOKEN is not set."
  echo "Please run as:  WEBEX_BOT_TOKEN=\"your-dev-token\" ./dev-start.sh"
  exit 1
fi

# Use a different port so it doesn't conflict with production (which uses 1458)
export PORT=1460

# Use a completely separate database file for testing
export DB_PATH="./data/dev_webex_sc_mappings.db"

# Set environment for clarity in logs
export NODE_ENV=development

# You can override CS_API_BASE here if you want to point the dev bot
# at a different backend. Leaving it unset will use whatever is in .env
# or the default.
# export CS_API_BASE="https://bot.joesjavajoint.com/CollabSupport"

# ============================================================

echo "=============================================================="
echo "  Starting ServChan DEV instance"
echo "=============================================================="
echo ""
echo "  Port:           $PORT"
echo "  Database:       $DB_PATH"
echo "  Environment:    $NODE_ENV"
echo "  Bot Token:      (provided via WEBEX_BOT_TOKEN env var)"
echo ""
echo "  Health checks will be available at:"
echo "    http://localhost:$PORT/healthz"
echo "    http://localhost:$PORT/health"
echo ""
echo "  To test commands, mention the DEV bot in Webex."
echo ""
echo "=============================================================="
echo ""

# Ensure the data directory exists
mkdir -p "$(dirname "$DB_PATH")"

# Start the application
npm run dev
