package user

import (
	context "context"

	"github.com/tidbcloud/account/thirdparty/jsm"
	"github.com/tidbcloud/tiinf/monitor/tilog"
	"go.uber.org/zap"
)

func noticeToJsmWhenRemoveUserFromOrg(ctx context.Context, userId uint64, orgId uint64) {

	// NOTE(bing.wang): Notify user is removed from org
	jsmClient := jsm.GetJsmClientSingleton()
	if jsmClient.IsEnable() {
		notifyRemoveUserFromOrgErr := jsmClient.NotifyRemoveUserFromOrgMessage(ctx, orgId, userId)
		if notifyRemoveUserFromOrgErr != nil {
			tilog.Error(ctx, "Sync Jsm failed when removing User from org", zap.Uint64("user", userId), zap.Uint64("org", orgId))
		}
	}

}
func noticeToJsmWhenRemoveUserFromProject(ctx context.Context, userId uint64, projectId uint64) {

	// NOTE(bing.wang): Notify user is removed from project
	jsmClient := jsm.GetJsmClientSingleton()
	if jsmClient.IsEnable() {
		notifyRemoveUserFromOrgErr := jsmClient.NotifyRemoveUserFromProjectMessage(ctx, projectId, userId)
		if notifyRemoveUserFromOrgErr != nil {
			tilog.Error(ctx, "Sync Jsm failed when removing User from project", zap.Uint64("user", userId), zap.Uint64("projectId", projectId))
		}
	}

}

func noticeToJsmWhenAddUserToOrg(ctx context.Context, userId, orgId uint64, role string) {

	jsmClient := jsm.GetJsmClientSingleton()
	if jsmClient.IsEnable() {
		notifyRemoveUserFromOrgErr := jsmClient.NotifyInviteUserToOrgMessage(ctx, orgId, userId, role)
		if notifyRemoveUserFromOrgErr != nil {
			tilog.Error(ctx, "Sync Jsm failed when add User to org", zap.Uint64("user", userId), zap.Uint64("org", orgId), zap.Any("role", role))
		}
	}

}

func noticeToJsmWhenAddUserToProject(ctx context.Context, userId, projectId uint64, role string) {

	jsmClient := jsm.GetJsmClientSingleton()
	if jsmClient.IsEnable() {
		notifyRemoveUserFromOrgErr := jsmClient.NotifyInviteUserToProjectMessage(ctx, projectId, userId)
		if notifyRemoveUserFromOrgErr != nil {
			tilog.Error(ctx, "Sync Jsm failed when add User to project", zap.Uint64("user", userId), zap.Uint64("projectId", projectId))
		}
	}

}
