-- Cleanup-Script für MySQL Keys
-- Führt dieses Script in der MySQL-Datenbank aus, um überflüssige Keys zu entfernen

USE trainingsdiary;

-- 1. Alle nicht-essentiellen Keys aus der member-Tabelle entfernen
-- (behält nur PRIMARY KEY und UNIQUE Keys für kritische Felder)

-- Überflüssige INDEX entfernen (falls vorhanden)
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;

-- 2. Überflüssige Keys aus anderen Tabellen entfernen
-- User-Tabelle
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  
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
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
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
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
DROP INDEX IF EXISTS idx_participant_created_at ON participants;
DROP INDEX IF EXISTS idx_participant_updated_at ON participants;

-- Activity-Tabelle
DROP INDEX IF EXISTS idx_activity_created_at ON activities;
DROP INDEX IF EXISTS idx_activity_updated_at ON activities;

-- Member_Note-Tabelle
DROP INDEX IF EXISTS idx_member_note_created_at ON member_note;
DROP INDEX IF EXISTS idx_member_note_updated_at ON member_note;

-- Diary_Note-Tabelle
DROP INDEX IF EXISTS idx_diary_note_created_at ON diary_note;
DROP INDEX IF EXISTS idx_diary_note_updated_at ON diary_note;

-- Diary_Tag-Tabelle
DROP INDEX IF EXISTS idx_diary_tag_created_at ON diary_tag;
DROP INDEX IF EXISTS idx_diary_tag_updated_at ON diary_tag;

-- Member_Diary_Tag-Tabelle
DROP INDEX IF EXISTS idx_member_diary_tag_created_at ON member_diary_tag;
DROP INDEX IF EXISTS idx_member_diary_tag_updated_at ON member_diary_tag;

-- Diary_Date_Tag-Tabelle
DROP INDEX IF EXISTS idx_diary_date_tag_created_at ON diary_date_tag;
DROP INDEX IF EXISTS idx_diary_date_tag_updated_at ON diary_date_tag;

-- Diary_Member_Note-Tabelle
DROP INDEX IF EXISTS idx_diary_member_note_created_at ON diary_member_note;
DROP INDEX IF EXISTS idx_diary_member_note_updated_at ON diary_member_note;

-- Predefined_Activity-Tabelle
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_Activity-Tabelle
DROP INDEX IF EXISTS idx_diary_date_activity_created_at ON diary_date_activity;
DROP INDEX IF EXISTS idx_diary_date_activity_updated_at ON diary_date_activity;

-- Match-Tabelle
DROP INDEX IF EXISTS idx_match_created_at ON match;
DROP INDEX IF EXISTS idx_match_updated_at ON match;

-- League-Tabelle
DROP INDEX IF EXISTS idx_league_created_at ON league;
DROP INDEX IF EXISTS idx_league_updated_at ON league;

-- Team-Tabelle
DROP INDEX IF EXISTS idx_team_created_at ON team;
DROP INDEX IF EXISTS idx_team_updated_at ON team;

-- Season-Tabelle
DROP INDEX IF EXISTS idx_season_created_at ON season;
DROP INDEX IF EXISTS idx_season_updated_at ON season;

-- Location-Tabelle
DROP INDEX IF EXISTS idx_location_created_at ON location;
DROP INDEX IF EXISTS idx_location_updated_at ON location;

-- Group-Tabelle
DROP INDEX IF EXISTS idx_group_created_at ON `group`;
DROP INDEX IF EXISTS idx_group_updated_at ON `group`;

-- Group_Activity-Tabelle
DROP INDEX IF EXISTS idx_group_activity_created_at ON group_activity;
DROP INDEX IF EXISTS idx_group_activity_updated_at ON group_activity;

-- Tournament-Tabelle
DROP INDEX IF EXISTS idx_tournament_created_at ON tournament;
DROP INDEX IF EXISTS idx_tournament_updated_at ON tournament;

-- Tournament_Group-Tabelle
DROP INDEX IF EXISTS idx_tournament_group_created_at ON tournament_group;
DROP INDEX IF EXISTS idx_tournament_group_updated_at ON tournament_group;

-- Tournament_Member-Tabelle
DROP INDEX IF EXISTS idx_tournament_member_created_at ON tournament_member;
DROP INDEX IF EXISTS idx_tournament_member_updated_at ON tournament_member;

-- Tournament_Match-Tabelle
DROP INDEX IF EXISTS idx_tournament_match_created_at ON tournament_match;
DROP INDEX IF EXISTS idx_tournament_match_updated_at ON tournament_match;

-- Tournament_Result-Tabelle
DROP INDEX IF EXISTS idx_tournament_result_created_at ON tournament_result;
DROP INDEX IF EXISTS idx_tournament_result_updated_at ON tournament_result;

-- Accident-Tabelle
DROP INDEX IF EXISTS idx_accident_created_at ON accident;
DROP INDEX IF EXISTS idx_accident_updated_at ON accident;

-- User_Token-Tabelle
DROP INDEX IF EXISTS idx_user_token_created_at ON UserToken;
DROP INDEX IF EXISTS idx_user_token_updated_at ON UserToken;

-- 3. Nur essentiellste Keys beibehalten
-- Diese Keys sind für die Funktionalität notwendig

-- Member-Tabelle: Nur PRIMARY KEY und UNIQUE für hashed_id
-- (wird automatisch von MySQL verwaltet)

-- User-Tabelle: Nur PRIMARY KEY und UNIQUE für email
-- (wird automatisch von MySQL verwaltet)

-- Clubs-Tabelle: Nur PRIMARY KEY und UNIQUE für name
-- (wird automatisch von MySQL verwaltet)

-- User_Club-Tabelle: Nur PRIMARY KEY
-- (wird automatisch von MySQL verwaltet)

-- Log-Tabelle: Nur PRIMARY KEY
-- (wird automatisch von MySQL verwaltet)

-- Diary_Dates-Tabelle: Nur PRIMARY KEY
-- (wird automatisch von MySQL verwaltet)

-- Participant-Tabelle: Nur PRIMARY KEY
-- (wird automatisch von MySQL verwaltet)

-- Alle anderen Tabellen: Nur PRIMARY KEY
-- (wird automatisch von MySQL verwaltet)

-- 4. Status anzeigen
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;

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