#!/usr/bin/env bash

# change directory to a ~/readme project. A script can't change your working
# directory, so you either need to eval this (. ,jf) or add an alias:
# `alias jf='. ~/.local/bin/,jf'`
ROOT=~/jellyfish

# select the best available command to display markdown. I tried to use glow
# and richify.py, but neither wanted to work inside fzf - richify just
# displayed some uv message and glow didn't show colors and doesn't have an
# equivalent to bat's --color=always
if command -v bat &> /dev/null; then
  display="bat --color=always --style=plain --language=markdown"
else
  display="cat"
fi

# ls is way faster than I was able to make find run when sorting by mtime, so
# we'll use it despite the downsides
#
# use /bin/ls to avoid any aliases of ls, since this is sourced so inherits
# aliases
# -t: sort by descending mtime
# -d: directories are listed not expanded
# -F: display a / after directory names
# -G: colorized output
#
# Build the list of directories, expanding jellyfish repo to show worktrees
# shellcheck disable=SC2012
dir_list=$(cd ${ROOT} && {
    # Get all directories except jellyfish
    /bin/ls -tdFG -- */ 2>/dev/null | sed 's#/$##' | grep -v '^jellyfish$'
    # Add jellyfish itself (will go to develop branch)
    echo "jellyfish"
    # Add jellyfish worktrees as separate options (excluding develop)
    if [[ -d jellyfish ]]; then
        cd jellyfish && /bin/ls -tdFG -- */ 2>/dev/null | sed 's#/$##' | grep -v '^develop$' | sed 's#^#jellyfish/#'
    fi
})

dir=$(echo "${dir_list}" | 
    fzf --ansi \
        --height 40% \
        --layout reverse \
        --border bold \
        --preview "
            # Extract just the directory name (handle both regular and jellyfish/* paths)
            preview_dir=\$(basename {})
            
            # Check if directory starts with OJ- and show Jira issue
            if [[ \${preview_dir} == OJ-* ]]; then
                # Extract just the OJ-XXXXX part (everything before the first dash after the number)
                jira_issue=\$(echo \"\${preview_dir}\" | grep -oE '^OJ-[0-9]+')
                jira issue view \"\${jira_issue}\"
            elif [[ -f ${ROOT}/{}README.md ]]; then 
                ${display} ${ROOT}/{}README.md
            elif [[ -f ${ROOT}/{}main/README.md ]]; then 
                ${display} ${ROOT}/{}main/README.md
            elif [[ -f ${ROOT}/{}next/README.md ]]; then 
                ${display} ${ROOT}/{}next/README.md
            elif [[ -f ${ROOT}/{}master/README.md ]]; then 
                ${display} ${ROOT}/{}master/README.md
            elif [[ -f ${ROOT}/{}develop/README.md ]]; then 
                ${display} ${ROOT}/{}develop/README.md
            else 
                ls -la ${ROOT}/{}
            fi" \
        --preview-window "right,60%,border,+3/3" \
        --query "${1}"
      )
        # above is a complicated preview that finds the README if available and
        # displays it; for a simpler preview, use:
        # --preview "ls -la ~/{}" \
echo "${dir}"

if [[ -n "${dir}" ]]; then
  # touch ROOT to update its timestamp so it will sort first next time
  touch ${ROOT}
  
  # Check if this is a jellyfish worktree path
  if [[ "${dir}" == jellyfish/* ]]; then
    # Already includes the worktree path, just cd to it
    cd ${ROOT}/"${dir}" || return
  else
    # Regular repository, navigate and check for branch directories
    cd ${ROOT}/"${dir}" || return
    if [[ -d "next" ]]; then
      cd next || return
    elif [[ -d "main" ]]; then
      cd main || return
    elif [[ -d "develop" ]]; then
      cd develop || return
    fi
  fi
fi





