#!/usr/bin/env bash

#MISE description="Show outdated dependencies."

# We purposefully do not set `set -e` here, as we want to see all outdated dependencies at once.

source "$(git rev-parse --show-toplevel)/scripts/_helpers.sh"

ensure_working_directory!

# Initialize arrays to track commands
declare -a command_names
declare -a command_results

# Function to run and track commands
run_and_track() {
  local name="$1"
  shift
  "$@"
  local result=$?
  command_names+=("$name")
  command_results+=("$result")
  return $result
}

debug_msg "Checking outdated NPM dependencies..."
run_and_track "NPM dependencies" npm outdated --prefix assets

debug_msg "Checking outdated tooling (tailwind, esbuild, sqlean)..."
run_and_track "Tailwind" mix tailwind.check_version
run_and_track "ESBuild" mix esbuild.check_version
run_and_track "Sqlean" mix sqlean.check_version

debug_msg "Checking outdated mix dependencies"
run_and_track "Mix dependencies" mix hex.outdated --all

# Print summary table
echo ""
echo "============================================="
echo "Summary"
echo "============================================="
printf "%-30s | %s\n" "Dependency" "Status"
echo "---------------------------------------------"

red=$(tput setaf 1)
green=$(tput setaf 2)
normal=$(tput sgr0)

for i in "${!command_names[@]}"; do
  name="${command_names[$i]}"
  result="${command_results[$i]}"

  if [ "$result" -eq 0 ]; then
    printf "%-30s | %s\n" "$name" "${green}✓ Up to date${normal}"
  else
    printf "%-30s | %s\n" "$name" "${red}✗ Run update${normal}"
  fi
done

echo "============================================="
