package user_social

import (
	"context"
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/usersocial"
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/utils/hander"
	"git.noahlan.cn/noahlan/ntool-biz/core/nstatus/code"
	"git.noahlan.cn/noahlan/ntool-biz/core/nstatus/msg"

	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/svc"
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core"
)

type UpdateUserSocialLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateUserSocialLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserSocialLogic {
	return &UpdateUserSocialLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateUserSocialLogic) UpdateUserSocial(in *core.UserSocialInfo) (*core.BaseResp, error) {
	update := l.svcCtx.DB.UserSocial.Update().Where(usersocial.IDEQ(in.ID))
	update.SetNotEmptyProvider(in.Provider).
		SetNotEmptyAccessToken(in.AccessToken).
		SetNotEmptyRefreshToken(in.RefreshToken).
		SetNotEmptyUID(in.Uid).
		SetNotEmptyOpenID(in.OpenId).
		SetNotEmptyUnionID(in.UnionId).
		SetNotEmptyKey(in.Key).
		SetExpiresIn(in.ExpiresIn)
	err := update.Exec(l.ctx)
	if err != nil {
		return nil, hander.HandleEntErr(err, in)
	}
	return &core.BaseResp{
		Code: code.StatusOK,
		Msg:  msg.UpdateSuccess,
	}, nil
}
