package handler

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

	"fmt"
	"log"
	"time"

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

type SuperAdminHandler struct {
	logName string
}

func NewSuperAdminHandler(logName string) *SuperAdminHandler {
	return &SuperAdminHandler{
		logName: logName,
	}
}
func (superAdminHandler *SuperAdminHandler) UpdateApartmentParamHandler(c *gin.Context) {
	var response entity.CustomResponse
	db, err := util.GetDBConnection()

	if db != nil {
		defer db.Close()
	}
	if err != nil {
		response.Message = "Problem in establishing database connection " + err.Error()
		response.Resultcode = 1
		c.Set("response", response)
		return
	}
	body := entity.UpdateApartmentParamInput{}

	if err := c.ShouldBind(&body); err != nil && err != io.EOF {
		log.Println(err.Error())
		fmt.Print("23", err.Error())
		syntaxErr := 0
		switch err.(type) {
		case validator.ValidationErrors:
			c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
				"error": err.(validator.ValidationErrors).Translate(util.Trans),
			})
		default:
			if jsonError, ok := err.(*json.SyntaxError); ok {
				// Check if it's the specific JSON syntax error you want to handle
				if jsonError.Error() == "unexpected end of JSON input" {
					syntaxErr = 1
				}
			} else {
				c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
			}
		}
		if syntaxErr == 0 {
			return
		}
	} else {
		if err != nil && err == io.EOF {
			if util.CheckRequired(body) {
				c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Mandatory input fields missing"})
				return
			}
		}
	}
	//news image
	var image_path, img_path string
	if body.News_Image != nil {
		file, err1 := c.FormFile("news_img")
		if err1 != nil {
			c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err1.Error()})
			fmt.Println("1", err1)
			return
		}
		if file.Size == 0 {
			errMsg := "No image uploaded"
			c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": errMsg})
			return
		}

		timestamp := time.Now().Unix()
		imageName := fmt.Sprintf("%d%s", timestamp, file.Filename)

		image_path = fmt.Sprintf("./var/www/html/bashyam/assets/apartment_image/%s", imageName)
		img_path = fmt.Sprintf("http://151.106.120.127/bashyam/assets/apartment_image/%s", imageName)
		// image_path = fmt.Sprintf("apartment_image/%s", imageName)
		if err2 := c.SaveUploadedFile(file, image_path); err2 != nil {
			fmt.Println("2", err2)
			c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err2})
			return
		}

	} else {
		// errMsg := "No image uploaded"
		// c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": errMsg})
		// return
	}

	configRepository := repository.ConfigRepositoryInit(db)
	configService := service.ConfigServiceInit(configRepository, nil, nil)
	response = configService.UpdateApartmentParamService(body.Apartment_Id, body.Param_Key, body.Param_Value, body.PARAM_SUB, body.Validity, body.Action, img_path)

	c.Set("response", response)
}
