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 ApplyInviteCodeLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

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

func (l *ApplyInviteCodeLogic) ApplyInviteCode(req *types.ApplyInviteCodeReq) (resp *types.MyInviteNetworkData, err error) {
	uid, ok := middleware.UIDFrom(l.ctx)
	if !ok || uid <= 0 {
		return nil, fmt.Errorf("unauthorized")
	}
	net, err := l.svcCtx.Auth.ApplyInviteCode(l.ctx, uid, req.Code)
	if err != nil {
		return nil, err
	}
	return types.MyInviteNetworkFromUC(net), nil
}
