-- ============================================================
-- RESET COMPLETO DE DATOS DE PRUEBA
-- Borra TODOS los tenants, cuentas, conversaciones y datos IA.
-- NO toca: admins ni platform_settings.
-- Usa DELETE FROM para compatibilidad con phpMyAdmin (sin FK issues).
-- ============================================================

-- Datos IA / inteligencia
DELETE FROM vale_actions;
DELETE FROM vale_recommendations;
DELETE FROM vale_findings;
DELETE FROM vale_memory;
DELETE FROM vale_constitution_versions;

-- Logs y eventos
DELETE FROM email_logs;
DELETE FROM stripe_events;
DELETE FROM agent_logs;

-- CRM / negocio (hijos de conversations/contacts → van primero)
DELETE FROM appointments;
DELETE FROM followups;
DELETE FROM leads;
DELETE FROM orders;
DELETE FROM subscriptions;

-- Conversaciones y mensajes
DELETE FROM messages;
DELETE FROM conversations;
DELETE FROM contacts;

-- Directorio
DELETE FROM listings_unclaimed;

-- Configuración por tenant
DELETE FROM tenant_whatsapp;
DELETE FROM staff;
DELETE FROM services;
DELETE FROM vale_config;

-- Auth
DELETE FROM oauth_states;
DELETE FROM google_tokens;

-- Monetización
DELETE FROM commissions;
DELETE FROM sellers;

-- Cuentas y tenants (padres al final)
DELETE FROM users;
DELETE FROM tenants;

-- Resetear auto-increment de las tablas principales
ALTER TABLE tenants       AUTO_INCREMENT = 1;
ALTER TABLE users         AUTO_INCREMENT = 1;
ALTER TABLE contacts      AUTO_INCREMENT = 1;
ALTER TABLE conversations AUTO_INCREMENT = 1;
ALTER TABLE messages      AUTO_INCREMENT = 1;
ALTER TABLE leads         AUTO_INCREMENT = 1;
ALTER TABLE appointments  AUTO_INCREMENT = 1;

-- Verificar que quedó limpio
SELECT 'tenants'               AS tabla, COUNT(*) AS filas FROM tenants
UNION ALL SELECT 'users',                COUNT(*) FROM users
UNION ALL SELECT 'contacts',             COUNT(*) FROM contacts
UNION ALL SELECT 'conversations',        COUNT(*) FROM conversations
UNION ALL SELECT 'messages',             COUNT(*) FROM messages
UNION ALL SELECT 'leads',                COUNT(*) FROM leads
UNION ALL SELECT 'appointments',         COUNT(*) FROM appointments
UNION ALL SELECT 'vale_memory',          COUNT(*) FROM vale_memory
UNION ALL SELECT 'google_tokens',        COUNT(*) FROM google_tokens
UNION ALL SELECT 'sellers',              COUNT(*) FROM sellers;
