#!/bin/sh

if [ -z "$JAVA_HOME" ] && ! command -v java >/dev/null 2>&1; then
    for path in "$HOME/.sdkman/candidates/java/current/bin" "/usr/lib/jvm/default-java/bin" "/usr/local/bin" "/usr/bin"; do
        if [ -x "$path/java" ]; then
            export PATH="$path:$PATH"
            break
        fi
    done
fi

if ! command -v java >/dev/null 2>&1; then
    echo "Error: java is not found in PATH and JAVA_HOME is not set."
    echo "Please ensure java is installed and accessible."
    exit 1
fi


java_staged_files=$(git --no-pager diff --cached --name-only | grep '\.java')
if [ -n "$java_staged_files" ]; then
  echo "Staged Java files: $java_staged_files"
  echo "Running spotlessApply on Java files..."

  for file in $java_staged_files; do
    ./gradlew --no-build-cache spotlessApply -PtargetFile="$file" --continue

  done

  git add $java_staged_files

  echo "spotlessApply completed."
else
  echo "No Java files modified. Skipping spotlessApply."
fi



ui_staged_files=$(git --no-pager diff --cached --name-only | grep -E '(^|/)(ui|ui-ee)(/|$)')

if [ -n "$ui_staged_files" ]; then
  echo "Staged UI files: $ui_staged_files"
  for dir in ui ui-ee; do
    if [ -d "$dir" ] && echo "$ui_staged_files" | grep -qE "^$dir/.*\.(ts|tsx|vue)$"; then
      echo "Updating the explicit any baseline in $dir..."
      (cd "$dir" && npm run check:ts-any -- --write) || exit 1
      git add "$dir/scripts/explicit-any/baseline.json"
    fi
  done
  if [ -d "ui" ] && echo "$ui_staged_files" | grep -qE "^ui/(package\.json|(src|packages/(design-system|topology)/src)/.*\.(ts|tsx|js|mjs|jsx|vue|css|scss))$"; then
    echo "Checking the dependencies / devDependencies split in ui..."
    (cd ui && npm run check:dep-split -- --write) || exit 1
    git add ui/package.json ui/package-lock.json
  fi
  if [ -d "ui" ]; then
      echo "Running lint-staged in ui directory..."
      cd ui && npx lint-staged
  fi

  if [ -d "ui-ee" ]; then
      echo "Running lint-staged in ui directory..."
      cd ui-ee && npx lint-staged
  fi
fi