#!/bin/bash

# LIMPIEZA SELECTIVA - SOLO BORRA LO QUE CREAMOS NOSOTROS
# Single Sends: Campaign_*
# Listas: Campaign_LINDERLAKE_Random_* o Domain_*
# NADA MÁS
# FIX: Máximo 100 por página (SendGrid limit)

API_KEY="SG.E4p-3JJxSk2EIRa4NBFJ8g"

echo ""
echo "╔════════════════════════════════════════════════════════════╗"
echo "║     LIMPIEZA SELECTIVA - SOLO NUESTRA BASURA              ║"
echo "╚════════════════════════════════════════════════════════════╝"
echo ""

# ════════════════════════════════════════════════════════════════
# 1. BORRAR SOLO Single Sends CON NOMBRE "Campaign_"
# ════════════════════════════════════════════════════════════════

echo "[1/2] Borrando Single Sends con nombre 'Campaign_'..."

DELETED=0
PAGE=1

while true; do
  SENDS=$(curl -s -H "Authorization: Bearer $API_KEY" \
    "https://api.sendgrid.com/v3/marketing/singlesends?page_size=100&page=$PAGE")
  
  # Contar resultados
  COUNT=$(echo "$SENDS" | grep -o '"id"' | wc -l)
  
  if [ "$COUNT" -eq 0 ]; then
    break
  fi
  
  echo "  Página $PAGE: procesando $COUNT..."
  
  # Extraer pares de ID y nombre
  echo "$SENDS" | grep -oP '(?<="id":")[^"]+' > /tmp/send_ids.txt
  echo "$SENDS" | grep -oP '(?<="name":")[^"]+' > /tmp/send_names.txt
  
  # Procesar línea por línea
  paste /tmp/send_ids.txt /tmp/send_names.txt | while IFS=$'\t' read id name; do
    # SOLO si empieza con "Campaign_"
    if [[ "$name" == Campaign_* ]]; then
      HTTP=$(curl -s -w "%{http_code}" -o /dev/null -X DELETE \
        -H "Authorization: Bearer $API_KEY" \
        "https://api.sendgrid.com/v3/marketing/singlesends/$id")
      
      if [ "$HTTP" -eq 204 ] || [ "$HTTP" -eq 200 ]; then
        echo "    ✓ $name"
        ((DELETED++))
      fi
    fi
  done
  
  PAGE=$((PAGE + 1))
done

echo "  Total borrados: $DELETED"
echo ""

# ════════════════════════════════════════════════════════════════
# 2. BORRAR SOLO Listas CON NOMBRE "Campaign_LINDERLAKE_Random_" O "Domain_"
# ════════════════════════════════════════════════════════════════

echo "[2/2] Borrando Listas con nombre 'Campaign_LINDERLAKE_Random_*' o 'Domain_*'..."

DELETED=0
PAGE=1

while true; do
  LISTS=$(curl -s -H "Authorization: Bearer $API_KEY" \
    "https://api.sendgrid.com/v3/marketing/lists?page_size=100&page=$PAGE")
  
  # Contar resultados
  COUNT=$(echo "$LISTS" | grep -o '"id"' | wc -l)
  
  if [ "$COUNT" -eq 0 ]; then
    break
  fi
  
  echo "  Página $PAGE: procesando $COUNT..."
  
  # Extraer pares de ID y nombre
  echo "$LISTS" | grep -oP '(?<="id":")[^"]+' > /tmp/list_ids.txt
  echo "$LISTS" | grep -oP '(?<="name":")[^"]+' > /tmp/list_names.txt
  
  # Procesar línea por línea
  paste /tmp/list_ids.txt /tmp/list_names.txt | while IFS=$'\t' read id name; do
    # SOLO si empieza con "Campaign_LINDERLAKE_Random_" o "Domain_"
    if [[ "$name" == Campaign_LINDERLAKE_Random_* ]] || [[ "$name" == Domain_* ]]; then
      HTTP=$(curl -s -w "%{http_code}" -o /dev/null -X DELETE \
        -H "Authorization: Bearer $API_KEY" \
        "https://api.sendgrid.com/v3/marketing/lists/$id")
      
      if [ "$HTTP" -eq 204 ] || [ "$HTTP" -eq 200 ]; then
        echo "    ✓ $name"
        ((DELETED++))
      fi
    fi
  done
  
  PAGE=$((PAGE + 1))
done

echo "  Total borradas: $DELETED"
echo ""

# Limpiar temporales
rm -f /tmp/send_ids.txt /tmp/send_names.txt /tmp/list_ids.txt /tmp/list_names.txt

echo "╔════════════════════════════════════════════════════════════╗"
echo "║         ✅ BASURA ELIMINADA - TODO LO VIEJO INTACTO        ║"
echo "╚════════════════════════════════════════════════════════════╝"
echo ""