#!/usr/bin/env bash

set -euo pipefail

#
# Starts up "live" local builds of the posthog Go
# services for a faster local dev inner loop
#

# set exit strategy
trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT

# ensure we have a service name
if [ -z "${1:-}" ]; then
  echo "ERROR missing required argument: Go service name" >&2
  echo "Usage: bin/start-go-service <SERVICE_NAME>" >&2
  exit 1
fi
GO_SVC="$1"

# set up env appropriate for each *known* service - add new ones here!
case "$GO_SVC" in
  livestream)
    # Download MaxMind DB if it doesn't exist
    ./bin/download-mmdb

    # Core settings
    export LIVESTREAM_DEBUG=true
    export PORT="8666"

    # Consumer settings (per-topic Kafka config)
    export LIVESTREAM_CONSUMERS_EVENT_ENABLED=true
    export LIVESTREAM_CONSUMERS_EVENT_BROKERS=localhost:9092
    export LIVESTREAM_CONSUMERS_EVENT_TOPIC=events_plugin_ingestion
    export LIVESTREAM_CONSUMERS_EVENT_GROUP_ID=livestream-dev
    export LIVESTREAM_CONSUMERS_EVENT_SECURITY_PROTOCOL=PLAINTEXT
    export LIVESTREAM_CONSUMERS_EVENT_SESSION_TIMEOUT_MS=6000
    export LIVESTREAM_CONSUMERS_EVENT_HEARTBEAT_INTERVAL_MS=1000
    export LIVESTREAM_CONSUMERS_EVENT_MAX_POLL_INTERVAL_MS=6000

    export LIVESTREAM_CONSUMERS_SESSION_RECORDING_ENABLED=true
    export LIVESTREAM_CONSUMERS_SESSION_RECORDING_BROKERS=localhost:9092
    export LIVESTREAM_CONSUMERS_SESSION_RECORDING_TOPIC=session_recording_snapshot_item_events
    export LIVESTREAM_CONSUMERS_SESSION_RECORDING_GROUP_ID=livestream-dev-session-recordings
    export LIVESTREAM_CONSUMERS_SESSION_RECORDING_SECURITY_PROTOCOL=PLAINTEXT

    export LIVESTREAM_CONSUMERS_NOTIFICATION_ENABLED=true
    export LIVESTREAM_CONSUMERS_NOTIFICATION_BROKERS=localhost:9092
    export LIVESTREAM_CONSUMERS_NOTIFICATION_TOPIC=notification_events
    export LIVESTREAM_CONSUMERS_NOTIFICATION_GROUP_ID=livestream-dev-notifications
    export LIVESTREAM_CONSUMERS_NOTIFICATION_SECURITY_PROTOCOL=PLAINTEXT
    export LIVESTREAM_REDIS_ADDRESS=localhost
    export LIVESTREAM_REDIS_PORT=6399
    export LIVESTREAM_REDIS_TLS=false
    export LIVESTREAM_REDIS_USE_PUB_SUB=true

    # MMDB path, relative to livestream directory where air runs
    export LIVESTREAM_MMDB_PATH=../share/GeoLite2-City.mmdb

    # Redis (needed for real-time notifications SSE endpoint)
    export LIVESTREAM_REDIS_URL="redis://localhost:6379"

    # JWT secret
    export LIVESTREAM_JWT_SECRET="<randomly generated secret key>"

    # ensure we're in the livestream directory for air
    cd livestream
    ;;

  *)
    echo "ERROR unknown Go service '$GO_SVC' - please register in: $0" >&2
    exit 1
    ;;
esac

# check if air is available for hot reloading
if command -v air &>/dev/null; then
  USE_AIR=true
else
  USE_AIR=false
  echo -e "\n\033[33mWarning: 'air' not found. Running without hot reload.\033[0m"
  echo -e "Install air for hot reloading: go install github.com/air-verse/air@latest\n"
fi

# ensure the service restarts if it crashes in dev
sleep 1
while true; do
  if ! ps -p ${GO_PID:-} &>/dev/null; then
    if [ "$USE_AIR" = true ]; then
      air &
    else
      go run . &
    fi
    export GO_PID=$!
    echo -e "\n\033[47m>>\033[0m Restarted '$GO_SVC' with PID $GO_PID\n"
  fi

  # restart subprocess if it fails without bin/start shutdown
  set +e
  wait $GO_PID
  set -e

  sleep 3
done

echo -e "\n\033[47m>>\033[0m Shut down '$GO_SVC'\n"
exit 0
