#!/usr/bin/env bash
#
# Audit any local copy of this project for iCloud-induced damage.
#
# Usage:  ./check-icloud-damage.sh [project-root]
#         (defaults to the script's own directory)
#
# Outputs:
#   - count of macOS Finder "* 2" ghost copies (in source vs in node_modules)
#   - count of .icloud placeholder stubs (files iCloud thinks are remote-only)
#   - count of 0-byte JS files (sync interrupted mid-write)
#   - git integrity check
#   - npm tree health per workspace
set -e
ROOT="${1:-$(cd "$(dirname "$0")" && pwd)}"
cd "$ROOT"

echo "Auditing: $ROOT"
echo ""

echo "=== iCloud ghost copies (* 2 files / folders) ==="
SRC_GHOSTS=$(find . -path './node_modules' -prune -o \
  -path '*/node_modules' -prune -o \
  -path './.git' -prune -o \
  \( -name '* 2' -o -name '* 2.*' \) -print 2>/dev/null | wc -l | tr -d ' ')
echo "  in source code:    $SRC_GHOSTS"

NM_GHOSTS=$(find . -path './.git' -prune -o \
  \( -name '* 2' -o -name '* 2.*' \) -print 2>/dev/null | wc -l | tr -d ' ')
NM_ONLY=$((NM_GHOSTS - SRC_GHOSTS))
echo "  in node_modules:   $NM_ONLY"

echo ""
echo "=== iCloud placeholders (file marked 'in cloud only') ==="
ICLOUD_STUBS=$(find . -name '*.icloud' 2>/dev/null | wc -l | tr -d ' ')
echo "  .icloud stubs:     $ICLOUD_STUBS"

echo ""
echo "=== Zero-byte JS / TS files (sync interrupted writes) ==="
# Filter false positives:
#   - .history/ — devtool artifacts shipped with some packages
#   - client-only / server-only — real 0-byte sentinel packages
#   - .d.ts — sometimes intentionally empty type stubs
EMPTY_JS=$(find . -path './.git' -prune -o \
  -path '*/.history' -prune -o \
  -path '*/client-only/*' -prune -o \
  -path '*/server-only/*' -prune -o \
  \( -name '*.js' -o -name '*.cjs' -o -name '*.mjs' \) \
  ! -name '*.d.*' \
  -size 0 -print 2>/dev/null | wc -l | tr -d ' ')
echo "  0-byte JS:         $EMPTY_JS"

if [ -d .git ]; then
  echo ""
  echo "=== Git integrity ==="
  GIT_FSCK=$(git fsck --full 2>&1 | grep -v '^Checking\|^$\|^dangling' | wc -l | tr -d ' ')
  echo "  git fsck issues:   $GIT_FSCK"
  echo "  files modified:    $(git status --porcelain 2>/dev/null | wc -l | tr -d ' ')"
fi

echo ""
echo "=== npm tree health ==="
# Real damage means the dist file the package.json points to is missing.
# We probe a handful of critical packages and check their main entry exists.
for d in backend web; do
  if [ -d "$d/node_modules" ]; then
    BROKEN=0
    for pkg in $(ls "$d/node_modules" 2>/dev/null | head -100); do
      [[ "$pkg" == .* || "$pkg" == @* ]] && continue
      pj="$d/node_modules/$pkg/package.json"
      [ ! -f "$pj" ] && continue
      MAIN=$(node -e "try{const p=require('$PWD/$pj');console.log(p.main||p.module||'index.js')}catch{}" 2>/dev/null)
      [ -z "$MAIN" ] && continue
      MAINPATH="$d/node_modules/$pkg/$MAIN"
      [ -f "$MAINPATH" ] || BROKEN=$((BROKEN+1))
    done
    echo "  $d: $BROKEN packages with missing main entry"
  fi
done

echo ""
TOTAL_DAMAGE=$((SRC_GHOSTS + NM_ONLY + ICLOUD_STUBS + EMPTY_JS))
if [ $TOTAL_DAMAGE -eq 0 ]; then
  echo "✓ This copy is CLEAN — no iCloud damage detected."
else
  echo "✗ Total damage indicators: $TOTAL_DAMAGE"
  echo "  → Move project off ~/Desktop & ~/Documents, or disable iCloud Desktop sync,"
  echo "    then re-clone fresh from GitHub."
fi
