package handler

import (
	"bpmsapi/entity"
	"bpmsapi/repository"
	"bpmsapi/service"
	"bpmsapi/util"
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
)

type PaymentHandler struct {
	logName string
}

func NewPaymentHandler(logName string) *PaymentHandler {
	return &PaymentHandler{
		logName: logName,
	}
}

func (paymenthandler *PaymentHandler) CreateOnlinePaymentIdHandler(c *gin.Context) {
	var resp entity.CustomResponse
	db, err := util.GetDBConnection()
	if db != nil {
		defer db.Close()
	}

	if err != nil {
		resp.Resultcode = 1
		resp.Message = "Problem in establishing database connection"
		c.Set("response", resp)
		return
	}

	body := entity.CreateOnlinePaymentIdInput{}
	if err := c.ShouldBindJSON(&body); err != nil {
		switch err.(type) {
		case validator.ValidationErrors:
			c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
				"error": err.(validator.ValidationErrors).Translate(util.Trans),
			})
		default:
			c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
				"error": err.Error()})

		}

		return
	}

	data := map[string]interface{}{
		"amount":          body.Total_Amount,
		"currency":        "INR",
		"receipt":         body.ReceiptNo,
		"payment_capture": 1,
	}
	tx, _ := db.Beginx()
	paymentRepository := repository.PaymentRepositoryInit(tx)
	paymentService := service.PaymentServiceInit(paymentRepository, tx)

	resp = paymentService.GetPaymentId(body, data)

	c.Set("response", resp)
}
