#!/usr/bin/env bash
# txnotify — send an Android notification.
# Usage: txnotify [-t TITLE] [-c CONTENT] [-i ID] [options]
#   Thin but strict wrapper around termux-notification: validates the
#   priority and id, requires a title or content, and --dry-run shows
#   the exact command instead of sending it.
set -u
prog=txnotify

usage() {
  cat <<'USAGE'
Usage: txnotify [-t TITLE] [-c CONTENT] [-i ID] [options]
  Send an Android notification via termux-notification.
  -t, --title TITLE        notification title
  -c, --content CONTENT    notification body (one of -t/-c is required)
  -i, --id ID              notification id (number, default 1)
  --group GROUP            notification group
  --priority P             low|default|high|max|min (default: default)
  --ongoing                ongoing notification (not swipe-dismissable)
  --alert-once             alert only the first time
  --sound                  play the default notification sound
  --vibrate PATTERN        vibrate pattern, e.g. 200,300,200
  --button1 LABEL --action1 CMD
  --button2 LABEL --action2 CMD
  --button3 LABEL --action3 CMD
  --led-color RRGGBB       LED colour, e.g. ff0000
  --icon NAME              icon name
  --image-path PATH        large image shown in the notification
  --dry-run                print the termux-notification command, do not send
  --selftest               run built-in checks (dry-run + guard only)
  --help                   show this help
  Requires termux-notification (Termux:API). No Linux equivalent exists,
  so on plain Linux this prints a clear message and exits non-zero.
USAGE
}

api_missing() {
  printf '%s: termux-api not available (termux-notification not found)\n' "$prog" >&2
  printf '%s: run this on Android with Termux:API installed. There is no Linux equivalent for Android notifications.\n' "$prog" >&2
  return 1
}

selftest() {
  local tmp fails=0 out rc
  tmp=$(mktemp -d); trap "rm -rf '$tmp'" EXIT
  local self="${BASH_SOURCE[0]}"
  out=$("$self" --dry-run -t "Hi" -c "body" -i 7 --priority high --ongoing 2>&1); rc=$?
  if (( rc == 0 )) && [[ $out == *"termux-notification"* && $out == *"-t Hi"* && $out == *"-i 7"* && $out == *"--priority high"* ]]; then echo "PASS: --dry-run builds the command"; else echo "FAIL: dry-run rc=$rc out=$out"; fails=1; fi
  out=$("$self" --dry-run -t "x" --priority bogus 2>&1); rc=$?
  if (( rc != 0 )) && [[ $out == *"priority"* ]]; then echo "PASS: bad priority rejected"; else echo "FAIL: bad priority rc=$rc out=$out"; fails=1; fi
  out=$("$self" --dry-run -i abc -c "x" 2>&1); rc=$?
  if (( rc != 0 )); then echo "PASS: non-numeric id rejected"; else echo "FAIL: bad id should fail"; fails=1; fi
  out=$("$self" --dry-run 2>&1); rc=$?
  if (( rc != 0 )); then echo "PASS: missing title/content rejected"; else echo "FAIL: empty notify should fail"; fails=1; fi
  out=$(PATH="/usr/bin:/bin" "$self" -t "Hi" -c "body" 2>&1); rc=$?
  if (( rc != 0 )) && [[ $out == *"termux-api not available"* ]]; then echo "PASS: graceful degradation message"; else echo "FAIL: guard rc=$rc out=$out"; fails=1; fi
  out=$("$self" --help 2>&1); rc=$?
  if (( rc == 0 )) && [[ $out == *"Usage:"* ]]; then echo "PASS: --help"; else echo "FAIL: --help rc=$rc"; fails=1; fi
  (( fails == 0 )) && echo "txnotify selftest: all PASS" || echo "txnotify selftest: $fails FAILURES"
  return $fails
}

title="" content="" id=1 group="" priority=default ongoing=0 alert_once=0 sound=0
vibrate="" led="" icon="" image="" dryrun=0
b1="" a1="" b2="" a2="" b3="" a3=""
while (( $# )); do
  case $1 in
    --help|-h) usage; exit 0;;
    --selftest) selftest; exit $?;;
    --dry-run) dryrun=1;;
    -t|--title) shift; title=${1:?--title needs a value};;
    -c|--content) shift; content=${1:?--content needs a value};;
    -i|--id) shift; id=${1:?--id needs a value};;
    --group) shift; group=${1:?--group needs a value};;
    --priority) shift; priority=${1:?--priority needs a value};;
    --ongoing) ongoing=1;;
    --alert-once) alert_once=1;;
    --sound) sound=1;;
    --vibrate) shift; vibrate=${1:?--vibrate needs a value};;
    --led-color) shift; led=${1:?--led-color needs a value};;
    --icon) shift; icon=${1:?--icon needs a value};;
    --image-path) shift; image=${1:?--image-path needs a value};;
    --button1) shift; b1=${1:?--button1 needs a label};;
    --action1) shift; a1=${1:?--action1 needs a command};;
    --button2) shift; b2=${1:?--button2 needs a label};;
    --action2) shift; a2=${1:?--action2 needs a command};;
    --button3) shift; b3=${1:?--button3 needs a label};;
    --action3) shift; a3=${1:?--action3 needs a command};;
    *) echo "$prog: unknown option: $1" >&2; usage >&2; exit 1;;
  esac
  shift
done
[[ -n $title || -n $content ]] || { echo "$prog: need a title (-t) or content (-c)" >&2; usage >&2; exit 1; }
[[ $id =~ ^[0-9]+$ ]] || { echo "$prog: --id must be a number, got '$id'" >&2; exit 1; }
case $priority in low|default|high|max|min) ;; *) echo "$prog: --priority must be low|default|high|max|min, got '$priority'" >&2; exit 1;; esac
[[ -z $led || $led =~ ^[0-9a-fA-F]{6}$ ]] || { echo "$prog: --led-color must be RRGGBB hex, got '$led'" >&2; exit 1; }
for pair in "1:$b1:$a1" "2:$b2:$a2" "3:$b3:$a3"; do
  n=${pair%%:*}; rest=${pair#*:}; lbl=${rest%%:*}; act=${rest#*:}
  if [[ -n $lbl && -z $act ]] || [[ -z $lbl && -n $act ]]; then echo "$prog: --button$n and --action$n must be given together" >&2; exit 1; fi
done

args=()
[[ -n $title ]] && args+=(-t "$title")
[[ -n $content ]] && args+=(-c "$content")
args+=(-i "$id")
[[ -n $group ]] && args+=(--group "$group")
args+=(--priority "$priority")
(( ongoing )) && args+=(--ongoing)
(( alert_once )) && args+=(--alert-once)
(( sound )) && args+=(--sound)
[[ -n $vibrate ]] && args+=(--vibrate "$vibrate")
[[ -n $led ]] && args+=(--led-color "$led")
[[ -n $icon ]] && args+=(--icon "$icon")
[[ -n $image ]] && args+=(--image-path "$image")
[[ -n $b1 ]] && args+=(--button1 "$b1" --action1 "$a1")
[[ -n $b2 ]] && args+=(--button2 "$b2" --action2 "$a2")
[[ -n $b3 ]] && args+=(--button3 "$b3" --action3 "$a3")

if (( dryrun )); then
  printf 'termux-notification'
  printf ' %q' "${args[@]}"
  printf '\n'
  exit 0
fi
command -v termux-notification >/dev/null 2>&1 || { api_missing; exit 1; }
termux-notification "${args[@]}"
