package handler

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

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

type ReportHandler struct {
	logName string
}

func NewReportHandler(logName string) *ReportHandler {
	return &ReportHandler{
		logName: logName,
	}
}

func (reporthandler *ReportHandler) GetOrderSummaryHandler(c *gin.Context) {
	var response entity.CustomResponse

	db, err := util.GetDBConnection()
	if db != nil {
		defer db.Close()
	}
	if err != nil {
		response.Message = "Problem in Database connection. Please try again"
		response.Resultcode = 1
		c.Set("response", response)
		return
	}

	body := entity.OrderSummaryInput{}

	if err := c.Bind(&body); err != nil && err != io.EOF {
		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

	}

	reportRepository := repository.ReportRepositoryInit(db)
	reportService := service.ReportServiceInit(reportRepository)
	response = reportService.GetOrderSummary(body)
	c.Set("response", response)

}

func (reporthandler *ReportHandler) GetTechOrderSummaryHandler(c *gin.Context) {
	var response entity.CustomResponse

	db, err := util.GetDBConnection()
	if db != nil {
		defer db.Close()
	}
	if err != nil {
		response.Message = "Problem in Database connection. Please try again"
		response.Resultcode = 1
		c.Set("response", response)
		return
	}

	body := entity.TechOrderSummaryInput{}

	if err := c.Bind(&body); err != nil && err != io.EOF {
		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

	}

	reportRepository := repository.ReportRepositoryInit(db)
	reportService := service.ReportServiceInit(reportRepository)
	response = reportService.GetTechOrderSummary(body)
	c.Set("response", response)

}
