#!/usr/bin/env bash
# Phone Agent Private Server helper commands.
#
# Usage:
#   source ./phoneagent-private-server-aliases.sh
#   pa-init-env
#   pa-load phoneagent-private-server-v0.1.1.tar
#   pa-start
#   pa-health
#   pa-webchat
#   pa-plans

export PA_IMAGE="${PA_IMAGE:-phonerelay/private-server:v0.1.1}"
export PA_CONTAINER="${PA_CONTAINER:-phoneagent-server}"
export PA_VOLUME="${PA_VOLUME:-phoneagent-data}"
export PA_HOST_PORT="${PA_HOST_PORT:-8443}"
export PA_CONTAINER_PORT="${PA_CONTAINER_PORT:-8443}"
export PA_ENV_FILE="${PA_ENV_FILE:-.phoneagent-private.env}"

pa-secret() {
  python3 -c 'import secrets; print(secrets.token_urlsafe(48))'
}

pa-init-env() {
  if [ -f "$PA_ENV_FILE" ]; then
    echo "$PA_ENV_FILE already exists"
    return 0
  fi
  {
    printf 'AUTH_SECRET_KEY=%s\n' "$(pa-secret)"
    printf 'MIDDLE_ADMIN_TOKEN=%s\n' "$(pa-secret)"
    printf 'GTS_DEVICE_PEPPER_RING=v1:%s\n' "$(pa-secret)"
    printf 'PHONEAGENT_PUBLIC_HOST=127.0.0.1\n'
  } > "$PA_ENV_FILE"
  chmod 600 "$PA_ENV_FILE"
  echo "Created $PA_ENV_FILE"
}

pa-load() {
  local tar_file="${1:-phoneagent-private-server-v0.1.1.tar}"
  docker load -i "$tar_file"
}

pa-start() {
  if [ ! -f "$PA_ENV_FILE" ]; then
    echo "Missing $PA_ENV_FILE. Run: pa-init-env"
    return 2
  fi
  if docker ps -a --format '{{.Names}}' | grep -Fxq "$PA_CONTAINER"; then
    docker start "$PA_CONTAINER"
    return
  fi
  docker run -d \
    --name "$PA_CONTAINER" \
    --env-file "$PA_ENV_FILE" \
    -p "$PA_HOST_PORT:$PA_CONTAINER_PORT" \
    -v "$PA_VOLUME:/data" \
    "$PA_IMAGE"
}

pa-restart() {
  docker restart "$PA_CONTAINER"
}

pa-stop() {
  docker stop "$PA_CONTAINER"
}

pa-rm() {
  docker rm "$PA_CONTAINER"
}

pa-logs() {
  docker logs -f "$PA_CONTAINER"
}

pa-health() {
  curl -fsS "http://127.0.0.1:$PA_HOST_PORT/health"
  printf '\n'
}

pa-shell() {
  docker exec -it "$PA_CONTAINER" sh
}

pa-status() {
  docker ps --filter "name=$PA_CONTAINER"
}

pa-open() {
  printf 'Private Server: http://127.0.0.1:%s\n' "$PA_HOST_PORT"
  printf 'Health:         http://127.0.0.1:%s/health\n' "$PA_HOST_PORT"
  printf 'WebChat:        http://127.0.0.1:%s/app/webchat/chat.html\n' "$PA_HOST_PORT"
}

pa-webchat() {
  local url="http://127.0.0.1:$PA_HOST_PORT/app/webchat/chat.html"
  if command -v xdg-open >/dev/null 2>&1; then
    xdg-open "$url" >/dev/null 2>&1 &
  elif command -v open >/dev/null 2>&1; then
    open "$url" >/dev/null 2>&1 &
  else
    printf '%s\n' "$url"
  fi
}

pa-site-open() {
  local url="$1"
  echo "$url"
  if command -v xdg-open >/dev/null 2>&1; then
    xdg-open "$url" >/dev/null 2>&1 &
  elif command -v open >/dev/null 2>&1; then
    open "$url" >/dev/null 2>&1 &
  fi
}

pa-plans() {
  pa-site-open "https://phonerelay.dev/subscribe.html"
}

pa-account() {
  pa-site-open "https://phonerelay.dev/account.html"
}

pa-private-docs() {
  pa-site-open "https://phonerelay.dev/docs/private-server.html"
}

alias pa-down='pa-stop'
alias pa-up='pa-start'

echo "Phone Agent aliases loaded: pa-init-env, pa-load, pa-start, pa-health, pa-logs, pa-webchat, pa-plans"
