package handler

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

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

type ProfileHandler struct {
	logName string
}

func NewProfiletHandler(logName string) *ProfileHandler {
	return &ProfileHandler{
		logName: logName,
	}
}

func (profilehandler *ProfileHandler) UpdateEmployeeProfileHandler(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.UpdateEmployeeProfileInput{}
	if err := c.ShouldBind(&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
			}
		}
	}

	body = util.Default_tag(body).(entity.UpdateEmployeeProfileInput)
	var img_path, img_path_save string
	if body.Photo != nil {
		file, err1 := c.FormFile("photo")

		if err1 != nil {
			c.AbortWithStatusJSON(http.StatusBadRequest, err1)
			return
		}
		if file == nil {
			errMsg := "No file uploaded for Photo"
			c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": errMsg})
			return
		}
		timestamp := time.Now().Unix()
		img_name := fmt.Sprintf("%d%s", timestamp, file.Filename)
		// img_path_save = fmt.Sprintf("image/%s", img_name)
		img_path = fmt.Sprintf("http://151.106.120.127/bashyam/assets/Profile/%s", img_name)
		img_path_save = fmt.Sprintf("./var/www/html/bashyam/assets/Profile/%s", img_name)

		if err2 := c.SaveUploadedFile(file, img_path_save); err != nil {
			c.AbortWithStatusJSON(http.StatusBadRequest, err2)
			return
		}
	}
	tx, _ := db.Beginx()
	profileRepository := repository.ProfileRepositoryInit(tx)
	profileService := service.ProfileServiceInit(profileRepository, tx)
	response = profileService.UpdateEmployeeProfile(body, img_path)
	c.Set("response", response)
}

func (profilehandler *ProfileHandler) EmployeeApartmentConfigHandler(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.EmpApartmentConfigInput{}

	if err := c.ShouldBind(&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()
	profileRepository := repository.ProfileRepositoryInit(tx)
	profileService := service.ProfileServiceInit(profileRepository, tx)
	response = profileService.EmpApartmentConfig(body)
	c.Set("response", response)

}
func (profilehandler *ProfileHandler) CreateLeaveHandler(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.CreateLeaveInput{}

	if err := c.ShouldBind(&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()
	profileRepository := repository.ProfileRepositoryInit(tx)
	profileService := service.ProfileServiceInit(profileRepository, tx)
	response = profileService.CreateLeave(body)
	c.Set("response", response)

}

func (profilehandler *ProfileHandler) CancelLeaveHandler(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.CancelLeaveInput{}

	if err := c.ShouldBind(&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()
	profileRepository := repository.ProfileRepositoryInit(tx)
	profileService := service.ProfileServiceInit(profileRepository, tx)
	response = profileService.CancelLeave(body)
	c.Set("response", response)

}

func (profilehandler *ProfileHandler) DeleteEmployeeHandler(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.DeleteEmployeeInput{}
	fmt.Println("e", body.Role)

	if err := c.ShouldBind(&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()
	profileRepository := repository.ProfileRepositoryInit(tx)
	profileService := service.ProfileServiceInit(profileRepository, tx)
	response = profileService.DeleteEmployee(body)
	c.Set("response", response)
}
