#!/usr/bin/env sh
#
# notes - note management cli with formatter powered by fzf
# dependencies: fzf | fd | ripgrep
#
# ---
# source: https://github.com/terror/dotfiles

# TODO(2026-02-25): find prettier alt.

EDITOR=${EDITOR:-vi}
NOTE_DIR=${NOTE_DIR:-${HOME}/notes}
NOTE_EXT=${NOTE_EXT:-md}

enter() {
  cd "${NOTE_DIR}" && fd --type=f | query \
    --bind "alt-bspace:execute(trash-put ${NOTE_DIR}/{})+reload(fd --type=f)" \
    --bind "enter:become(${EDITOR} ${NOTE_DIR}/{} &>/dev/tty)" \
    --preview "bat --style=numbers --color=always --line-range :500 ${NOTE_DIR}/{}"
}

# NOTE_FORMAT_COMMAND=${NOTE_FORMAT_COMMAND:-prettier --prose-wrap always --single-quote --write}
# format() {
#   cd "${NOTE_DIR}" && fd --type=f | query \
#     --bind "alt-bs:execute(trash-put ${NOTE_DIR}/{})+reload(fd --type=f)" \
#     --bind "enter:become(${NOTE_FORMAT_COMMAND} ${NOTE_DIR}/{})" \
#     --preview "bat --style=numbers --color=always --line-range :500 ${NOTE_DIR}/{}"
# }

# f | format        Format files with a prose formatter
usage() {
  cat <<EOF
A simple note management CLI powered by fzf, fd, and ripgrep.

Usage: notes [COMMAND]

Available commands:
  d | daily         Open up todays note
  e | enter         Interact with the notes directory
  h | help          Show this message
  j | journal       Open up todays journal
  k | keybindings   View all fzf window keybindings
  s | search        Start a full-text search
EOF
  exit "${1:-0}"
}

daily() {
  note_file="${NOTE_DIR}/$(date +%F).md"
  touch "${note_file}"
  "${EDITOR}" "${note_file}"
}

journal() {
  date=$(date '+%Y-%m-%d')
  path="${NOTE_DIR}/${date}.${NOTE_EXT}"

  if [ ! -f "${path}" ]; then
    echo "## ${date}" >"${path}"
  fi
  "${EDITOR}" "${path}"
}

# echo '<ctrl-f> Format the selected file'
keybindings() {
  echo '<ctrl-\> Toggle the preview window'
  echo '<ctrl-c> Exit'
  echo '<alt-bs> Delete the selected file'
  echo '<ctrl-e> Create a new file whose name is the current query string'
  echo '<ctrl-j> Scroll down the preview window'
  echo '<ctrl-k> Scroll up the preview window'
  echo '<ctrl-r> Reload the result buffer'
  echo '<enter>  Edit the selected file'
}

search() {
  INITIAL_QUERY=""

  RG_PREFIX="rg --column --line-number --no-heading --color=always --smart-case "
  # shellcheck disable=SC2034
  FZF_DEFAULT_COMMAND="${RG_PREFIX} '${INITIAL_QUERY}'"

  cd "${NOTE_DIR}" && query \
    --bind="change:reload:${RG_PREFIX} {q} || true" \
    --bind="alt-bs:execute(trash-put ${NOTE_DIR}/{1})+reload(fd --type=f)" \
    --bind="enter:become(${EDITOR:-vi} ${NOTE_DIR}/{1} &>/dev/tty)" \
    --delimiter=':' \
    --disabled \
    --preview "test -n {1} && bat --style=numbers --color=always --highlight-line {2} --line-range :500 ${NOTE_DIR}/{1} | rg --color=always --passthru --smart-case {q} || true" \
    --query="${INITIAL_QUERY}"
}

# --bind="ctrl-f:execute(${NOTE_FORMAT_COMMAND} ${NOTE_DIR}/{})+reload(fd --type=f)"
query() {
  fzf \
    --ansi \
    --bind="ctrl-e:become(${EDITOR:-vi} ${NOTE_DIR}/{q}.${NOTE_EXT} &>/dev/tty)" \
    --bind="ctrl-r:reload(fd --type=f)" \
    --bind='ctrl-\:toggle-preview' \
    --bind='ctrl-c:abort' \
    --bind='ctrl-k:preview-up,ctrl-j:preview-down' \
    --border \
    --height=90% \
    --min-height=20 \
    --preview-window=75% \
    "$@"
}

# f | format) format
main() {
  case "$1" in
    "") enter ;;
    d | daily) daily ;;
    e | enter) enter ;;
    h | help) usage ;;
    j | journal) journal ;;
    k | keybindings) keybindings ;;
    s | search) search ;;
    -h | --help) usage ;;
    *)
      echo 'error: Invalid command' >&2
      return 1
      ;;
  esac
}
main "$@"
