package types

import memberUC "apps/backend/internal/module/member/usecase"

func InviteBriefFromUC(b memberUC.InviteMemberBrief) InviteMemberBrief {
	out := InviteMemberBrief{
		Uid: b.UID, Email: b.Email, DisplayName: b.DisplayName, InviteCode: b.InviteCode,
		Roles: b.Roles, Status: b.Status, DownlineCount: b.DownlineCount,
		TotalDownlineCount: b.TotalDownlineCount, Depth: b.Depth, JoinedAt: b.JoinedAt,
		AvatarUrl: b.AvatarURL,
	}
	if b.ParentUID != nil {
		out.ParentUid = *b.ParentUID
	}
	return out
}

func InviteTreeFromUC(n memberUC.InviteTreeNode) InviteTreeNode {
	out := InviteTreeNode{
		Uid: n.UID, Email: n.Email, DisplayName: n.DisplayName, InviteCode: n.InviteCode,
		Roles: n.Roles, Status: n.Status, DownlineCount: n.DownlineCount,
		TotalDownlineCount: n.TotalDownlineCount, Depth: n.Depth, JoinedAt: n.JoinedAt,
		AvatarUrl: n.AvatarURL, Children: make([]InviteTreeNode, 0, len(n.Children)),
	}
	if n.ParentUID != nil {
		out.ParentUid = *n.ParentUID
	}
	for _, c := range n.Children {
		out.Children = append(out.Children, InviteTreeFromUC(c))
	}
	return out
}

func MyInviteNetworkFromUC(n *memberUC.MyInviteNetwork) *MyInviteNetworkData {
	if n == nil {
		return nil
	}
	out := &MyInviteNetworkData{
		Me:                 InviteBriefFromUC(n.Me),
		Downlines:          make([]InviteMemberBrief, 0, len(n.Downlines)),
		TotalDownlineCount: n.TotalDownlineCount,
	}
	if n.Upline != nil {
		u := InviteBriefFromUC(*n.Upline)
		out.Upline = &u
	}
	for _, d := range n.Downlines {
		out.Downlines = append(out.Downlines, InviteBriefFromUC(d))
	}
	return out
}
