import DiaryMemberService from '../services/diaryMemberService.js';

import { devLog } from '../utils/logger.js';
const getMemberTags = async (req, res) => {
    try {
        const { diaryDateId, memberId } = req.query;
        const { clubId } = req.params;
        const { authcode: userToken } = req.headers;
        devLog(diaryDateId, memberId, clubId);
        const tags = await DiaryMemberService.getTagsForMemberAndDate(userToken, clubId, diaryDateId, memberId);
        res.status(200).json(tags);
    } catch (error) {
        console.error('[getMemberTags] - Error: ', error);
        res.status(500).json({ error: 'systemerror' });
    }
};

const getMemberNotes = async (req, res) => {
    try {
        const { diaryDateId, memberId } = req.query;
        const { clubId } = req.params;
        const { authcode: userToken } = req.headers;
        devLog('---------->', userToken, clubId);
        const notes = await DiaryMemberService.getNotesForMember(userToken, clubId, diaryDateId, memberId);
        res.status(200).json(notes);
    } catch (error) {
        console.error('[getMemberNotes] - Error: ', error);
        res.status(500).json({ error: 'systemerror' });
    }
};

const addMemberNote = async (req, res) => {
    try {
        const { authcode: userToken } = req.headers;
        const { memberId, diaryDateId, content } = req.body;
        const { clubId } = req.params;
        await DiaryMemberService.addNoteToMember(userToken, clubId, diaryDateId, memberId, content);
        const notes = await DiaryMemberService.getNotesForMember(userToken, clubId, diaryDateId, memberId);
        res.status(201).json(notes);
    } catch (error) {
        console.error('[addMemberNote] - Error: ', error);
        res.status(500).json({ error: 'systemerror' });
    }
};

const addMemberTag = async (req, res) => {
    try {
        const { diaryDateId, memberId, tagId } = req.body;
        const { authcode: userToken } = req.headers;
        const { clubId } = req.params;
        await DiaryMemberService.addTagToMemberAndDate(userToken, clubId, diaryDateId, memberId, tagId);
        const tags = await DiaryMemberService.getTagsForMemberAndDate(userToken, clubId, diaryDateId, memberId);
        res.status(201).json(tags);
    } catch (error) {
        console.error('[addMemberTag] - Error: ', error);
        res.status(500).json({ error: 'systemerror' });
    }
};

const removeMemberNote = async (req, res) => {
    try {
        const { authcode: userToken } = req.headers;
        const { clubId, noteId } = req.params;
        const { diaryDateId, memberId } = req.query;
        if (!diaryDateId || !memberId) {
            return res.status(400).json({ error: 'diaryDateId and memberId query parameters are required' });
        }
        await DiaryMemberService.removeNoteFromMember(userToken, clubId, noteId);
        const notes = await DiaryMemberService.getNotesForMember(userToken, clubId, diaryDateId, memberId);
        res.status(200).json(notes);
    } catch (error) {
        console.error('[removeMemberNote] - Error: ', error.message);
        res.status(400).json({ error: error.message });
    }
};

const removeMemberTag = async (req, res) => {
    try {
        const { diaryDateId, memberId, tagId } = req.body;
        const { authcode: userToken } = req.headers;
        await DiaryMemberService.removeTagFromMemberAndDate(userToken, req.params.clubId, diaryDateId, memberId, { id: tagId });
        const tags = await DiaryMemberService.getTagsForMemberAndDate(userToken, req.params.clubId, diaryDateId, memberId);
        res.status(200).json(tags);
    } catch (error) {
        console.error('[removeMemberTag] - Error: ', error.message);
        res.status(400).json({ error: error.message });
    }
};

export { getMemberTags, getMemberNotes, addMemberNote, addMemberTag, removeMemberNote, removeMemberTag };
