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

# Script Name: timer
# Description: Countdown timer with audio and notification
# Source: https://evanhahn.com/scripts-i-wrote-that-i-use-all-the-time/
# Usage: timer 5m
#        timer 30s
#        timer 1h

if [[ $# -ne 1 ]]; then
  echo "Usage: timer <duration>" >&2
  echo "Examples: timer 5m, timer 30s, timer 1h" >&2
  exit 1
fi

duration="$1"

# Show countdown timer with gum if available
if command -v gum &>/dev/null; then
  echo "⏱️  Timer started for $duration"
  sleep "$duration"
else
  echo "⏱️  Timer started for $duration (sleeping...)"
  sleep "$duration"
fi

# Play notification sound if available
if command -v paplay &>/dev/null && [[ -f /usr/share/sounds/freedesktop/stereo/complete.oga ]]; then
  paplay /usr/share/sounds/freedesktop/stereo/complete.oga &
elif command -v aplay &>/dev/null && [[ -f /usr/share/sounds/freedesktop/stereo/complete.wav ]]; then
  aplay /usr/share/sounds/freedesktop/stereo/complete.wav &
fi

# Send desktop notification
if command -v notify-send &>/dev/null; then
  notify-send "⏰ Timer Complete" "Timer finished: $duration"
fi

echo "✅ Timer complete: $duration"
