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 TimeSlotHandler struct {
	logName string
}

func NewTimeSlotHandler(logName string) *TimeSlotHandler {
	return &TimeSlotHandler{
		logName: logName,
	}
}

func (TimeSlotHandler *TimeSlotHandler) GetTimeSlotHandler(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.GetTimeSlotInput{}
	if err := c.Bind(&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
	}
	trans,_ := db.Beginx()
	timeslotRepository := repository.TimeSlotRepositoryInit(trans)
	timeslotService := service.TimeSlotServiceInit(timeslotRepository, trans)
	resp = timeslotService.GetTimeSlot(body)
	c.Set("response", resp)

}
