import diaryDateActivityService from '../services/diaryDateActivityService.js';

export const createDiaryDateActivity = async (req, res) => {
    try {
        const { authcode: userToken } = req.headers;
        const { clubId } = req.params;
        const { diaryDateId, activity, duration, durationText, orderId, isTimeblock } = req.body;
        const activityItem = await diaryDateActivityService.createActivity(userToken, clubId, {
            diaryDateId,
            activity,
            duration,
            durationText,
            orderId,
            isTimeblock,
        });
        res.status(201).json(activityItem);
    } catch (error) {
        console.log(error);
        res.status(500).json({ error: 'Error creating activity' });
    }
};

export const updateDiaryDateActivity = async (req, res) => {
    try {
        const { authcode: userToken } = req.headers;
        const { clubId, id } = req.params;
        const { predefinedActivityId, customActivityName, duration, durationText, orderId, groupId } = req.body; // Add groupId
        const updatedActivity = await diaryDateActivityService.updateActivity(userToken, clubId, id, {
            predefinedActivityId,
            customActivityName,
            duration,
            durationText,
            orderId,
            groupId, // Pass groupId to the service
        });
        res.status(200).json(updatedActivity);
    } catch (error) {
        res.status(500).json({ error: 'Error updating activity' });
    }
};

export const deleteDiaryDateActivity = async (req, res) => {
    try {
        const { authcode: userToken } = req.headers;
        const { clubId, id } = req.params;
        await diaryDateActivityService.deleteActivity(userToken, clubId, id);
        res.status(200).json({ message: 'Activity deleted' });
    } catch (error) {
        res.status(500).json({ error: 'Error deleting activity' });
    }
};

export const updateDiaryDateActivityOrder = async (req, res) => {
    try {
        const { authcode: userToken } = req.headers;
        const { clubId, id } = req.params;
        const { orderId } = req.body;
        const updatedActivity = await diaryDateActivityService.updateActivityOrder(userToken, clubId, id, orderId);
        res.status(200).json(updatedActivity);
    } catch (error) {
        console.log(error);
        res.status(500).json({ error: 'Error updating activity order' });
    }
};

export const getDiaryDateActivities = async (req, res) => {
    try {
        const { authcode: userToken } = req.headers;
        const { clubId, diaryDateId } = req.params;
        const activities = await diaryDateActivityService.getActivities(userToken, clubId, diaryDateId);
        res.status(200).json(activities);
    } catch (error) {
        console.log(error);
        res.status(500).json({ error: 'Error getting activities' });
    }
}

export const addGroupActivity = async(req, res) => {
    try {
        const { authcode: userToken } = req.headers;
        const { clubId, diaryDateId, groupId, activity } = req.body;
        const activityItem = await diaryDateActivityService.addGroupActivity(userToken, clubId, diaryDateId, groupId, activity);
        res.status(201).json(activityItem);        
    } catch (error) {
        console.log(error);
        res.status(500).json({ error: 'Error adding group activity' });
    }
}