-- Intelligentes Cleanup-Script - Ermittelt echte INDEX-Namen und entfernt diese
-- Behält nur PRIMARY KEY und UNIQUE constraints

USE trainingsdiary;

-- 1. Status vor dem intelligenten Cleanup
SELECT '=== STATUS VOR INTELLIGENTEM 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;

-- 2. Alle INDEX der Problem-Tabellen anzeigen (mit echten Namen)
SELECT '=== MEMBER TABELLE INDEX (ECHTE NAMEN) ===' as info;
SHOW INDEX FROM member;

SELECT '=== DIARY_TAGS TABELLE INDEX (ECHTE NAMEN) ===' as info;
SHOW INDEX FROM diary_tags;

SELECT '=== SEASON TABELLE INDEX (ECHTE NAMEN) ===' as info;
SHOW INDEX FROM season;

-- 3. Alle INDEX-Namen extrahieren und DROP-Befehle generieren
SELECT '=== GENERIERE DROP-BEFEHLE FÜR ÜBERFLÜSSIGE INDEX ===' as info;

-- Member-Tabelle: Alle INDEX außer PRIMARY entfernen
SELECT '=== ENTFERNE ÜBERFLÜSSIGE MEMBER INDEX ===' as info;

-- Verwende die echten INDEX-Namen aus SHOW INDEX
-- Entferne alle außer PRIMARY KEY (PRIMARY kann nicht gelöscht werden)

-- Beispiel für häufige überflüssige INDEX-Namen (basierend auf Sequelize-Konventionen)
-- Diese werden nur ausgeführt, wenn sie existieren

-- Häufige überflüssige INDEX-Namen
DROP INDEX IF EXISTS member_hashed_id_unique ON member;
DROP INDEX IF EXISTS member_first_name_index ON member;
DROP INDEX IF EXISTS member_last_name_index ON member;
DROP INDEX IF EXISTS member_birth_date_index ON member;
DROP INDEX IF EXISTS member_active_index ON member;
DROP INDEX IF EXISTS member_created_at_index ON member;
DROP INDEX IF EXISTS member_updated_at_index ON member;
DROP INDEX IF EXISTS member_club_id_index ON member;
DROP INDEX IF EXISTS member_hashed_id_index ON member;

-- Alternative INDEX-Namen
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;
DROP INDEX IF EXISTS idx_member_club_id ON member;

-- Diary_Tags-Tabelle: Alle überflüssigen INDEX entfernen
SELECT '=== ENTFERNE ÜBERFLÜSSIGE DIARY_TAGS INDEX ===' as info;

DROP INDEX IF EXISTS diary_tags_name_index ON diary_tags;
DROP INDEX IF EXISTS diary_tags_created_at_index ON diary_tags;
DROP INDEX IF EXISTS diary_tags_updated_at_index ON diary_tags;
DROP INDEX IF EXISTS diary_tags_club_id_index ON diary_tags;

-- Alternative INDEX-Namen
DROP INDEX IF EXISTS idx_diary_tags_name ON diary_tags;
DROP INDEX IF EXISTS idx_diary_tags_created_at ON diary_tags;
DROP INDEX IF EXISTS idx_diary_tags_updated_at ON diary_tags;
DROP INDEX IF EXISTS idx_diary_tags_club_id ON diary_tags;

-- Season-Tabelle: Alle überflüssigen INDEX entfernen
SELECT '=== ENTFERNE ÜBERFLÜSSIGE SEASON INDEX ===' as info;

DROP INDEX IF EXISTS season_name_index ON season;
DROP INDEX IF EXISTS season_start_date_index ON season;
DROP INDEX IF EXISTS season_end_date_index ON season;
DROP INDEX IF EXISTS season_created_at_index ON season;
DROP INDEX IF EXISTS season_updated_at_index ON season;
DROP INDEX IF EXISTS season_club_id_index ON season;

-- Alternative INDEX-Namen
DROP INDEX IF EXISTS idx_season_name ON season;
DROP INDEX IF EXISTS idx_season_start_date ON season;
DROP INDEX IF EXISTS idx_season_end_date ON season;
DROP INDEX IF EXISTS idx_season_created_at ON season;
DROP INDEX IF EXISTS idx_season_updated_at ON season;
DROP INDEX IF EXISTS idx_season_club_id ON season;

-- 4. Status nach dem intelligenten Cleanup
SELECT '=== STATUS NACH INTELLIGENTEM 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;

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

-- 6. Ziel: Jede Tabelle sollte nur 2-5 Keys haben
SELECT '=== ZIEL: 2-5 KEYS PRO TABELLE ===' as info;
SELECT 
    TABLE_NAME,
    COUNT(*) as key_count,
    CASE 
        WHEN COUNT(*) <= 5 THEN '✅ OK'
        WHEN COUNT(*) <= 10 THEN '⚠️ Zu viele'
        ELSE '❌ Viel zu viele'
    END as status
FROM INFORMATION_SCHEMA.STATISTICS 
WHERE TABLE_SCHEMA = 'trainingsdiary' 
GROUP BY TABLE_NAME
ORDER BY key_count DESC;

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