package member

import (
	"context"

	"haixun-backend/internal/library/authctx"
	app "haixun-backend/internal/library/errors"
	"haixun-backend/internal/library/errors/code"
	"haixun-backend/internal/svc"
	"haixun-backend/internal/types"
)

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

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

func (l *GetMemberMeLogic) GetMemberMe() (*types.MemberMeData, error) {
	actor, ok := authctx.ActorFromContext(l.ctx)
	if !ok {
		return nil, app.For(code.Auth).AuthUnauthorized("missing actor")
	}
	member, err := l.svcCtx.Member.GetByUID(l.ctx, actor.TenantID, actor.UID)
	if err != nil {
		return nil, err
	}
	return toMemberMeData(member), nil
}
