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

source ./0-config_var_setup.sh

MODE="${1:-dry-run}"
WORKDIR="/tmp/sendgrid_remote_cleanup_$(date +%Y%m%d_%H%M%S)"
mkdir -p "$WORKDIR"

EXPORT_API="https://api.sendgrid.com/v3/marketing/contacts/exports"

echo "======================================"
echo "97 CLEAN SENDGRID DEAD CONTACTS"
echo "MODE: $MODE"
echo "WORKDIR: $WORKDIR"
echo "======================================"

echo "1) Generando candidatos desde MySQL, excluyendo ASM..."

mysql -u"$MYSQL_USER" -p"$MYSQL_PASS" "$MYSQL_DB" -Nse "
SELECT DISTINCT LOWER(s.email)
FROM sendgrid_suppressions s
WHERE s.suppression_type IN ('bounce','block','spam_report','invalid_email')
AND NOT EXISTS (
    SELECT 1
    FROM sendgrid_suppressions a
    WHERE a.email=s.email
    AND a.suppression_type='asm_unsubscribe'
);
" > "$WORKDIR/delete_candidate_emails.txt"

echo "Candidatos:"
wc -l "$WORKDIR/delete_candidate_emails.txt"

echo "2) Exportando contactos actuales de SendGrid..."

curl -sS -X POST "$EXPORT_API" \
-H "Authorization: Bearer $SENDGRID_API_KEY" \
-H "Content-Type: application/json" \
--data '{}' > "$WORKDIR/export.json"

EXPORT_ID="$(jq -r '.id // empty' "$WORKDIR/export.json")"
echo "Export ID: $EXPORT_ID"

while true; do
    curl -sS "$EXPORT_API/$EXPORT_ID" \
    -H "Authorization: Bearer $SENDGRID_API_KEY" > "$WORKDIR/export.json"

    STATUS="$(jq -r '.status // empty' "$WORKDIR/export.json")"
    URL="$(jq -r '.urls[0] // empty' "$WORKDIR/export.json")"

    echo "Status: $STATUS"

    [[ -n "$URL" ]] && break
    [[ "$STATUS" == "failed" ]] && { cat "$WORKDIR/export.json"; exit 1; }

    sleep 10
done

curl -fL -sS "$URL" -o "$WORKDIR/sendgrid_download"

python3 - "$WORKDIR/sendgrid_download" "$WORKDIR/sendgrid_contacts.csv" <<'PY'
import sys, zipfile, gzip, shutil

src, dst = sys.argv[1], sys.argv[2]

with open(src, "rb") as f:
    head = f.read(4)

if zipfile.is_zipfile(src):
    with zipfile.ZipFile(src) as z:
        csvs = [n for n in z.namelist() if n.lower().endswith(".csv")]
        with z.open(csvs[0]) as f_in, open(dst, "wb") as f_out:
            shutil.copyfileobj(f_in, f_out)
elif head[:2] == b"\x1f\x8b":
    with gzip.open(src, "rb") as f_in, open(dst, "wb") as f_out:
        shutil.copyfileobj(f_in, f_out)
else:
    shutil.copyfile(src, dst)
PY

echo "3) Cruzando candidatos con export de SendGrid..."

python3 - "$WORKDIR/sendgrid_contacts.csv" "$WORKDIR/delete_candidate_emails.txt" "$WORKDIR/delete_ids.txt" "$WORKDIR/delete_preview.tsv" <<'PY'
import csv, sys

csv_file, candidates_file, ids_file, preview_file = sys.argv[1:5]

with open(candidates_file, encoding="utf-8") as f:
    candidates = {line.strip().lower() for line in f if line.strip()}

with open(csv_file, newline="", encoding="utf-8-sig") as f:
    reader = csv.DictReader(f)
    fields = reader.fieldnames or []

    email_col = None
    id_col = None

    for col in fields:
        if col.lower() in ("email", "email_address", "identifier"):
            email_col = col
        if col.lower() in ("id", "contact_id", "contact id", "contactid"):
            id_col = col

    if not email_col:
        raise SystemExit(f"ERROR: No encontré columna EMAIL. Headers: {fields}")

    if not id_col:
        raise SystemExit(f"ERROR: No encontré columna contact ID. Headers: {fields}")

    ids = []
    previews = []

    for row in reader:
        email = (row.get(email_col) or "").strip().lower()
        cid = (row.get(id_col) or "").strip()

        if email in candidates and cid:
            ids.append(cid)
            previews.append((email, cid))

with open(ids_file, "w", encoding="utf-8") as out:
    for cid in sorted(set(ids)):
        out.write(cid + "\n")

with open(preview_file, "w", encoding="utf-8") as out:
    out.write("email\tcontact_id\n")
    for email, cid in previews:
        out.write(f"{email}\t{cid}\n")

print(f"Candidates in MySQL: {len(candidates)}")
print(f"Delete IDs found in SendGrid: {len(set(ids))}")
PY

echo ""
echo "Resumen:"
echo "Emails candidatos MySQL:"
wc -l "$WORKDIR/delete_candidate_emails.txt"

echo "IDs encontrados en SendGrid:"
wc -l "$WORKDIR/delete_ids.txt"

echo ""
echo "Preview:"
head "$WORKDIR/delete_preview.tsv"

if [[ "$MODE" != "--execute" ]]; then
    echo ""
    echo "DRY RUN ONLY"
    echo "Para ejecutar REAL:"
    echo "./97-clean_sendgrid_dead_contacts.sh --execute"
    exit 0
fi

echo ""
echo "4) EJECUTANDO DELETE REAL EN SENDGRID..."

split -l 100 "$WORKDIR/delete_ids.txt" "$WORKDIR/batch_"

for FILE in "$WORKDIR"/batch_*; do
    IDS="$(paste -sd, "$FILE")"

    [[ -z "$IDS" ]] && continue

    echo "Deleting batch $(basename "$FILE")..."

    curl -sS -X DELETE \
    -H "Authorization: Bearer $SENDGRID_API_KEY" \
    "https://api.sendgrid.com/v3/marketing/contacts?ids=$IDS" \
    >> "$WORKDIR/delete_responses.jsonl"

    echo "" >> "$WORKDIR/delete_responses.jsonl"
    sleep 1
done

echo ""
echo "5) Contact count después del envío de jobs:"
curl -sS -H "Authorization: Bearer $SENDGRID_API_KEY" \
https://api.sendgrid.com/v3/marketing/contacts/count | jq .

echo ""
echo "DONE"
echo "Workdir: $WORKDIR"