package invite

import (
	"context"
	"fmt"

	"apps/backend/internal/middleware"
	"apps/backend/internal/svc"
	"apps/backend/internal/types"

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

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

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

func (l *MoveInviteMemberLogic) MoveInviteMember(req *types.MoveInviteMemberReq) (resp *types.InviteMemberBrief, err error) {
	actor, ok := middleware.UIDFrom(l.ctx)
	if !ok || actor <= 0 {
		return nil, fmt.Errorf("unauthorized")
	}
	var parent *int64
	if req.ParentUid > 0 {
		p := req.ParentUid
		parent = &p
	}
	b, err := l.svcCtx.Auth.MoveInviteMember(l.ctx, actor, req.Uid, parent)
	if err != nil {
		return nil, err
	}
	out := types.InviteBriefFromUC(*b)
	return &out, nil
}
