# zsh being installed via homebrew leads to some directories used for autocomplete
# being considered insecure by zsh, this disables that check since we trust homebrew
export ZSH_DISABLE_COMPFIX=true

export DOTFILES_DIR=$HOME/dotfiles

export TASKS_DIR=$HOME/projects/.tasks

export EDITOR=vim
export BAT_THEME="ansi"
export DELTA_FEATURES="+side-by-side +line-numbers"

alias dots="cd ${DOTFILES_DIR}"
alias c="clear"
alias tma="tmux a"
alias tml="tmux list-sessions"
alias dc="docker-compose"
alias tf="terraform"
alias k="kubectl"
alias eks="eksctl"
alias da="direnv allow"

alias la="ls -laGh"

alias gl="git pull"
alias gp="git push"
alias gpf="git push --force-with-lease"
alias gst="git status"
alias gd="git diff"
alias gdc="git diff --cached"
alias gdst="git diff --stat"
alias gb="git branch"
alias gfp="git fetch --prune"
alias gsw="git switch"
alias gco="git checkout"

alias wtp="git worktree prune"

alias lg="lazygit"
alias ld="lazydocker"

alias ccb="git branch --no-color --show-current | tr -d '\n' | pbcopy"
alias ytop="ytop -c default-dark"

alias python=python3
alias pip=pip3

alias tm="${HOME}/dotfiles/scripts/tmopen.sh"
alias aic="aicommits --type conventional"

# find worktrunk worktree directories
function wtl() {
  fd --type d '\.wt\.' "${1:-.}"
}

# Task workspace management
alias tl="ls -laG $TASKS_DIR"
alias cdt="cd ${TASKS_DIR}"

function tn() {
  task_desc="${1-$(date +%s)}"
  task_dir="$TASKS_DIR/${task_desc}"
  mkdir -p "$task_dir"

  cat >"$task_dir/TASK.md" <<'EOF'
## Background

## Goals

# Non-Goals

## Questions

## Progress
EOF
  cd "$task_dir" || {
    echo "failed to cd: ${task_dir}"
    return 1
  }
}

function tcd() {
  local selected
  selected=$(find "$TASKS_DIR" -maxdepth 1 -mindepth 1 -type d 2>/dev/null |
    sed 's|^'"$TASKS_DIR"'/||' |
    fzf --height 40% --reverse --preview "ls -la $TASKS_DIR/{}" \
      --preview-window=right:50%:wrap)
  if [[ -n "$selected" ]]; then
    cd "$TASKS_DIR/$selected" || return 1
  fi
}

function trm() {
  local selected
  selected=$(find "$TASKS_DIR" -maxdepth 1 -mindepth 1 -type d 2>/dev/null |
    sed 's|^'"$TASKS_DIR"'/||' |
    fzf --height 40% --reverse --preview "ls -la $TASKS_DIR/{}" \
      --preview-window=right:50%:wrap)
  [[ -z "$selected" ]] && return 1

  local task_path="$TASKS_DIR/$selected"

  trash "$task_path"
  echo "Removed task: $selected"
}

# Gets today's date in YYYY-MM-DD format
# Open neovim on ~/obsidian/<vault>/Daily/${date}.md
# vault is `brain` if hostname is `vita`, `zapier` if hostname is `asteroid`
function dn() {
  local vault
  case "$(hostname -s)" in
  vita) vault="brain" ;;
  asteroid) vault="zapier" ;;
  *)
    echo "dn: unknown host $(hostname -s)"
    return 1
    ;;
  esac

  local date_str file
  date_str=$(date +%Y-%m-%d)
  file="$HOME/obsidian/$vault/Daily/${date_str}.md"

  mkdir -p "$(dirname "$file")"
  nvim -c Goyo "$file"
}

# Accepts a target markdown file and opens it with firefox.
#
# Expects the [markdown viewer](https://github.com/simov/markdown-viewer)
# FF extension to be installed.
function mdp {
  local file="${1}"

  # If it isn't already an absolute path, make it one
  if [[ ! "$file" == "/"* ]]; then
    file="${PWD}/${file}"
  fi

  # If it isn't a markdown file, bail
  if [[ ! "$file" == *".md" ]]; then
    echo "Must provide a markdown file"
    exit 1
  fi

  open -a Helium.app "${file}"
}

function rand {
  count=$1
  if [ -z "$count" ]; then
    count=8
  fi
  date +%s | shasum | head -c$1
}

function gittmp {
  repo_uri=$1
  if [ -z "$1" ]; then
    echo "must provide a repo to clone"
    return 1
  fi

  newdir=$(mktemp -d)
  git clone $1 $newdir

  session_name="gittmp-$(rand 4)"
  tmux new -A -s $session_name -c $newdir -d
  tmux switch -t $session_name
}

function gbrowse() {
  branch=$(git branch --show-current)
  if [ -z "$branch" ]; then
    branch=$(git rev-parse HEAD)
  fi

  origin=$(git remote get-url origin)
  if [[ "$origin" =~ .*gitlab\.com.* ]]; then
    # Assume it's on gitlab
    # gitlab's cli doesn't have a `browse` command, so do this instead
    baseurl=$origin
    if [[ "$origin" =~ ^git@gitlab.* ]]; then
      baseurl=$(echo $origin | sed 's/git@/https:\/\//' | sed 's/\.com:/\.com\//' | sed 's/\.git$//')
    fi
    open "$baseurl/-/blob/$branch/$1"
  else
    # Assume it's on github
    gh browse --branch="$branch" $1
  fi
}

# Load zapier-specific config if on my work machine
ZAPIER_ZSHENV="${HOME}/projects/zapier/utils/zshenv"
if [[ $(hostname) == "asteroid" && -f "${ZAPIER_ZSHENV}" ]]; then
  source "${ZAPIER_ZSHENV}"
fi

export HOMEBREW_BUNDLE_FILE="$HOME/dotfiles/Brewfile"

eval "$(zoxide init zsh)"
