syntax = "v1"

// M6 邀請網絡：會員補填碼 + admin 樹／移動（防環）

type (
	InviteMemberBrief {
		Uid                int64    `json:"uid"`
		Email              string   `json:"email"`
		DisplayName        string   `json:"display_name"`
		InviteCode         string   `json:"invite_code"`
		ParentUid          int64    `json:"parent_uid,optional"` // 0 = 無上線
		Roles              []string `json:"roles"`
		Status             string   `json:"status"`
		DownlineCount      int      `json:"downline_count"`
		TotalDownlineCount int      `json:"total_downline_count"`
		Depth              int      `json:"depth"`
		JoinedAt           int64    `json:"joined_at"`
		AvatarUrl          string   `json:"avatar_url,optional"`
	}

	InviteTreeNode {
		Uid                int64            `json:"uid"`
		Email              string           `json:"email"`
		DisplayName        string           `json:"display_name"`
		InviteCode         string           `json:"invite_code"`
		ParentUid          int64            `json:"parent_uid,optional"`
		Roles              []string         `json:"roles"`
		Status             string           `json:"status"`
		DownlineCount      int              `json:"downline_count"`
		TotalDownlineCount int              `json:"total_downline_count"`
		Depth              int              `json:"depth"`
		JoinedAt           int64            `json:"joined_at"`
		AvatarUrl          string           `json:"avatar_url,optional"`
		Children           []InviteTreeNode `json:"children"`
	}

	MyInviteNetworkData {
		Me                 InviteMemberBrief   `json:"me"`
		Upline             *InviteMemberBrief  `json:"upline,optional"`
		Downlines          []InviteMemberBrief `json:"downlines"`
		TotalDownlineCount int                 `json:"total_downline_count"`
	}

	ApplyInviteCodeReq {
		Code string `json:"code"`
	}

	InviteTreeData {
		List []InviteTreeNode `json:"list"`
	}

	MoveInviteMemberReq {
		Uid       int64 `json:"uid"`
		ParentUid int64 `json:"parent_uid,optional"` // 0 = 獨立根
	}

	ListParentCandidatesReq {
		ExcludeSubtreeRootUid int64 `form:"exclude_subtree_root_uid,optional"`
	}

	InviteParentCandidatesData {
		List []InviteMemberBrief `json:"list"`
	}
)

@server (
	group:      invite
	prefix:     /api/v1/invite
	middleware: AuthJWT
)
service gateway {
	@handler GetMyInviteNetwork
	get /me returns (MyInviteNetworkData)

	@handler ApplyInviteCode
	post /apply (ApplyInviteCodeReq) returns (MyInviteNetworkData)
}

@server (
	group:      invite
	prefix:     /api/v1/invite
	middleware: AuthJWT,AdminAuth
)
service gateway {
	@handler GetInviteTree
	get /tree returns (InviteTreeData)

	@handler MoveInviteMember
	post /move (MoveInviteMemberReq) returns (InviteMemberBrief)

	@handler ListInviteParentCandidates
	get /parent-candidates (ListParentCandidatesReq) returns (InviteParentCandidatesData)
}
