#!/usr/bin/env bash
# Phase 1 setup — copy archive backend/frontend source files into kitchen/
# Run once from the repo root: bash kitchen/_copy_from_archive.sh
set -euo pipefail

ARCHIVE=/home/jtr/laptop-archive/kitchen-invoice-flash-docker
DEST=/home/jtr/dev/HNF-PROXMOX/kitchen

echo "==> Copying backend source from archive..."

# API routers (all except kds.py)
cp -r "$ARCHIVE/backend/api" "$DEST/backend/"
rm -f "$DEST/backend/api/kds.py"

# Services (exclude KDS-specific)
cp -r "$ARCHIVE/backend/services" "$DEST/backend/"
rm -f "$DEST/backend/services/signalr_listener.py"
rm -f "$DEST/backend/services/kds_graphql.py"

# Models (exclude kds.py — tables stay in DB for KDS app migration later)
cp -r "$ARCHIVE/backend/models" "$DEST/backend/"
rm -f "$DEST/backend/models/kds.py"

# Migrations (exclude KDS migrations)
cp -r "$ARCHIVE/backend/migrations" "$DEST/backend/"
rm -f "$DEST/backend/migrations/add_kds_tables.py"
rm -f "$DEST/backend/migrations/add_kds_course_flow.py"
rm -f "$DEST/backend/migrations/add_kds_order_tracking.py"
rm -f "$DEST/backend/migrations/add_kds_bookings_refresh.py"

# OCR pipeline
cp -r "$ARCHIVE/backend/ocr" "$DEST/backend/"

# Core files (auth.py and main.py are rewritten — do not copy)
cp "$ARCHIVE/backend/database.py" "$DEST/backend/"
cp "$ARCHIVE/backend/scheduler.py" "$DEST/backend/"

# Fix auth import in every copied router
echo "==> Patching auth imports in api/*.py..."
find "$DEST/backend/api" -name "*.py" \
  -exec sed -i 's/from auth\.jwt import get_current_user/from auth import get_current_user, require_cap/g' {} \;

echo "==> Copying frontend source from archive..."

# Components (all 45 — shell conversion done; B5 tracks full design-system pass)
cp -r "$ARCHIVE/frontend/src/components" "$DEST/frontend/src/"

# Pages (exclude Login, KDS, KDSApp — those belong to other apps)
mkdir -p "$DEST/frontend/src/pages"
for page in Settings NewbookData ResosData ResidentsTableChart BookingsStats WastageLogbook UploadApp; do
  src="$ARCHIVE/frontend/src/pages/${page}.tsx"
  [ -f "$src" ] && cp "$src" "$DEST/frontend/src/pages/" && echo "  pages/${page}.tsx"
done

# Utilities
cp -r "$ARCHIVE/frontend/src/utils" "$DEST/frontend/src/"

# Public dir (sw.js for UploadApp PWA service worker)
[ -d "$ARCHIVE/frontend/public" ] && cp -r "$ARCHIVE/frontend/public" "$DEST/frontend/"

# index.html (vite entry — already exists from scaffold, only copy if missing)
[ ! -f "$DEST/frontend/index.html" ] && \
  [ -f "$ARCHIVE/frontend/index.html" ] && \
  cp "$ARCHIVE/frontend/index.html" "$DEST/frontend/"

echo "==> Copying archive docs..."
mkdir -p "$DEST/docs/archive"
cp "$ARCHIVE/"PLAN_*.md "$DEST/docs/archive/" 2>/dev/null || true
for f in RECIPE-SYSTEM-PLAN.md PURCHASE-ORDER-PLAN.md MENUS-PLAN.md LLM-INTEGRATION-PLAN.md LLM-MANIFEST.md; do
  [ -f "$ARCHIVE/$f" ] && cp "$ARCHIVE/$f" "$DEST/docs/archive/"
done
[ -d "$ARCHIVE/docs" ] && cp "$ARCHIVE/docs/"*.md "$DEST/docs/archive/" 2>/dev/null || true

echo ""
echo "Done. Next: docker compose build (backend) && npm run build (frontend)."
echo "Do NOT push to Forgejo until Phase 4 registration is complete."
