import trainingGroupService from '../services/trainingGroupService.js';
import { getSafeErrorMessage } from '../utils/errorUtils.js';

export const getTrainingGroups = async (req, res) => {
    try {
        const { authcode: userToken } = req.headers;
        const { clubId } = req.params;
        const groups = await trainingGroupService.getTrainingGroups(userToken, clubId);
        res.status(200).json(groups);
    } catch (error) {
        console.error('[getTrainingGroups] - Error:', error);
        const msg = getSafeErrorMessage(error, 'Fehler beim Laden der Trainingsgruppen');
        res.status(error.statusCode || 500).json({ error: msg });
    }
};

export const createTrainingGroup = async (req, res) => {
    try {
        const { authcode: userToken } = req.headers;
        const { clubId } = req.params;
        const { name, sortOrder } = req.body;
        const group = await trainingGroupService.createTrainingGroup(userToken, clubId, name, sortOrder);
        res.status(201).json(group);
    } catch (error) {
        console.error('[createTrainingGroup] - Error:', error);
        const msg = getSafeErrorMessage(error, 'Fehler beim Erstellen der Trainingsgruppe');
        res.status(error.statusCode || 500).json({ error: msg });
    }
};

export const updateTrainingGroup = async (req, res) => {
    try {
        const { authcode: userToken } = req.headers;
        const { clubId, groupId } = req.params;
        const group = await trainingGroupService.updateTrainingGroup(userToken, clubId, groupId, req.body);
        res.status(200).json(group);
    } catch (error) {
        console.error('[updateTrainingGroup] - Error:', error);
        const msg = getSafeErrorMessage(error, 'Fehler beim Aktualisieren der Trainingsgruppe');
        res.status(error.statusCode || 500).json({ error: msg });
    }
};

export const deleteTrainingGroup = async (req, res) => {
    try {
        const { authcode: userToken } = req.headers;
        const { clubId, groupId } = req.params;
        await trainingGroupService.deleteTrainingGroup(userToken, clubId, groupId);
        res.status(200).json({ success: true });
    } catch (error) {
        console.error('[deleteTrainingGroup] - Error:', error);
        const msg = getSafeErrorMessage(error, 'Fehler beim Löschen der Trainingsgruppe');
        res.status(error.statusCode || 500).json({ error: msg });
    }
};

export const addMemberToGroup = async (req, res) => {
    try {
        const { authcode: userToken } = req.headers;
        const { clubId, groupId, memberId } = req.params;
        const memberGroup = await trainingGroupService.addMemberToGroup(userToken, clubId, groupId, memberId);
        res.status(201).json(memberGroup);
    } catch (error) {
        console.error('[addMemberToGroup] - Error:', error);
        const msg = getSafeErrorMessage(error, 'Fehler beim Hinzufügen des Mitglieds zur Gruppe');
        res.status(error.statusCode || 500).json({ error: msg });
    }
};

export const removeMemberFromGroup = async (req, res) => {
    try {
        const { authcode: userToken } = req.headers;
        const { clubId, groupId, memberId } = req.params;
        await trainingGroupService.removeMemberFromGroup(userToken, clubId, groupId, memberId);
        res.status(200).json({ success: true });
    } catch (error) {
        console.error('[removeMemberFromGroup] - Error:', error);
        const msg = getSafeErrorMessage(error, 'Fehler beim Entfernen des Mitglieds aus der Gruppe');
        res.status(error.statusCode || 500).json({ error: msg });
    }
};

export const getMemberGroups = async (req, res) => {
    try {
        const { authcode: userToken } = req.headers;
        const { clubId, memberId } = req.params;
        const groups = await trainingGroupService.getMemberGroups(userToken, clubId, memberId);
        res.status(200).json(groups);
    } catch (error) {
        console.error('[getMemberGroups] - Error:', error);
        const msg = getSafeErrorMessage(error, 'Fehler beim Laden der Gruppen des Mitglieds');
        res.status(error.statusCode || 500).json({ error: msg });
    }
};

export const ensurePresetGroups = async (req, res) => {
    try {
        const { authcode: userToken } = req.headers;
        const { clubId } = req.params;
        const groups = await trainingGroupService.ensurePresetGroups(userToken, clubId);
        res.status(200).json({ 
            message: 'Preset-Gruppen wurden erstellt/überprüft',
            groups: groups.length 
        });
    } catch (error) {
        console.error('[ensurePresetGroups] - Error:', error);
        const msg = getSafeErrorMessage(error, 'Fehler beim Erstellen der Preset-Gruppen');
        res.status(error.statusCode || 500).json({ error: msg });
    }
};

export const enablePresetGroup = async (req, res) => {
    try {
        const { authcode: userToken } = req.headers;
        const { clubId, presetType } = req.params;
        const group = await trainingGroupService.enablePresetGroup(userToken, clubId, presetType);
        res.status(200).json({ 
            message: 'Preset-Gruppe wurde aktiviert',
            group 
        });
    } catch (error) {
        console.error('[enablePresetGroup] - Error:', error);
        const msg = getSafeErrorMessage(error, 'Fehler beim Aktivieren der Preset-Gruppe');
        res.status(error.statusCode || 500).json({ error: msg });
    }
};

