import MemberService from "../services/memberService.js";

const getClubMembers = async(req, res) => {
    try {
        const { authcode: userToken } = req.headers;
        const { id: clubId } = req.params;
        console.log('[getClubMembers]', userToken, clubId);
        res.status(200).json(await MemberService.getClubMembers(userToken, clubId));
    } catch(error) {
        console.log('[getClubMembers] - Error: ', error);
        res.status(500).json({ error: 'systemerror' });
    }
}

const getWaitingApprovals = async(req, res) => {
    try {
        console.log('[getWaitingApprovals] - Start');
        const { id: clubId } = req.params;
        console.log('[getWaitingApprovals] - get token');
        const { authcode: userToken } = req.headers;
        console.log('[getWaitingApprovals] - load for waiting approvals');
        const waitingApprovals = await MemberService.getApprovalRequests(userToken, clubId);
        console.log('[getWaitingApprovals] - set response');
        res.status(200).json(waitingApprovals);
        console.log('[getWaitingApprovals] - done');
    } catch(error) {
        console.log('[getWaitingApprovals] - Error: ', error);
        res.status(403).json({ error: error });
    }
} 

const setClubMembers = async(req, res) => {
    const { id: memberId, firstname: firstName, lastname: lastName, street, city, birthdate, phone, email} = req.body;
    const { id: clubId } = req.params;
    const { authcode: userToken } = req.headers;
    const addResult = await MemberService.setClubMember(userToken, clubId, memberId, firstName, lastName, street, city, birthdate, phone, email);
    res.status(addResult.status || 500).json(addResult.response);
}

export { getClubMembers, getWaitingApprovals, setClubMembers };