package service

import (
	"bpmsapi/dto"
	"bpmsapi/entity"
	"bpmsapi/repository"

	"github.com/jmoiron/sqlx"
)

type Paymentusecase interface {
	GetPaymentId(input entity.CreateOnlinePaymentIdInput) entity.CustomResponse
}

type PaymentService struct {
	paymentRepository *repository.PaymentRepository
	tx                *sqlx.Tx
}

func PaymentServiceInit(paymentRepository *repository.PaymentRepository, tx *sqlx.Tx) *PaymentService {
	return &PaymentService{
		paymentRepository: paymentRepository,
		tx:                tx,
	}
}

func (paymentServ *PaymentService) GetPaymentId(input entity.CreateOnlinePaymentIdInput,data map[string]interface{}) (response entity.CustomResponse) {
	resp, err := paymentServ.paymentRepository.GetOnlinePaymentId(input,data)
	if err != nil {
		paymentServ.tx.Rollback()
		response.Resultcode = 1
		response.Message = dto.ErrorOnlinePaymentId + err.Error()
	} else {
		commitError := paymentServ.tx.Commit()
		if commitError != nil {
			response.Resultcode = 1
			response.Message = dto.ErrorOnlinePaymentId + err.Error()
		} else {

			response.Resultcode = 0
			response.Message = dto.SuccessOnlinePaymentId
			response.Payload = resp
		}
	}

	return response
}