#!/bin/bash

# omarchy:summary=Choose which Claude, Codex, or Grok account sessions use
# omarchy:args=[claude|codex|grok] [account|next]
# omarchy:examples=omarchy agent account use work | omarchy agent account use main | omarchy agent account use codex next

set -uo pipefail

# The provider can be left out when it's your default agent:
# `omarchy agent account use work`.
if [[ ${1:-} != "claude" && ${1:-} != "codex" && ${1:-} != "grok" ]]; then
  default=$(omarchy-default-agent)
  [[ $default == "claude" || $default == "codex" || $default == "grok" ]] && set -- "$default" "$@"
fi

case "${1:-}" in
claude) name="Claude" ;;
codex) name="Codex" ;;
grok) name="Grok" ;;
*)
  echo "Usage: omarchy-agent-account-use [claude|codex|grok] [account|next]" >&2
  exit 1
  ;;
esac

result=$(omarchy-agent-account-state use "$1" "${2:-next}") || exit 1

# Commands run outside omarchy-agent follow the switch through the dispatcher,
# which a CLI installed outside Omarchy wouldn't have yet.
omarchy-agent-account-dispatch "$1"

# Who running sessions are on, which differs from the previous choice while an
# earlier switch is still held back.
running=$(jq -r '.running // .previous.label' <<<"$result")
active=$(jq -r '.active.label + (if (.active.plan // "") != "" then " (" + .active.plan + ")" else "" end)' <<<"$result")
label=$(jq -r .active.label <<<"$result")
seated=$(jq -r .seated <<<"$result")
moved=$(jq -r .moved <<<"$result")

if [[ $(jq -r .previous.id <<<"$result") == "$(jq -r .active.id <<<"$result")" && $moved != "true" ]]; then
  echo "New $name sessions already use $active."
elif [[ $seated == "true" ]]; then
  # The login moved into the home every session runs in, so running sessions
  # pick it up on their next message.
  echo "$name sessions now use $active, running ones included."
  omarchy-notification-send -g 󰚩 --image "$(omarchy-agent-logo "$1")" "$(omarchy-i18n agents.sessionsUse "$name" "$label")" "$(omarchy-i18n agents.runningFollow)"
  omarchy-agent-usage-update --limits-only "$1" >/dev/null 2>&1 &
else
  echo "New $name sessions now use $active. Running sessions stay on $running."
  holders=$(jq -r '.holders | map(tostring) | join(" ")' <<<"$result")
  if [[ -n $holders ]]; then
    # Its login can't leave its own home while sessions started there still
    # hold it; once they've quit, the next usage check moves it.
    echo "Sessions started as $label still hold its login (pid $holders). Once they quit, running sessions move to it too."
  fi
  # The headline leaves out the plan, so it stays on one line.
  omarchy-notification-send -g 󰚩 --image "$(omarchy-agent-logo "$1")" "$(omarchy-i18n agents.newSessionsUse "$name" "$label")" "$(omarchy-i18n agents.runningStayOn "$running")"
  # The usage record's own limits describe the active account.
  omarchy-agent-usage-update --limits-only "$1" >/dev/null 2>&1 &
fi
