package billing

import (
	"context"

	"apps/backend/internal/middleware"
	billingModule "apps/backend/internal/module/billing"
	"apps/backend/internal/svc"
	"apps/backend/internal/types"

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

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

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

func (l *CreateBillingPortalSessionLogic) CreateBillingPortalSession() (resp *types.BillingPortalData, err error) {
	uid, ok := middleware.UIDFrom(l.ctx)
	if !ok {
		return nil, billingModule.ErrInvalidRequest
	}
	url, err := l.svcCtx.Billing.Portal(l.ctx, uid)
	if err != nil {
		return nil, err
	}
	return &types.BillingPortalData{Url: url}, nil
}
