package billing

import (
	"context"

	"apps/backend/internal/middleware"
	billingModule "apps/backend/internal/module/billing"
	"apps/backend/internal/svc"
	"github.com/zeromicro/go-zero/core/logx"
)

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

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

func (l *HandleStripeWebhookLogic) HandleStripeWebhook() error {
	payload, signature, ok := middleware.StripeWebhookPayload(l.ctx)
	if !ok {
		return billingModule.ErrInvalidSignature
	}
	return l.svcCtx.Billing.HandleWebhook(l.ctx, payload, signature)
}
