import MemberNoteService from "../services/memberNoteService.js";

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

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

const deleteMemberNote = async (req, res) => {
    try {
        const { authcode: userToken } = req.headers;
        const { noteId } = req.params;
        const { clubId } = req.body;
        console.log('[deleteMemberNote]', userToken, noteId, clubId);
        const memberId = await MemberNoteService.getMemberIdForNote(noteId); // Member ID ermitteln
        await MemberNoteService.deleteNoteForMember(userToken, clubId, noteId);
        const notes = await MemberNoteService.getNotesForMember(userToken, clubId, memberId);
        res.status(200).json(notes);
    } catch (error) {
        console.log('[deleteMemberNote] - Error: ', error);
        res.status(500).json({ error: 'systemerror' });
    }
};

export { getMemberNotes, addMemberNote, deleteMemberNote };
