-- =========================================================
-- Script de vidage des tables de données (PRODUCTION)
-- Option B : Vider TOUTES les tables SAUF x_* et entité 1
-- Conserve les tables de référence x_*
-- Conserve l'entité id=1 (super admins) et ses users/opérations
-- Date: 2025-10-09
-- =========================================================

-- Désactiver les contraintes de clés étrangères temporairement
SET FOREIGN_KEY_CHECKS = 0;

-- =========================================================
-- TABLES CHAT
-- =========================================================
TRUNCATE TABLE chat_read_receipts;
TRUNCATE TABLE chat_messages;
TRUNCATE TABLE chat_participants;
TRUNCATE TABLE chat_rooms;

-- =========================================================
-- TABLES EMAIL
-- =========================================================
TRUNCATE TABLE email_queue;
TRUNCATE TABLE email_counter;

-- =========================================================
-- TABLES SÉCURITÉ
-- =========================================================
TRUNCATE TABLE sec_failed_login_attempts;
TRUNCATE TABLE sec_blocked_ips;
TRUNCATE TABLE sec_alerts;
TRUNCATE TABLE sec_performance_metrics;

-- =========================================================
-- TABLES STRIPE
-- =========================================================
TRUNCATE TABLE stripe_webhooks;
TRUNCATE TABLE stripe_payment_history;
TRUNCATE TABLE stripe_refunds;
TRUNCATE TABLE stripe_terminal_readers;
TRUNCATE TABLE stripe_android_certified_devices;
-- NOTE: stripe_accounts conservé car lié à entites via FK

-- =========================================================
-- TABLES DONNÉES MÉTIER (conserver entité 1)
-- =========================================================

-- 1. Supprimer les devices des users (sauf entité 1)
DELETE FROM user_devices
WHERE fk_user IN (SELECT id FROM users WHERE fk_entite != 1);

-- 2. Supprimer les sessions (sauf users entité 1)
DELETE FROM z_sessions
WHERE fk_user IN (SELECT id FROM users WHERE fk_entite != 1);

-- 3. Supprimer les médias (sauf entité 1)
DELETE FROM medias WHERE fk_entite != 1;

-- 4. Supprimer les comptes Stripe (sauf entité 1)
DELETE FROM stripe_accounts WHERE fk_entite != 1;

-- 5. Supprimer l'historique des passages (sauf entité 1)
DELETE FROM ope_pass_histo
WHERE fk_pass IN (
    SELECT id FROM ope_pass
    WHERE fk_operation IN (
        SELECT id FROM operations WHERE fk_entite != 1
    )
);

-- 6. Supprimer les passages (sauf ceux des opérations de l'entité 1)
DELETE FROM ope_pass
WHERE fk_operation IN (
    SELECT id FROM operations WHERE fk_entite != 1
);

-- 7. Supprimer les associations users-sectors (sauf entité 1)
DELETE FROM ope_users_sectors
WHERE fk_operation IN (
    SELECT id FROM operations WHERE fk_entite != 1
);

-- 8. Supprimer les associations users-operations (sauf entité 1)
DELETE FROM ope_users
WHERE fk_operation IN (
    SELECT id FROM operations WHERE fk_entite != 1
);

-- 9. Supprimer les adresses de secteurs (sauf entité 1)
DELETE FROM sectors_adresses
WHERE fk_sector IN (
    SELECT id FROM ope_sectors WHERE fk_operation IN (
        SELECT id FROM operations WHERE fk_entite != 1
    )
);

-- 10. Supprimer les secteurs (sauf ceux de l'entité 1)
DELETE FROM ope_sectors
WHERE fk_operation IN (
    SELECT id FROM operations WHERE fk_entite != 1
);

-- 11. Supprimer les opérations (sauf celles de l'entité 1)
DELETE FROM operations WHERE fk_entite != 1;

-- 12. Supprimer les utilisateurs (sauf ceux de l'entité 1)
DELETE FROM users WHERE fk_entite != 1;

-- 13. Supprimer les entités (sauf l'entité 1)
DELETE FROM entites WHERE id != 1;

-- 14. Vider la table params (paramètres globaux)
TRUNCATE TABLE params;

-- Réactiver les contraintes de clés étrangères
SET FOREIGN_KEY_CHECKS = 1;

-- =========================================================
-- VÉRIFICATIONS POST-VIDAGE
-- =========================================================

SELECT '========================================' as '';
SELECT '=== TABLES DE DONNÉES (après vidage) ===' as '';
SELECT '========================================' as '';

SELECT 'entites' as table_name, COUNT(*) as count FROM entites
UNION ALL SELECT 'users', COUNT(*) FROM users
UNION ALL SELECT 'operations', COUNT(*) FROM operations
UNION ALL SELECT 'ope_sectors', COUNT(*) FROM ope_sectors
UNION ALL SELECT 'ope_pass', COUNT(*) FROM ope_pass
UNION ALL SELECT 'medias', COUNT(*) FROM medias
UNION ALL SELECT 'user_devices', COUNT(*) FROM user_devices
UNION ALL SELECT 'z_sessions', COUNT(*) FROM z_sessions;

SELECT '' as '';
SELECT '========================================' as '';
SELECT '=== TABLES CHAT (doivent être vides) ===' as '';
SELECT '========================================' as '';

SELECT 'chat_rooms' as table_name, COUNT(*) as count FROM chat_rooms
UNION ALL SELECT 'chat_messages', COUNT(*) FROM chat_messages
UNION ALL SELECT 'chat_participants', COUNT(*) FROM chat_participants
UNION ALL SELECT 'chat_read_receipts', COUNT(*) FROM chat_read_receipts;

SELECT '' as '';
SELECT '========================================' as '';
SELECT '=== TABLES STRIPE (vides sauf accounts) ===' as '';
SELECT '========================================' as '';

SELECT 'stripe_accounts' as table_name, COUNT(*) as count FROM stripe_accounts
UNION ALL SELECT 'stripe_webhooks', COUNT(*) FROM stripe_webhooks
UNION ALL SELECT 'stripe_terminal_readers', COUNT(*) FROM stripe_terminal_readers
UNION ALL SELECT 'stripe_android_certified_devices', COUNT(*) FROM stripe_android_certified_devices;

SELECT '' as '';
SELECT '========================================' as '';
SELECT '=== ENTITÉ 1 (doit être conservée) ===' as '';
SELECT '========================================' as '';

SELECT id, encrypted_name, encrypted_email, chk_active FROM entites WHERE id = 1;

SELECT '' as '';
SELECT 'Nombre de users entité 1:' as info, COUNT(*) as count FROM users WHERE fk_entite = 1;
SELECT 'Nombre d\'opérations entité 1:' as info, COUNT(*) as count FROM operations WHERE fk_entite = 1;

SELECT '' as '';
SELECT '========================================' as '';
SELECT '=== TABLES x_* (doivent être remplies) ===' as '';
SELECT '========================================' as '';

SELECT 'x_devises' as table_name, COUNT(*) as count FROM x_devises
UNION ALL SELECT 'x_pays', COUNT(*) FROM x_pays
UNION ALL SELECT 'x_regions', COUNT(*) FROM x_regions
UNION ALL SELECT 'x_departements', COUNT(*) FROM x_departements
UNION ALL SELECT 'x_villes', COUNT(*) FROM x_villes
UNION ALL SELECT 'x_departements_contours', COUNT(*) FROM x_departements_contours
UNION ALL SELECT 'x_entites_types', COUNT(*) FROM x_entites_types
UNION ALL SELECT 'x_types_passages', COUNT(*) FROM x_types_passages
UNION ALL SELECT 'x_types_reglements', COUNT(*) FROM x_types_reglements
UNION ALL SELECT 'x_users_roles', COUNT(*) FROM x_users_roles
UNION ALL SELECT 'x_users_titres', COUNT(*) FROM x_users_titres;
