#!/usr/bin/env bash
#
# usage: ./run command [argument ...]
#
# Commands for kistitch development.
#

set -o nounset
set -o pipefail
set -o errexit

# Change the current directory to the project root.
PROJECT_ROOT=${0%/*}
if [[ $0 != $PROJECT_ROOT && $PROJECT_ROOT != "" ]]; then
  cd "$PROJECT_ROOT"
fi
readonly PROJECT_ROOT=$(pwd)

################################################################################
# Development Commands

function sync {
  #@ Create or update the virtualenv with the dev dependencies and the gui extra
  #@ Category: Development
  uv sync --extra gui "$@"
}

function test {
  #@ Run the tests (extra arguments go to pytest)
  #@ Category: Development
  sync --quiet
  .venv/bin/python -m pytest "$@"
}

function lint {
  #@ Check linting and formatting with ruff
  #@ Category: Development
  .venv/bin/ruff check kistitch tests
  .venv/bin/ruff format --check kistitch tests
}

function format {
  #@ Format the code and apply ruff's safe lint fixes
  #@ Category: Development
  .venv/bin/ruff check --fix kistitch tests
  .venv/bin/ruff format kistitch tests
}

function check {
  #@ Run lint and the tests, as CI does (before committing)
  #@ Category: Development
  sync --quiet
  lint
  test
}

################################################################################
# Version Commands

function bumpversion {
  #@ Bump the version in VERSION and pyproject.toml (patch|minor|major)
  #@ Category: Version
  local BUMP_TYPE="${1:-}"

  if [[ ! "$BUMP_TYPE" =~ ^(patch|minor|major)$ ]]; then
    echo "Usage: ./run bumpversion [patch|minor|major]"
    exit 1
  fi

  if ! command -v bumpversion &> /dev/null; then
    echo "Error: bumpversion not installed"
    echo "Install with: uv tool install bump2version"
    exit 1
  fi

  command bumpversion --no-commit "$BUMP_TYPE"
  # The lockfile records the project's own version.
  uv lock
  echo "Version is now $(cat VERSION)"
}

################################################################################
# Help System

function help {
  #@ Show this help message
  #@ Category: Core
  echo "kistitch - Development Commands"
  echo "==============================="
  echo ""
  echo "Available commands:"
  echo ""

  # Extract function definitions and their help comments
  awk '/^function / {
    fname = $2
    sub(/[({].*/, "", fname)
    getline
    if ($0 ~ /#@/) {
      desc = $0
      sub(/.*#@ /, "", desc)
      sub(/ *$/, "", desc)
      getline
      if ($0 ~ /#@ Category:/) {
        cat = $0
        sub(/.*Category: /, "", cat)
        sub(/ *$/, "", cat)
        if (!seen[cat]++) categories[++cat_count] = cat
        commands[cat, ++cmd_count[cat]] = fname
        descriptions[cat, cmd_count[cat]] = desc
      }
    }
  }
  END {
    for (i = 1; i <= cat_count; i++) {
      cat = categories[i]
      print "\n" cat ":"
      for (j = 1; j <= cmd_count[cat]; j++) {
        printf "  %-25s %s\n", commands[cat, j], descriptions[cat, j]
      }
    }
  }' "$0"

  echo ""
  echo "Usage: ./run <command> [arguments...]"
  echo ""
}

################################################################################
# Commands end.

TIMEFORMAT=$'\nTask completed in %3lR'
time "${@:-help}"
