#!/bin/bash
# Example: Project-specific shell functions built on wa
#
# These show how to use `wa get wt --worktree` to create quick navigation
# shortcuts for any project. Copy and customize for your own projects.
#
# The pattern:
#   cd $(wa get wt --worktree)/path/to/subdir || return
#
# This works because `wa get wt --worktree` returns the root path of your
# currently active worktree. Combined with cdwa (which sets the active
# worktree by alias), you get instant navigation anywhere in your project.

# --- Navigation shortcuts ---

# Jump to common directories
projects() {
  cd ~/Desktop/projects || return
}

dev() {
  cd ~/dev || return
}

# --- Example: Elixir/Phoenix project ---

# Jump to the web app within the current worktree
web() {
  cd $(wa get wt --worktree)/apps/casemgr_web || return
}

# --- Example: React/Node project ---

# Jump to the portal app
portal() {
  cd $(wa get wt --worktree)/apps/portal || return
}

# Jump to a feature directory
features() {
  cd $(wa get wt --worktree)/apps/portal/src/features || return
}

# Jump to a specific feature
ftime() {
  cd $(wa get wt --worktree)/apps/portal/src/features/time || return
}

ftimesheets() {
  cd $(wa get wt --worktree)/apps/portal/src/features/time/timesheets || return
}

fhooks() {
  cd $(wa get wt --worktree)/apps/portal/src/features/time/timesheets/hooks || return
}

# Jump to build output
static() {
  cd $(wa get wt --worktree)/apps/portal/build/static || return
}

# Copy build artifacts to a deployment target
copystatic() {
  cp -R $(wa get wt --worktree)/apps/portal/build/* '/mnt/c/dev/build/unanet'$1'/web/ext/react'
}

# Nuke node_modules and rebuild
nuke() {
  yarn nuke; git checkout yarn.lock; yarn install; yarn build; yarn start:dev
}

startdev() {
  yarn start:dev
}

# --- Example: Docker ---

up() {
  docker-compose up services
}

down() {
  docker-compose down
}

# --- Example: Ticket integration ---
# Use the worktree name as the branch/ticket identifier

ut() {
  unanet-ticket $1 $(basename $(wa get wt --worktree)) || return
}

utr() {
  unanet-ticket $1 $(basename $(wa get wt --worktree)) -r || return
}

# --- Example: Multi-environment navigation (WSL) ---

bases() {
  cd '/mnt/c/dev/apps/tomcat9/bases/unanet'$1 || return
}

logs() {
  cd '/mnt/c/dev/apps/tomcat9/bases/unanet'$1/logs || return
}

build() {
  cd '/mnt/c/dev/build' || return
}

apps() {
  cd '/mnt/c/dev/apps' || return
}

plat() {
  cd '/mnt/c/dev/platform' || return
}

# --- Example: Interactive release management ---

MONTH_NUMBER=01
set_month() {
  while true; do
    read -p "Enter the release month number? (1-12) " myInput
    case $myInput in
    1 | 01) MONTH_NUMBER=01; break ;;
    2 | 02) MONTH_NUMBER=02; break ;;
    3 | 03) MONTH_NUMBER=03; break ;;
    4 | 04) MONTH_NUMBER=04; break ;;
    5 | 05) MONTH_NUMBER=05; break ;;
    6 | 06) MONTH_NUMBER=06; break ;;
    7 | 07) MONTH_NUMBER=07; break ;;
    8 | 08) MONTH_NUMBER=08; break ;;
    9 | 09) MONTH_NUMBER=09; break ;;
    10) MONTH_NUMBER=10; break ;;
    11) MONTH_NUMBER=11; break ;;
    12) MONTH_NUMBER=12; break ;;
    q | Q | exit | EXIT) exit ;;
    esac
  done
}

uplat() {
  set_month
  cd '/mnt/c/dev/platform/unanet-platform-'$MONTH_NUMBER || return
}

nplat() {
  set_month
  cd '/mnt/c/dev/platform/'$MONTH_NUMBER || return
}

copycore() {
  local MYDEV=/mnt/c/dev
  set_month
  read -p "Enter the release number version to copy?   unanet" myInput
  RELEASE_NUMBER=$myInput
  echo "Source"
  ls -al $MYDEV/build/unanet$RELEASE_NUMBER/unanet/unanet-core-*.jar
  cp $MYDEV/build/unanet$RELEASE_NUMBER/unanet/unanet-core-*.jar $MYDEV/platform/$MONTH_NUMBER/platform-config/local-libs
  echo "Destination"
  ls -al $MYDEV/platform/$MONTH_NUMBER/platform-config/local-libs/unanet-core-*.jar
}

copywar() {
  local MYDEV=/mnt/c/dev
  set_month
  cp $MYDEV/platform/unanet-platform-$MONTH_NUMBER/build/libs/*.war $MYDEV/platform/$MONTH_NUMBER
}

# --- Miscellaneous ---

fix_hh() {
  sudo sysctl -w dev.tty.legacy_tiocsti=1
}
