#!/usr/bin/env bash

set -euo pipefail

: "${OYSTERUN_STACK:=production}"

source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/oysterun_service_common.sh"

parse_common_args "$@"
configure_stack_runtime
require_host_port_configured
ensure_runtime_dirs
cleanup_legacy_home_port_host_jobs_if_needed
sync_host_stack_port_config

STARTED_HOST=0

cleanup_failed_start() {
  trap - ERR

  if [[ "${STARTED_HOST}" -eq 1 ]]; then
    stop_managed_process "${HOST_LABEL}" "${HOST_PID_FILE}" "Host service" >/dev/null 2>&1 || true
  fi
  clear_host_origin_file
  invalidate_routec_stack_readiness "start_failed" "tool_scripts/start_oysterun.sh" >/dev/null 2>&1 || true
}

trap cleanup_failed_start ERR

NODE_BIN="$(resolve_command_path node)"
if [[ -z "${NODE_BIN}" ]]; then
  echo "[oysterun-service] node is required but was not found in PATH" >&2
  exit 1
fi

ensure_stack_dashboard_credentials "${NODE_BIN}"
quarantine_legacy_routec_runtime_env_files_if_present "${NODE_BIN}"
cleanup_legacy_routec_spike0_synapse_processes_if_present "${NODE_BIN}"
invalidate_routec_stack_readiness "start_in_progress" "tool_scripts/start_oysterun.sh" >/dev/null

LAUNCH_PATH="$(build_launch_path)"
PERSISTENT_PLIST=""
if launch_agent_is_installed_for_label "${HOST_LABEL}"; then
  PERSISTENT_PLIST="$(launch_agent_plist_path_for_label "${HOST_LABEL}")"
fi

if [[ -z "${PERSISTENT_PLIST}" ]]; then
  ensure_process_not_running "${HOST_LABEL}" "${HOST_PID_FILE}" "Host service" "${HOST_PORT}"
  clear_host_origin_file
else
  clear_stale_pid_file "${HOST_PID_FILE}" "Host service"
fi

ensure_dashboard_static_app "${NODE_BIN}"
ensure_routec_web_chat_static_app

: > "${HOST_LOG}"

if [[ -n "${PERSISTENT_PLIST}" ]]; then
  current_pid="$(launchctl_pid_for_label "${HOST_LABEL}")"
  if [[ -n "${current_pid}" ]] && pid_is_running "${current_pid}"; then
    echo "[oysterun-service] error: Host service is already running with PID ${current_pid}" >&2
    exit 1
  fi
  if launch_service_is_loaded "${HOST_LABEL}"; then
    bootout_launch_agent_service "${HOST_LABEL}"
  fi
  rm -f "${HOST_PID_FILE}"
  clear_host_origin_file
  ensure_port_available "${HOST_PORT}"

  echo "[oysterun-service] Starting ${STACK_NAME} persistent LaunchAgent on ${HOST_URL}..."
  bootstrap_launch_agent_plist "${PERSISTENT_PLIST}"
  STARTED_HOST=1
  HOST_PID="$(launchctl_pid_for_label "${HOST_LABEL}")"
  wait_for_http "${HOST_URL}/health" "Host service" "${HOST_PID}" "${HOST_LOG}" "${HOST_LABEL}"
  HOST_PID="$(pid_from_file "${HOST_PID_FILE}")"
  if [[ -z "${HOST_PID}" ]]; then
    HOST_PID="$(launchctl_pid_for_label "${HOST_LABEL}")"
  fi
  verify_process_cwd "${HOST_PID}" "${HOST_DIR}" "Host service"
  write_host_origin_file "${HOST_PID}" "${HOST_LABEL}"
else
  echo "[oysterun-service] Starting ${STACK_NAME} Host service on ${HOST_URL}..."
  collect_routec_host_env_args
  printf -v HOST_COMMAND 'export PATH=%q; cd %q && exec /usr/bin/env OYSTERUN_CONFIG_DIR=%q OYSTERUN_PORT=%q OYSTERUN_REPO_ROOT=%q' "${LAUNCH_PATH}" "${HOST_DIR}" "${CONFIG_DIR}" "${HOST_PORT}" "${ROOT_DIR}"
  if (( ${#ROUTEC_HOST_ENV_ARGS[@]} > 0 )); then
    for routec_host_env_arg in "${ROUTEC_HOST_ENV_ARGS[@]}"; do
      printf -v HOST_COMMAND '%s %q' "${HOST_COMMAND}" "${routec_host_env_arg}"
    done
  fi
  printf -v HOST_COMMAND '%s %q server.mjs' "${HOST_COMMAND}" "${NODE_BIN}"
  submit_launchctl_job "${HOST_LABEL}" "${HOST_LOG}" "${HOST_COMMAND}"
  STARTED_HOST=1
  HOST_PID="$(launchctl_pid_for_label "${HOST_LABEL}")"
  wait_for_http "${HOST_URL}/health" "Host service" "${HOST_PID}" "${HOST_LOG}" "${HOST_LABEL}"
  write_pid_file_from_label "${HOST_LABEL}" "${HOST_PID_FILE}"
  HOST_PID="$(pid_from_file "${HOST_PID_FILE}")"
  verify_process_cwd "${HOST_PID}" "${HOST_DIR}" "Host service"
  write_host_origin_file "${HOST_PID}" "${HOST_LABEL}"
fi

write_current_routec_stack_readiness "tool_scripts/start_oysterun.sh" >/dev/null

trap - ERR

echo
if [[ -n "${PERSISTENT_PLIST}" ]]; then
  echo "[oysterun-service] LaunchAgent:      ${PERSISTENT_PLIST}"
fi
echo "[oysterun-service] Stack:          ${STACK_NAME}"
echo "[oysterun-service] Host health:    ${HOST_URL}/health"
echo "[oysterun-service] Web Client:    ${HOST_URL}/app"
echo "[oysterun-service] Host config:    ${CONFIG_DIR}"
echo "[oysterun-service] Host log:       ${HOST_LOG}"
echo "[oysterun-service] Host origin:    ${HOST_ORIGIN_FILE}"
echo "[oysterun-service] Host PID:       ${HOST_PID}"
