package notify

import (
	"encoding/json"
	"io"

	"github.com/gin-gonic/gin"
	"github.com/perfect-panel/server/internal/logic/notify"
	"github.com/perfect-panel/server/internal/svc"
	"github.com/perfect-panel/server/pkg/result"
)

func AppleIAPNotifyHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) {
	return func(c *gin.Context) {
		raw, _ := io.ReadAll(c.Request.Body)
		var body map[string]interface{}
		_ = json.Unmarshal(raw, &body)
		sp, _ := body["signedPayload"].(string)
		l := notify.NewAppleIAPNotifyLogic(c.Request.Context(), svcCtx)
		err := l.Handle(sp)
		result.HttpResult(c, map[string]bool{"success": err == nil}, err)
	}
}
