#!/usr/bin/env bash
set -euo pipefail

# Usage:
#   bash scripts/auto_recover_and_build.sh [PROJECT_DIR]
# Default PROJECT_DIR is current working directory.

PROJECT_DIR="${1:-$(pwd)}"
PROJECT_DIR="$(realpath "$PROJECT_DIR")"
TS="$(date +%Y%m%d_%H%M%S)"

if [[ ! -f "$PROJECT_DIR/main.py" ]]; then
  echo "ERROR: main.py not found under PROJECT_DIR: $PROJECT_DIR" >&2
  exit 1
fi

BACKUP_DIR="${PROJECT_DIR}.backup_${TS}"
TRASH_DIR="$PROJECT_DIR/trash/${TS}"

printf "[1/7] Backup project -> %s\n" "$BACKUP_DIR"
cp -a "$PROJECT_DIR" "$BACKUP_DIR"

printf "[2/7] Quarantine noisy/generated files -> %s\n" "$TRASH_DIR"
mkdir -p "$TRASH_DIR"

# Candidate directories to quarantine (safe move, no delete)
for d in \
  apk_test \
  helloworld \
  build \
  dist \
  flutter.参考 \
  __pycache__ \
  .pytest_cache \
  .mypy_cache
 do
  if [[ -e "$PROJECT_DIR/$d" ]]; then
    mv "$PROJECT_DIR/$d" "$TRASH_DIR/"
  fi
done

# Candidate files to quarantine (safe move, no delete)
shopt -s nullglob
for f in \
  "$PROJECT_DIR"/app_*.py \
  "$PROJECT_DIR"/test_*.py \
  "$PROJECT_DIR"/debug_test.py \
  "$PROJECT_DIR"/run_*.py \
  "$PROJECT_DIR"/main_simple.py \
  "$PROJECT_DIR"/minimal.py \
  "$PROJECT_DIR"/*.spec
 do
  if [[ -e "$f" ]]; then
    mv "$f" "$TRASH_DIR/"
  fi
done
shopt -u nullglob

printf "[3/7] Ensure .gitignore has generated-file rules\n"
GITIGNORE="$PROJECT_DIR/.gitignore"
touch "$GITIGNORE"
append_if_missing() {
  local line="$1"
  grep -Fqx "$line" "$GITIGNORE" || echo "$line" >> "$GITIGNORE"
}

append_if_missing ""
append_if_missing "# build/cache"
append_if_missing "build/"
append_if_missing "dist/"
append_if_missing "__pycache__/"
append_if_missing "*.pyc"
append_if_missing ""
append_if_missing "# generated"
append_if_missing "generated_pdfs/"
append_if_missing "audit_export/"
append_if_missing "trash/"
append_if_missing ""
append_if_missing "# experiments"
append_if_missing "apk_test/"
append_if_missing "helloworld/"

printf "[4/7] Initialize git baseline if needed\n"
if [[ ! -d "$PROJECT_DIR/.git" ]]; then
  git -C "$PROJECT_DIR" init
fi

printf "[5/7] Stage core project files\n"
# Stage only the likely core set; ignore errors for missing paths.
git -C "$PROJECT_DIR" add \
  .gitignore \
  README.md \
  requirements.txt \
  main.py \
  models \
  services \
  components \
  sales.db \
  sales_assist.db \
  scripts || true

if ! git -C "$PROJECT_DIR" diff --cached --quiet; then
  git -C "$PROJECT_DIR" commit -m "baseline: quarantine generated files and stabilize project"
else
  printf "No staged changes to commit.\n"
fi

printf "[6/7] Optional runtime check (python main.py)\n"
printf "Skipped by default. Run manually if needed:\n"
printf "  source %s/.venv/bin/activate && python %s/main.py\n" "$PROJECT_DIR" "$PROJECT_DIR"

printf "[7/7] Optional APK build check\n"
printf "Run manually once your Flet build config is ready:\n"
printf "  source %s/.venv/bin/activate && flet build apk %s\n" "$PROJECT_DIR" "$PROJECT_DIR"

printf "Done.\n"
printf "Backup: %s\n" "$BACKUP_DIR"
printf "Quarantine: %s\n" "$TRASH_DIR"
