#!/usr/bin/env bash
set -euo pipefail

ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
LABEL="${AURORA_LAUNCHD_LABEL:-com.daarion.aurora}"
DOMAIN="gui/$(id -u)"
LAUNCH_AGENTS_DIR="${HOME}/Library/LaunchAgents"
PLIST_PATH="${LAUNCH_AGENTS_DIR}/${LABEL}.plist"
START_SCRIPT="${ROOT_DIR}/start-native-macos.sh"

PORT_VALUE="${PORT:-9401}"
AURORA_ENV_FILE_VALUE="${AURORA_ENV_FILE:-${HOME}/.sofiia/aurora.env}"
DATA_DIR_VALUE="${AURORA_DATA_DIR:-${HOME}/.sofiia/aurora-data}"
MODELS_DIR_VALUE="${AURORA_MODELS_DIR:-${DATA_DIR_VALUE}/models}"
PUBLIC_BASE_URL_VALUE="${AURORA_PUBLIC_BASE_URL:-http://127.0.0.1:${PORT_VALUE}}"
CORS_ORIGINS_VALUE="${AURORA_CORS_ORIGINS:-*}"
FORCE_CPU_VALUE="${AURORA_FORCE_CPU:-false}"
PREFER_MPS_VALUE="${AURORA_PREFER_MPS:-true}"
ENABLE_VTB_VALUE="${AURORA_ENABLE_VIDEOTOOLBOX:-true}"
KLING_ACCESS_KEY_VALUE="${KLING_ACCESS_KEY:-}"
KLING_SECRET_KEY_VALUE="${KLING_SECRET_KEY:-}"
KLING_BASE_URL_VALUE="${KLING_BASE_URL:-https://api.klingai.com}"
KLING_TIMEOUT_VALUE="${KLING_TIMEOUT:-60}"

LOG_DIR="${DATA_DIR_VALUE}/logs"
LOG_OUT="${LOG_DIR}/launchd.out.log"
LOG_ERR="${LOG_DIR}/launchd.err.log"
PATH_VALUE="${PATH:-/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin}"

if [ ! -x "${START_SCRIPT}" ]; then
  echo "[aurora-launchd] missing start script: ${START_SCRIPT}"
  exit 1
fi

if [ ! -x "${ROOT_DIR}/.venv-macos/bin/python" ]; then
  echo "[aurora-launchd] .venv-macos is missing. Run ./setup-native-macos.sh first."
  exit 1
fi

mkdir -p "${LAUNCH_AGENTS_DIR}" "${LOG_DIR}" "${DATA_DIR_VALUE}" "${MODELS_DIR_VALUE}"

cat > "${PLIST_PATH}" <<PLIST
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>${LABEL}</string>

  <key>ProgramArguments</key>
  <array>
    <string>${START_SCRIPT}</string>
  </array>

  <key>WorkingDirectory</key>
  <string>${ROOT_DIR}</string>

  <key>RunAtLoad</key>
  <true/>

  <key>KeepAlive</key>
  <true/>

  <key>StandardOutPath</key>
  <string>${LOG_OUT}</string>
  <key>StandardErrorPath</key>
  <string>${LOG_ERR}</string>

  <key>EnvironmentVariables</key>
  <dict>
    <key>PATH</key>
    <string>${PATH_VALUE}</string>
    <key>PYTHONUNBUFFERED</key>
    <string>1</string>
    <key>PORT</key>
    <string>${PORT_VALUE}</string>
    <key>AURORA_ENV_FILE</key>
    <string>${AURORA_ENV_FILE_VALUE}</string>
    <key>AURORA_DATA_DIR</key>
    <string>${DATA_DIR_VALUE}</string>
    <key>AURORA_MODELS_DIR</key>
    <string>${MODELS_DIR_VALUE}</string>
    <key>AURORA_PUBLIC_BASE_URL</key>
    <string>${PUBLIC_BASE_URL_VALUE}</string>
    <key>AURORA_CORS_ORIGINS</key>
    <string>${CORS_ORIGINS_VALUE}</string>
    <key>AURORA_FORCE_CPU</key>
    <string>${FORCE_CPU_VALUE}</string>
    <key>AURORA_PREFER_MPS</key>
    <string>${PREFER_MPS_VALUE}</string>
    <key>AURORA_ENABLE_VIDEOTOOLBOX</key>
    <string>${ENABLE_VTB_VALUE}</string>
    <key>KLING_ACCESS_KEY</key>
    <string>${KLING_ACCESS_KEY_VALUE}</string>
    <key>KLING_SECRET_KEY</key>
    <string>${KLING_SECRET_KEY_VALUE}</string>
    <key>KLING_BASE_URL</key>
    <string>${KLING_BASE_URL_VALUE}</string>
    <key>KLING_TIMEOUT</key>
    <string>${KLING_TIMEOUT_VALUE}</string>
  </dict>
</dict>
</plist>
PLIST

chmod 644 "${PLIST_PATH}"

launchctl bootout "${DOMAIN}/${LABEL}" >/dev/null 2>&1 || true
launchctl bootstrap "${DOMAIN}" "${PLIST_PATH}"
launchctl enable "${DOMAIN}/${LABEL}" >/dev/null 2>&1 || true
launchctl kickstart -k "${DOMAIN}/${LABEL}"

echo "[aurora-launchd] installed: ${PLIST_PATH}"
echo "[aurora-launchd] active label: ${DOMAIN}/${LABEL}"
echo "[aurora-launchd] logs: ${LOG_OUT} | ${LOG_ERR}"
echo "[aurora-launchd] check: launchctl print ${DOMAIN}/${LABEL}"
