#!/bin/bash

# BORRAR TODO - VERSIÓN SIMPLE QUE FUNCIONA

API_KEY="SG.E4p-3JJxSk2EIRa4NBFJ8g"

echo ""
echo "╔════════════════════════════════════════════════════════════╗"
echo "║     BORRADO FINAL - SIMPLE Y FUNCIONAL                    ║"
echo "╚════════════════════════════════════════════════════════════╝"
echo ""

# ════════════════════════════════════════════════════════════════
# 1. BORRAR SINGLE SENDS
# ════════════════════════════════════════════════════════════════

echo "[1/2] Borrando Single Sends..."

SENDS=$(curl -s -H "Authorization: Bearer $API_KEY" \
  "https://api.sendgrid.com/v3/marketing/singlesends?page_size=100")

# Extraer IDs simples con awk
IDS=$(echo "$SENDS" | grep '"id"' | cut -d'"' -f4)

DELETED=0
for id in $IDS; do
  if [ ! -z "$id" ]; 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" = "204" ] || [ "$HTTP" = "200" ]; then
      echo "  ✓ $id"
      ((DELETED++))
    fi
  fi
done

echo "  Total: $DELETED"
echo ""

# ════════════════════════════════════════════════════════════════
# 2. BORRAR LISTAS
# ════════════════════════════════════════════════════════════════

echo "[2/2] Borrando Listas..."

LISTS=$(curl -s -H "Authorization: Bearer $API_KEY" \
  "https://api.sendgrid.com/v3/marketing/lists?page_size=100")

# Extraer IDs simples con awk
IDS=$(echo "$LISTS" | grep '"id"' | cut -d'"' -f4)

DELETED=0
for id in $IDS; do
  if [ ! -z "$id" ]; 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" = "204" ] || [ "$HTTP" = "200" ]; then
      echo "  ✓ $id"
      ((DELETED++))
    fi
  fi
done

echo "  Total: $DELETED"
echo ""

echo "╔════════════════════════════════════════════════════════════╗"
echo "║              ✅ LIMPIEZA COMPLETA                         ║"
echo "╚════════════════════════════════════════════════════════════╝"
echo ""

