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

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

func (l *GetBillingCheckoutSessionLogic) GetBillingCheckoutSession(req *types.BillingCheckoutGetReq) (resp *types.BillingCheckoutData, err error) {
	uid, ok := middleware.UIDFrom(l.ctx)
	if !ok {
		return nil, billingModule.ErrInvalidRequest
	}
	a, err := l.svcCtx.Billing.GetCheckout(l.ctx, uid, req.Id)
	if err != nil {
		return nil, err
	}
	return checkoutData(a), nil
}
