package handler

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

	// "fmt"
	// "io"
	// "log"
	// "net/http"

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

type TechnicianHandler struct {
	logName string
}

func NewTechnicianHandler(logName string) *TechnicianHandler {
	return &TechnicianHandler{
		logName: logName,
	}
}

func (technicianHandler *TechnicianHandler) GetTechnicianHandler(c *gin.Context) {
	db, err := util.GetDBConnection()
	var getTechnicianResp entity.CustomResponse
	if db != nil {
		defer db.Close()
	}
	if err != nil {
		getTechnicianResp.Message = "Problem in Database connection. Please try again"
		getTechnicianResp.Resultcode = 1
		c.Set("response", getTechnicianResp)
		return
	}
	body := entity.GetTechnicianListInput{}
	// using BindJson method to serialize body with struct
	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
	}
	body = util.Default_tag(body).(entity.GetTechnicianListInput)
	//userRepository := repository.UserRepositoryInit(db)
	//notification := repository.NotificationRepositoryInit(db)
	technicianRepository := repository.TechnicianRepositoryInit(db)
	getTechnicianService := service.TechnicianServiceInit(technicianRepository)
	getTechnicianResp = getTechnicianService.GetTechnicianListService(body)
	c.Set("response", getTechnicianResp)
}
func (technicianHandler *TechnicianHandler) GetConfirmationPinHandler(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.GetConfirmationPinInput{}
	if err := c.ShouldBindJSON(&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

	} else {
		if err != nil && err == io.EOF {
			if util.CheckRequired(body) {
				c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Mandatory input fields missing"})
				return
			}
		}
	}

	// tx, _ := db.Beginx()
	technicianRepository := repository.TechnicianRepositoryInit(db)
	technicianService := service.TechnicianServiceInit(technicianRepository)
	response = technicianService.GetConfirmationPin(body)
	c.Set("response", response)
}

func (technicianHandler *TechnicianHandler) GetRoleHandler(c *gin.Context) {
	db, err := util.GetDBConnection()

	var response entity.CustomResponse
	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.GetRoleInput{}
	
	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
	}
	
	technicianRepository := repository.TechnicianRepositoryInit(db)
	getTechnicianService := service.TechnicianServiceInit(technicianRepository)
	response = getTechnicianService.GetRole(body)
	c.Set("response", response)
}


func (technicianHandler *TechnicianHandler) GetAllEmployeeHandler(c *gin.Context) {
	db, err := util.GetDBConnection()

	var response entity.CustomResponse
	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.GetAllEmployeeInput{}
	
	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
	}
	body = util.Default_tag(body).(entity.GetAllEmployeeInput)
	
	techRepository := repository.TechnicianRepositoryInit(db)
	techService := service.TechnicianServiceInit(techRepository)
	response = techService.GetAllEmployee(body)
	c.Set("response", response)
}
