-- ============================================================================
-- ASISMA CRM - Limpieza de datos de demostración
-- ============================================================================
-- Elimina todo lo creado por seeds_demo.sql y por las pruebas de la API.
-- Ejecutar: mysql -u root -p asisma_crm < database/limpiar_demo.sql
-- ============================================================================

USE asisma_crm;

-- Leads demo (y sus dependencias por CASCADE: actividades, cotizaciones,
-- asignaciones, whatsapp_mensajes, operaciones, cobros con SET NULL)
DELETE FROM leads WHERE telefono_real IN
  ('+56944444401', '+56944444402', '+56944444403', '+56944444404', '+56944444405', '+56955555001');

-- Cobros demo restantes
DELETE cl FROM cobros_leads cl
INNER JOIN franquiciados f ON cl.franquiciado_id = f.id
INNER JOIN empresas_franquiciados ef ON f.empresa_madre_id = ef.id
WHERE ef.rut IN ('761112223', '76111222-3', '763334445', '76333444-5');

-- Contactos y empresas cliente demo
DELETE FROM contactos WHERE telefono IN
  ('+56944444401', '+56944444402', '+56944444403', '+56944444404', '+56944444405', '+56955555001');
DELETE FROM empresas WHERE rut IN ('770001112', '77000111-2', '770003334', '77000333-4', '770005556', '77000555-6', '779998887', '77999888-7');

-- Instancia WhatsApp demo
DELETE FROM whatsapp_instancias WHERE nombre_instancia = 'demo-matriz';

-- Colas, usuarios y franquiciados demo
DELETE c FROM cola_asignacion c
INNER JOIN usuarios u ON c.usuario_id = u.id
WHERE u.email IN ('carla.munoz@partnersur.cl', 'diego.soto@partnersur.cl',
                  'pamela.rojas@andes.cl', 'pedro.pino@prueba.cl');

DELETE FROM usuarios WHERE email IN
  ('demo.admin@asisma.cl', 'maria.torres@partnersur.cl', 'carla.munoz@partnersur.cl',
   'diego.soto@partnersur.cl', 'pamela.rojas@andes.cl', 'pedro.pino@prueba.cl');

DELETE f FROM franquiciados f
INNER JOIN empresas_franquiciados ef ON f.empresa_madre_id = ef.id
WHERE ef.rut IN ('761112223', '76111222-3', '763334445', '76333444-5');

DELETE FROM empresas_franquiciados WHERE rut IN ('761112223', '76111222-3', '763334445', '76333444-5');
