-- Cleanup-Script mit echten INDEX-Namen
-- Zeigt zuerst alle vorhandenen INDEX an und entfernt dann nur die überflüssigen

USE trainingsdiary;

-- 1. Alle vorhandenen INDEX anzeigen
SELECT '=== VORHANDENE INDEX VOR CLEANUP ===' as info;
SELECT 
    TABLE_NAME,
    INDEX_NAME,
    COLUMN_NAME,
    NON_UNIQUE,
    SEQ_IN_INDEX
FROM INFORMATION_SCHEMA.STATISTICS 
WHERE TABLE_SCHEMA = 'trainingsdiary' 
ORDER BY TABLE_NAME, INDEX_NAME, SEQ_IN_INDEX;

-- 2. Anzahl der Keys pro Tabelle vor Cleanup
SELECT '=== KEYS PRO TABELLE VOR CLEANUP ===' as info;
SELECT 
    TABLE_NAME,
    COUNT(*) as key_count
FROM INFORMATION_SCHEMA.STATISTICS 
WHERE TABLE_SCHEMA = 'trainingsdiary' 
GROUP BY TABLE_NAME
ORDER BY key_count DESC;

-- 3. Gesamtanzahl der Keys vor Cleanup
SELECT '=== GESAMTANZAHL KEYS VOR CLEANUP ===' as info;
SELECT 
    COUNT(*) as total_keys_before
FROM INFORMATION_SCHEMA.STATISTICS 
WHERE TABLE_SCHEMA = 'trainingsdiary';

-- 4. Cleanup: Nur INDEX entfernen, die tatsächlich existieren
-- Verwende DROP INDEX IF EXISTS für alle möglichen INDEX

-- Member-Tabelle
SELECT '=== ENTFERNE MEMBER INDEX ===' as info;
DROP INDEX IF EXISTS idx_member_hashed_id ON member;
DROP INDEX IF EXISTS idx_member_first_name ON member;
DROP INDEX IF EXISTS idx_member_last_name ON member;
DROP INDEX IF EXISTS idx_member_birth_date ON member;
DROP INDEX IF EXISTS idx_member_active ON member;
DROP INDEX IF EXISTS idx_member_created_at ON member;
DROP INDEX IF EXISTS idx_member_updated_at ON member;

-- User-Tabelle
SELECT '=== ENTFERNE USER INDEX ===' as info;
DROP INDEX IF EXISTS idx_user_email ON user;
DROP INDEX IF EXISTS idx_user_created_at ON user;
DROP INDEX IF EXISTS idx_user_updated_at ON user;

-- Clubs-Tabelle
SELECT '=== ENTFERNE CLUBS INDEX ===' as info;
DROP INDEX IF EXISTS idx_clubs_name ON clubs;
DROP INDEX IF EXISTS idx_clubs_created_at ON clubs;
DROP INDEX IF EXISTS idx_clubs_updated_at ON clubs;

-- User_Club-Tabelle
SELECT '=== ENTFERNE USER_CLUB INDEX ===' as info;
DROP INDEX IF EXISTS idx_user_club_approved ON user_club;
DROP INDEX IF EXISTS idx_user_club_created_at ON user_club;
DROP INDEX IF EXISTS idx_user_club_updated_at ON user_club;

-- Log-Tabelle
SELECT '=== ENTFERNE LOG INDEX ===' as info;
DROP INDEX IF EXISTS idx_log_activity ON log;
DROP INDEX IF EXISTS idx_log_created_at ON log;
DROP INDEX IF EXISTS idx_log_updated_at ON log;

-- Diary_Dates-Tabelle
SELECT '=== ENTFERNE DIARY_DATES INDEX ===' as info;
DROP INDEX IF EXISTS idx_diary_dates_date ON diary_dates;
DROP INDEX IF EXISTS idx_diary_dates_created_at ON diary_dates;
DROP INDEX IF EXISTS idx_diary_dates_updated_at ON diary_dates;

-- Participants-Tabelle
SELECT '=== ENTFERNE PARTICIPANTS INDEX ===' as info;
DROP INDEX IF EXISTS idx_participant_created_at ON participants;
DROP INDEX IF EXISTS idx_participant_updated_at ON participants;

-- Activities-Tabelle
SELECT '=== ENTFERNE ACTIVITIES INDEX ===' as info;
DROP INDEX IF EXISTS idx_activity_created_at ON activities;
DROP INDEX IF EXISTS idx_activity_updated_at ON activities;

-- Member_Notes-Tabelle
SELECT '=== ENTFERNE MEMBER_NOTES INDEX ===' as info;
DROP INDEX IF EXISTS idx_member_note_created_at ON member_notes;
DROP INDEX IF EXISTS idx_member_note_updated_at ON member_notes;

-- Diary_Notes-Tabelle
SELECT '=== ENTFERNE DIARY_NOTES INDEX ===' as info;
DROP INDEX IF EXISTS idx_diary_note_created_at ON diary_notes;
DROP INDEX IF EXISTS idx_diary_note_updated_at ON diary_notes;

-- Diary_Tags-Tabelle
SELECT '=== ENTFERNE DIARY_TAGS INDEX ===' as info;
DROP INDEX IF EXISTS idx_diary_tag_created_at ON diary_tags;
DROP INDEX IF EXISTS idx_diary_tag_updated_at ON diary_tags;

-- Member_Diary_Tags-Tabelle
SELECT '=== ENTFERNE MEMBER_DIARY_TAGS INDEX ===' as info;
DROP INDEX IF EXISTS idx_member_diary_tag_created_at ON member_diary_tags;
DROP INDEX IF EXISTS idx_member_diary_tag_updated_at ON member_diary_tags;

-- Diary_Date_Tags-Tabelle
SELECT '=== ENTFERNE DIARY_DATE_TAGS INDEX ===' as info;
DROP INDEX IF EXISTS idx_diary_date_tag_created_at ON diary_date_tags;
DROP INDEX IF EXISTS idx_diary_date_tag_updated_at ON diary_date_tags;

-- Diary_Member_Notes-Tabelle
SELECT '=== ENTFERNE DIARY_MEMBER_NOTES INDEX ===' as info;
DROP INDEX IF EXISTS idx_diary_member_note_created_at ON diary_member_notes;
DROP INDEX IF EXISTS idx_diary_member_note_updated_at ON diary_member_notes;

-- Predefined_Activities-Tabelle
SELECT '=== ENTFERNE PREDEFINED_ACTIVITIES INDEX ===' as info;
DROP INDEX IF EXISTS idx_predefined_activity_created_at ON predefined_activities;
DROP INDEX IF EXISTS idx_predefined_activity_updated_at ON predefined_activities;

-- Diary_Date_Activities-Tabelle
SELECT '=== ENTFERNE DIARY_DATE_ACTIVITIES INDEX ===' as info;
DROP INDEX IF EXISTS idx_diary_date_activity_created_at ON diary_date_activities;
DROP INDEX IF EXISTS idx_diary_date_activity_updated_at ON diary_date_activities;

-- 5. Nach der Bereinigung: Status anzeigen
SELECT '=== STATUS NACH BEREINIGUNG ===' as info;

-- Anzahl der Keys pro Tabelle nach der Bereinigung
SELECT 
    TABLE_NAME,
    COUNT(*) as key_count
FROM INFORMATION_SCHEMA.STATISTICS 
WHERE TABLE_SCHEMA = 'trainingsdiary' 
GROUP BY TABLE_NAME
ORDER BY key_count DESC;

-- Gesamtanzahl der Keys nach der Bereinigung
SELECT 
    COUNT(*) as total_keys_after
FROM INFORMATION_SCHEMA.STATISTICS 
WHERE TABLE_SCHEMA = 'trainingsdiary';

-- 6. Zusammenfassung der Änderungen
SELECT '=== ZUSAMMENFASSUNG ===' as info;
SELECT 
    'Cleanup abgeschlossen. Überprüfen Sie die Anzahl der Keys oben.' as message;
