import MemberNoteService from "../services/memberNoteService.js";
import MemberNote from '../models/MemberNote.js';

import { devLog } from '../utils/logger.js';
const getMemberNotes = async (req, res) => {
    try {
        const { authcode: userToken } = req.headers;
        const { memberId } = req.params;
        const { clubId } = req.query;
        const notes = await MemberNoteService.getNotesForMember(userToken, clubId, memberId);
        res.status(200).json(notes);
    } catch (error) {
        res.status(500).json({ error: 'systemerror' });
    }
};

const addMemberNote = async (req, res) => {
    try {
        const { authcode: userToken } = req.headers;
        const { memberId, content, clubId } = req.body;
        await MemberNoteService.addNoteToMember(userToken, clubId, memberId, content);
        const notes = await MemberNoteService.getNotesForMember(userToken, clubId, memberId);
        res.status(201).json(notes);
    } catch (error) {
        console.error('[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;
        const note = await MemberNote.findByPk(noteId);
        if (!note) {
            return res.status(404).json({ error: 'notfound' });
        }
        const memberId = note.memberId;
        await MemberNoteService.deleteNoteForMember(userToken, clubId, noteId);
        const notes = await MemberNoteService.getNotesForMember(userToken, clubId, memberId);
        res.status(200).json(notes);
    } catch (error) {
        console.error('[deleteMemberNote] - Error:', error);
        res.status(500).json({ error: 'systemerror' });
    }
};

export { getMemberNotes, addMemberNote, deleteMemberNote };
