import UserClub from "../models/UserClub.js";
import { checkAccess } from "../utils/userUtils.js";
import { getUserByToken } from "../utils/userUtils.js";
import HttpError from "../exceptions/HttpError.js";
import Member from "../models/Member.js";
import { response } from "express";

class MemberService {
    async getApprovalRequests(userToken, clubId) {
        console.log('[MemberService::getApprovalRequest] - Check user access');
        await checkAccess(userToken, clubId);
        console.log('[MemberService::getApprovalRequest] - Load user');
        const user = await getUserByToken(userToken);
        console.log('[MemberService::getApprovalRequest] - Load userclub');
        return await UserClub.findAll({
            where: {
                clubId: clubId,
                approved: false,
                userId: user.id 
            }
        });
    }

    async getClubMembers(userToken, clubId) {
        console.log('[getClubMembers] - Check access');
        await checkAccess(userToken, clubId);
        console.log('[getClubMembers] - Find members');
        const members = await Member.findAll({ where: { clubId: clubId }});
        console.log('[getClubMembers] - return members');
        return members;
    }

    async setClubMember(userToken, clubId, memberId, firstName, lastName, street, city, birthdate, phone, email) {
        try {
            console.log('[setClubMembers] - Check access');
            await checkAccess(userToken, clubId);
            console.log('[setClubMembers] - set default member');
            let member = null;
            console.log('[setClubMembers] - load member if possible');
            if (memberId) {
                member = await Member.findOne({ where: { id: memberId }});
            }
            console.log('[setClubMembers] - set member');
            if (member) {
                member.firstName = firstName;
                member.lastName = lastName;
                member.street = street;
                member.city = city;
                member.birthDate = birthdate;
                member.phone = phone;
                member.email = email;
                await member.save();
            } else {
                await Member.create({
                    firstName: firstName,
                    lastName: lastName,
                    street: street,
                    city: city,
                    birthDate: birthdate,
                    phone: phone,
                    email: email,
                    clubId: clubId,
                });
            }
            console.log('[setClubMembers] - load club members');
            const members = await this.getClubMembers(userToken, clubId);
            console.log('[setClubMembers] - return response');
            return {
                status: 200,
                response: members,
            }
        } catch (error) {
            console.log(error);
            return {
                status: error.statusCode || 500,
                response: { error: "nocreation" }
            }
        }
    }
}

export default new MemberService();