#!/bin/bash
# ===========================================================
# Package Release — Standard Deliverable for M4
# Usage: bash scripts/package_release.sh <version> [file_uuid]
#   version:    e.g. v1.0.2
#   file_uuid:  default aeed71342a899fe4b4c57b7d41bcb692
# ===========================================================
set -euo pipefail

VERSION="${1:?Usage: $0 <version> [file_uuid]}"
UUID="${2:-aeed71342a899fe4b4c57b7d41bcb692}"
PG_BIN="/Users/accusys/pgsql/18.3/bin"
PROJECT="/Users/accusys/momentry_core_0.1"
OUTPUT="$PROJECT/release/phase1/${VERSION}"
M4_DIR="$PROJECT/docs_v1.0/M4_HANDOVER"
T0=$(date +%s)

echo "=========================================="
echo "  Package Release ${VERSION}"
echo "  UUID: ${UUID}"
echo "=========================================="
echo ""

mkdir -p "$OUTPUT"

# ── Step 1: DB backup (post-correction) ──
echo "[1/5] DB backup (post-correction)..."
$PG_BIN/pg_dump -U accusys -d momentry \
  --schema=dev \
  --table=dev.chunk \
  --table=dev.chunk_vectors \
  --data-only --column-inserts \
  > "$OUTPUT/dev_backup_post_correction.sql" 2>/dev/null
echo "  $(ls -lh "$OUTPUT/dev_backup_post_correction.sql" | awk '{print $5}')"

# ── Step 2: Verify counts ──
echo "[2/5] Verify counts..."
SENTENCE=$($PG_BIN/psql -U accusys -d momentry -t -A -c "SELECT count(*) FROM dev.chunk WHERE file_uuid='$UUID' AND chunk_type='sentence';" 2>/dev/null)
VECTORS=$($PG_BIN/psql -U accusys -d momentry -t -A -c "SELECT count(*) FROM dev.chunk_vectors WHERE uuid='$UUID';" 2>/dev/null)
MATCHED=$($PG_BIN/psql -U accusys -d momentry -t -A -c "
  SELECT count(*) FROM dev.chunk_vectors cv
  JOIN dev.chunk c ON c.file_uuid=cv.uuid AND c.chunk_id=cv.chunk_id
  WHERE cv.uuid='$UUID';" 2>/dev/null)
echo "  sentence chunks: $SENTENCE"
echo "  vectors: $VECTORS"
echo "  matched: $MATCHED"

if [ "$SENTENCE" != "$VECTORS" ] || [ "$VECTORS" != "$MATCHED" ]; then
  echo "  ❌ MISMATCH — aborting"
  exit 1
fi
echo "  ✅ All counts consistent"

# ── Step 3: Source code archive ──
echo "[3/5] Source code (git archive)..."
cd "$PROJECT"
git archive --format=tar.gz -o "$OUTPUT/momentry_core_${VERSION}_source.tar.gz" HEAD 2>/dev/null
echo "  $(ls -lh "$OUTPUT/momentry_core_${VERSION}_source.tar.gz" | awk '{print $5}')"

# ── Step 4: Correction record ──
echo "[4/5] Correction record (asr-1.json)..."
ASR1="/Users/accusys/momentry/output_dev/${UUID}.asr-1.json"
if [ -f "$ASR1" ]; then
  cp "$ASR1" "$OUTPUT/"
  echo "  $(ls -lh "$OUTPUT/${UUID}.asr-1.json" | awk '{print $5}')"
else
  echo "  ⚠️ No asr-1.json found for $UUID"
fi

# ── Step 5: Copy to M4_HANDOVER ──
echo "[5/5] Deploy to M4_HANDOVER..."
rm -rf "${M4_DIR:?}"/*
cp "$OUTPUT/dev_backup_post_correction.sql" "$M4_DIR/"
cp "$OUTPUT/momentry_core_${VERSION}_source.tar.gz" "$M4_DIR/"
if [ -f "$OUTPUT/${UUID}.asr-1.json" ]; then
  cp "$OUTPUT/${UUID}.asr-1.json" "$M4_DIR/"
fi
# Scripts
for s in generate_asr1.py apply_asr_corrections.py clean_sentence_text.py pipeline_status.py; do
  cp "$PROJECT/scripts/$s" "$M4_DIR/"
done
# Handover doc + test script
cp "$PROJECT/docs_v1.0/API_V1.0.0/RELEASE/PHASE1_HANDOVER_V1.0.0.md" "$M4_DIR/HANDOVER_${VERSION}.md"
cp "/tmp/test_api.sh" "$M4_DIR/api_test.sh"

# Create RELEASE_INFO
cat > "$M4_DIR/RELEASE_INFO.txt" << EOF
Release: ${VERSION}
Date: $(date +%Y-%m-%d)
UUID: ${UUID}
Pipeline: $(python3 $PROJECT/scripts/pipeline_status.py 2>/dev/null | grep "TOTAL" | tr -d ' ')
EOF

ELAPSED=$(($(date +%s) - T0))
echo ""
echo "=========================================="
echo "  Package ${VERSION} complete (${ELAPSED}s)"
echo "  Output: $OUTPUT"
echo "  M4:     $M4_DIR"
echo "=========================================="
