#!/bin/bash
# Removes E2E test data that leaked into the database.
# Documents with title LIKE 'E2E%' and persons whose last_name ends with a
# timestamp suffix (Sender-<ts> / Receiver-<ts> from bilateral fixtures), plus
# the manually-created E2E Testperson from persons.spec.ts.
set -euo pipefail

CONTAINER="archive-db"
DB_USER="archive_user"
DB_NAME="family_archive_db"

DRY_RUN=false
if [[ "${1:-}" == "--dry-run" ]]; then
    DRY_RUN=true
fi

run_sql() {
    docker exec "$CONTAINER" psql -U "$DB_USER" -d "$DB_NAME" -c "$1"
}

# ── Dry-run summary ──────────────────────────────────────────────────────────

echo ""
echo "E2E test data found in the database:"
echo ""

run_sql "
SELECT count(*) AS e2e_documents
FROM documents
WHERE title LIKE 'E2E%';
"

run_sql "
SELECT count(*) AS e2e_persons
FROM persons
WHERE
  -- bilateral fixture: Visual Sender-<ts>, A11y Receiver-<ts>, etc.
  last_name ~ '^(Sender|Receiver)-[0-9]{10,}$'
  -- persons.spec.ts: E2E Testperson
  OR (first_name = 'E2E' AND last_name = 'Testperson');
"

run_sql "
SELECT count(*) AS e2e_users
FROM users
WHERE first_name = 'E2E' AND last_name = 'Testuser';
"

if $DRY_RUN; then
    echo "Dry run — no changes made."
    exit 0
fi

# ── Confirmation ─────────────────────────────────────────────────────────────

echo ""
echo "This will permanently delete all E2E test documents, persons, and users."
read -rp "Type 'yes' to continue: " CONFIRM
[ "$CONFIRM" != "yes" ] && echo "Aborted." && exit 0

# ── Cleanup ──────────────────────────────────────────────────────────────────

echo ""
echo "Deleting E2E test data..."

run_sql "
-- Notifications have no FK on document_id, must be cleaned manually.
DELETE FROM notifications
WHERE document_id IN (SELECT id FROM documents WHERE title LIKE 'E2E%');

-- Delete E2E documents. All dependent tables cascade:
--   document_receivers, document_tags, document_training_labels,
--   document_versions, document_annotations, document_comments,
--   comment_mentions, transcription_blocks, transcription_block_versions,
--   block_mentioned_persons
DELETE FROM documents WHERE title LIKE 'E2E%';

-- Match all test persons:
--   bilateral fixtures  → last_name = 'Sender-<timestamp>' or 'Receiver-<timestamp>'
--   persons.spec.ts     → first_name = 'E2E', last_name = 'Testperson'
-- Nullify sender FK on any non-E2E documents first (prevents FK violation).
UPDATE documents
SET sender_id = NULL
WHERE sender_id IN (
  SELECT id FROM persons
  WHERE last_name ~ '^(Sender|Receiver)-[0-9]{10,}$'
     OR (first_name = 'E2E' AND last_name = 'Testperson')
);

-- Remove receiver links from non-E2E documents (same edge case).
DELETE FROM document_receivers
WHERE person_id IN (
  SELECT id FROM persons
  WHERE last_name ~ '^(Sender|Receiver)-[0-9]{10,}$'
     OR (first_name = 'E2E' AND last_name = 'Testperson')
);

-- Delete test persons. person_name_aliases cascade automatically.
DELETE FROM persons
WHERE last_name ~ '^(Sender|Receiver)-[0-9]{10,}$'
   OR (first_name = 'E2E' AND last_name = 'Testperson');

-- Delete leaked E2E users (created but not cleaned up in admin.spec.ts).
DELETE FROM users_groups
WHERE user_id IN (SELECT id FROM users WHERE first_name = 'E2E' AND last_name = 'Testuser');

DELETE FROM users
WHERE first_name = 'E2E' AND last_name = 'Testuser';
" --quiet

echo "Done."
