import HttpError from '../exceptions/HttpError.js';
import groupService from '../services/groupService.js';
import { emitActivityChanged, emitGroupChanged } from '../services/socketService.js';
import DiaryDate from '../models/DiaryDates.js';

import { devLog } from '../utils/logger.js';
const addGroup = async(req, res) => {
    try {
        const { authcode: userToken } = req.headers;
        const { clubid: clubId, dateid: dateId, name, lead } = req.body;
        const result = await groupService.addGroup(userToken, clubId, dateId, name, lead);
        
        // Emit Socket-Event für Gruppen-Änderungen
        if (dateId) {
            const diaryDate = await DiaryDate.findByPk(dateId);
            if (diaryDate?.clubId) {
                emitGroupChanged(diaryDate.clubId, dateId);
            }
        }
        
        res.status(201).json(result);
    } catch (error) {
        console.error('[addGroup] - Error:', error);
        devLog(req.params, req.headers, req.body)
        res.status(error.statusCode || 500).json({ error: error.message });
    }
}

const getGroups = async(req, res) => {  
    try {
        const { clubId, dateId } = req.params;
        const { authcode: userToken } = req.headers;
        const result = await groupService.getGroups(userToken, clubId, dateId);
        res.status(200).json(result);        
    } catch (error) {
        console.error('[getGroups] - Error:', error);
        res.status(error.statusCode || 500).json({ error: error.message }); 
    }
}

const changeGroup = async(req, res) => {
    try {
        const { authcode: userToken } = req.headers;
        const { groupId } = req.params;
        const { clubid: clubId, dateid: dateId, name, lead } = req.body;
        const result = await groupService.changeGroup(userToken, groupId, clubId, dateId, name, lead);
        
        // Emit Socket-Event für Gruppen-Änderungen
        if (dateId) {
            const diaryDate = await DiaryDate.findByPk(dateId);
            if (diaryDate?.clubId) {
                emitGroupChanged(diaryDate.clubId, dateId);
            }
        }
        
        res.status(200).json(result);
    } catch (error) {
        console.error('[changeGroup] - Error:', error);
        res.status(error.statusCode || 500).json({ error: error.message });
    }
}

const deleteGroup = async(req, res) => {
    try {
        const { authcode: userToken } = req.headers;
        const { groupId } = req.params;
        const { clubid: clubId, dateid: dateId } = req.body;
        const result = await groupService.deleteGroup(userToken, groupId, clubId, dateId);
        
        // Emit Socket-Events für Gruppen- und Aktivitäts-Änderungen (Gruppen werden in Aktivitäten verwendet)
        if (dateId) {
            const diaryDate = await DiaryDate.findByPk(dateId);
            if (diaryDate?.clubId) {
                emitGroupChanged(diaryDate.clubId, dateId);
                emitActivityChanged(diaryDate.clubId, dateId);
            }
        }
        
        res.status(200).json(result);
    } catch (error) {
        console.error('[deleteGroup] - Error:', error);
        res.status(error.statusCode || 500).json({ error: error.message });
    }
}

export { addGroup, getGroups, changeGroup, deleteGroup};