#!/bin/bash

# Define the exact list of packages that failed and need a full re-upload
PACKAGES=(
  "imillegal1" "imillegal2" "imillegal3" "imillegal4" "imillegal5"
  "ishowfeet1" "ishowfeet2" "ishowfeet3" "ishowfeet4" "ishowfeet5"
  "ishowfeet6" "ishowfeet7" "ishowfeet8" "ishowfeet9" "ishowfeet10"
  "ishowfeet11" "ishowfeet12" "ishowfeet13" "ishowfeet14" "ishowfeet15"
  "ishowfeet16" "ishowfeet17" "ishowfeet18" "ishowfeet19" "ishowfeet20"
  "nottuff1" "nottuff2" "nottuff3" "nottuff4" "nottuff5"
  "nottuff6" "nottuff7" "nottuff8" "nottuff9" "nottuff10"
  "nottuff11" "nottuff12" "nottuff13" "nottuff14" "nottuff15"
  "nottuff16" "nottuff17" "nottuff18" "nottuff19" "nottuff20"
  "nottuff21" "nottuff22" "nottuff23" "nottuff24" "nottuff25"
  "nottuff26" "nottuff27" "nottuff28" "nottuff29" "nottuff30"
  "abuden1" "abuden2" "abuden3" "abuden4" "abuden5"
  "abuden21" "abuden22" "abuden23" "abuden24" "abuden25"
  "abuden26" "abuden27" "abuden28" "abuden29" "abuden230"
  "abuden210" "abuden211" "abuden212" "abuden213" "abuden214"
  "abuden215" "abuden216" "abuden217" "abuden218" "abuden219"
  "abuden220" "abuden221" "abuden222" "abuden223" "abuden224"
  "abuden225" "abuden226" "abuden227" "abuden228" "abuden229"
  "ratelimitsucks" "ratelimitsucks2" "ratelimitsucks5" "ratelimitsucks6" "ratelimitsucks10"
)

TOTAL=${#PACKAGES[@]}
PARALLEL=3 # Kept slightly lower to ensure file copies complete reliably

# Read original package.json contents
PKG_CONTENT=$(cat package.json)

draw_progress() {
  local current=$1
  local total=$2
  local percent=$(( current * 100 / total ))
  local bar_size=30
  local filled=$(( current * bar_size / total ))
  local empty=$(( bar_size - filled ))

  local bar=$(printf "%${filled}s" | tr ' ' '#')
  local spaces=$(printf "%${empty}s" | tr ' ' '-')

  printf "\rProgress: [%s%s] %d%% (%d/%d)" "$bar" "$spaces" "$percent" "$current" "$total"
}

publish_one() {
  local NAME=$1
  local TMP_DIR
  
  TMP_DIR=$(mktemp -d 2>/dev/null || mktemp -d -t 'tmp_npm')

  (
    # Copy all files from the current folder, EXCEPT node_modules and .git
    # This ensures logo.svg and your code get copied over
    rsync -a --exclude='node_modules' --exclude='.git' ./ "$TMP_DIR/" 2>/dev/null || \
    cp -R . "$TMP_DIR" 2>/dev/null || true

    cd "$TMP_DIR" || exit 1
    
    # Overwrite the package.json and inject the fresh name
    echo "$PKG_CONTENT" > package.json
    node -e "
      const fs = require('fs');
      const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
      pkg.name = process.argv[1];
      fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2));
    " "$NAME"

    # Force publish over the existing version
    npm publish --silent > /dev/null 2>&1
  )

  rm -rf "$TMP_DIR"
  echo "$NAME"
}

echo "Starting full re-deployment of failed packages..."
echo "================================================="
draw_progress 0 "$TOTAL"

completed=0
while read -r FINISHED_PKG
do
  completed=$((completed + 1))
  
  # Live UI output format
  printf "\r\033[K"
  echo " ✓ Fixed & Re-uploaded: $FINISHED_PKG"
  draw_progress "$completed" "$TOTAL"

done < <(
  for TARGET_PKG in "${PACKAGES[@]}"
  do
    while [ "$(jobs -p | wc -l)" -ge "$PARALLEL" ]
    do
      sleep 0.05
    done

    publish_one "$TARGET_PKG" &
  done
  
  wait
)

printf "\r\033[K"
echo -e "\n================================================="
echo "ALL FIXED PACKAGES UPLOADED SUCCESSFULLY"