#!/usr/bin/env bash
# Deep clean script — supports platform filtering
#
# Usage:
#   bash scripts/clean.sh                   # clean all platforms (default)
#   bash scripts/clean.sh android           # clean Android only
#   bash scripts/clean.sh ios               # clean iOS only
#   bash scripts/clean.sh macos             # clean macOS only
#   bash scripts/clean.sh windows          # clean Windows only
#   bash scripts/clean.sh android ios       # clean multiple platforms
#
# Via melos:
#   melos run clean:deep                    # all platforms
#   melos run clean:deep -- android         # Android only
#   melos run clean:deep -- ios macos       # iOS + macOS

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(dirname "$SCRIPT_DIR")"
APP_DIR="$ROOT_DIR/apps/im_app"

# ── Parse platforms ────────────────────────────────────────────────────────
ALL_PLATFORMS=(android ios macos windows)

if [ $# -eq 0 ]; then
  TARGETS=("${ALL_PLATFORMS[@]}")
else
  TARGETS=("$@")
fi

should_clean() {
  local platform="$1"
  for t in "${TARGETS[@]}"; do
    [[ "$t" == "$platform" ]] && return 0
  done
  return 1
}

echo "==> Platforms: ${TARGETS[*]}"

# ── Flutter & Dart (always runs) ───────────────────────────────────────────
echo "==> flutter clean..."
cd "$APP_DIR"
flutter clean

echo "==> Removing .dart_tool directories..."
while IFS= read -r -d '' dir; do
  echo "    rm -rf $dir"
  rm -rf "$dir"
done < <(find "$ROOT_DIR" -type d -name ".dart_tool" -not -path "*/.git/*" -print0)

echo "==> Removing generated Dart files (*.g.dart / *.freezed.dart / *.mocks.dart)..."
while IFS= read -r -d '' file; do
  echo "    rm $file"
  rm "$file"
done < <(find "$ROOT_DIR" -type f \
  \( -name "*.g.dart" -o -name "*.freezed.dart" -o -name "*.mocks.dart" \) \
  -not -path "*/.git/*" -not -path "*/build/*" -print0)

# ── Android ────────────────────────────────────────────────────────────────
if should_clean android; then
  echo "==> Cleaning Android Gradle..."
  cd "$APP_DIR/android"
  chmod +x gradlew
  ./gradlew clean
fi

# ── iOS ────────────────────────────────────────────────────────────────────
if should_clean ios; then
  IOS_DIR="$APP_DIR/ios"
  if [ -d "$IOS_DIR" ]; then
    echo "==> Cleaning iOS Pods..."
    rm -rf "$IOS_DIR/Pods"
    rm -f  "$IOS_DIR/Podfile.lock"
    rm -rf "$IOS_DIR/.symlinks"
    if command -v pod &>/dev/null; then
      cd "$IOS_DIR" && pod cache clean --all
    fi
  fi
fi

# ── macOS ──────────────────────────────────────────────────────────────────
if should_clean macos; then
  MACOS_DIR="$APP_DIR/macos"
  if [ -d "$MACOS_DIR" ]; then
    echo "==> Cleaning macOS Pods..."
    rm -rf "$MACOS_DIR/Pods"
    rm -f  "$MACOS_DIR/Podfile.lock"
    if command -v pod &>/dev/null; then
      cd "$MACOS_DIR" && pod cache clean --all
    fi
  fi
fi

# ── Windows ────────────────────────────────────────────────────────────────
if should_clean windows; then
  WIN_DIR="$APP_DIR/windows"
  if [ -d "$WIN_DIR" ]; then
    echo "==> Cleaning Windows build artifacts..."
    rm -rf "$WIN_DIR/flutter/ephemeral"
    find "$WIN_DIR" -name "CMakeCache.txt" -delete
    find "$WIN_DIR" -name "CMakeFiles" -type d -exec rm -rf {} + 2>/dev/null || true
  fi
fi

# ── Re-fetch dependencies ──────────────────────────────────────────────────
echo "==> Running dart pub get at workspace root..."
cd "$ROOT_DIR"
dart pub get

echo ""
echo "Deep clean complete. Platforms cleaned: ${TARGETS[*]}"
echo "Next: melos run gen"
