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

const getClubMembers = async(req, res) => {
    try {
        const { authcode: userToken } = req.headers;
        const { id: clubId, showAll } = req.params;
        if (showAll === null) {
            showAll = false;
        }
        res.status(200).json(await MemberService.getClubMembers(userToken, clubId, showAll));
    } 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) => {
    try {
        const { id: memberId, firstname: firstName, lastname: lastName, street, city, birthdate, phone, email, active, 
            testMembership, picsInInternetAllowed } = 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, active, testMembership, picsInInternetAllowed);
        res.status(addResult.status || 500).json(addResult.response);
    } catch (error) {
        console.error('[setClubMembers] - Error:', error);
        res.status(500).json({ error: 'Failed to upload image' });
    }
}

const uploadMemberImage = async (req, res) => {
    try {
        const { clubId, memberId } = req.params;
        const { authcode: userToken } = req.headers;
        const result = await MemberService.uploadMemberImage(userToken, clubId, memberId, req.file.buffer);
        res.status(result.status).json(result.message ? { message: result.message } : { error: result.error });
    } catch (error) {
        console.error('[uploadMemberImage] - Error:', error);
        res.status(500).json({ error: 'Failed to upload image' });
    }
};

const getMemberImage = async (req, res) => {
    console.log('[getMemberImage]');
    try {
        const { clubId, memberId } = req.params;
        const { authcode: userToken } = req.headers;
        const result = await MemberService.getMemberImage(userToken, clubId, memberId);
        if (result.status === 200) {
            res.sendFile(result.imagePath);
        } else {
            res.status(result.status).json({ error: result.error });
        }
    } catch (error) {
        console.error('[getMemberImage] - Error:', error);
        res.status(500).json({ error: 'Failed to retrieve image' });
    }
};

export { getClubMembers, getWaitingApprovals, setClubMembers, uploadMemberImage, getMemberImage };