// Code scaffolded by goctl. Safe to edit.
package member

import (
	"context"

	domusecase "gateway/internal/model/member/domain/usecase"
	"gateway/internal/svc"
	"gateway/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateMemberMeLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateMemberMeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateMemberMeLogic {
	return &UpdateMemberMeLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
}

func (l *UpdateMemberMeLogic) UpdateMemberMe(req *types.UpdateMemberMeReq) (*types.MemberMeData, error) {
	actor, err := actorOrErr(l.ctx)
	if err != nil {
		return nil, err
	}
	if l.svcCtx.MemberProfile == nil {
		return nil, errb.SysNotImplemented("member profile not configured")
	}
	update := &domusecase.UpdateMemberRequest{TenantID: actor.TenantID, UID: actor.UID}
	if req != nil {
		if req.DisplayName != "" {
			update.DisplayName = &req.DisplayName
		}
		if req.Avatar != "" {
			update.Avatar = &req.Avatar
		}
		if req.Language != "" {
			update.Language = &req.Language
		}
		if req.Currency != "" {
			update.Currency = &req.Currency
		}
		if req.Phone != "" {
			update.Phone = &req.Phone
		}
	}
	dto, err := l.svcCtx.MemberProfile.Update(l.ctx, update)
	if err != nil {
		return nil, err
	}
	return memberDTOToTypes(dto), nil
}
