import {Type, User, CorporateUser, AgentUser, Group} from "@/interfaces/user";

export const USER_TYPE_LABELS: {[key in Type]: string} = {
	student: "Student",
	teacher: "Teacher",
	corporate: "Corporate",
	agent: "Country Manager",
	admin: "Super Admin",
	developer: "Developer",
	mastercorporate: "Master Corporate",
};

export function isCorporateUser(user: User): user is CorporateUser {
	return (user as CorporateUser)?.corporateInformation !== undefined;
}

export function isAgentUser(user: User): user is AgentUser {
	return (user as AgentUser)?.agentInformation !== undefined;
}

export function getUserCompanyName(user: User, users: User[], groups: Group[]) {
	if (isCorporateUser(user)) return user.corporateInformation?.companyInformation?.name || user.name;
	if (isAgentUser(user)) return user.agentInformation?.companyName || user.name;

	const belongingGroups = groups.filter((x) => x.participants.includes(user?.id));
	const belongingGroupsAdmins = belongingGroups.map((x) => users.find((u) => u.id === x.admin)).filter((x) => !!x && isCorporateUser(x));

	if (belongingGroupsAdmins.length === 0) return "";

	const admin = belongingGroupsAdmins[0] as CorporateUser;
	return admin.corporateInformation?.companyInformation.name || admin.name;
}

export function getCorporateUser(user: User, users: User[], groups: Group[]) {
	if (isCorporateUser(user)) return user;

	const belongingGroups = groups.filter((x) => x.participants.includes(user.id));
	const belongingGroupsAdmins = belongingGroups.map((x) => users.find((u) => u.id === x.admin)).filter((x) => !!x && isCorporateUser(x));

	if (belongingGroupsAdmins.length === 0) return undefined;

	const admin = belongingGroupsAdmins[0] as CorporateUser;
	return admin;
}
