package package_type

import (
	"cargo-erp-backend/internal/handlers/dto"
	"cargo-erp-backend/internal/usecases"
	"cargo-erp-backend/pkg/helpers"
	"cargo-erp-backend/pkg/response"
	"net/http"

	"github.com/gin-gonic/gin"
)

type PackageTypeHandlerInterface interface {
	Router()
}

type PackageTypeHandler struct {
	Helper helpers.HelperInterface
	Group  *gin.RouterGroup
}

func NewPackageTypeHandler(helper helpers.HelperInterface, g *gin.RouterGroup) PackageTypeHandlerInterface {
	return &PackageTypeHandler{Helper: helper, Group: g}
}

func (h *PackageTypeHandler) Router() {
	r := h.Group.Group("package-types")
	{
		r.GET("/", h.GetAll)
		r.POST("/list", h.GetList)
		r.GET("/:id", h.GetByID)
		r.POST("/", h.Create)
		r.PUT("/:id", h.Update)
		r.DELETE("/:id", h.Delete)
	}
}

func (h *PackageTypeHandler) GetAll(c *gin.Context) {
	userid, err := h.Helper.GetAuthInfo(c.GetString("auth"))
	if err != nil {
		response.Error(c, http.StatusUnauthorized, err.Error())
		return
	}
	var req dto.PackageTypeFilterAllRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		response.Error(c, http.StatusBadRequest, err.Error())
		return
	}
	u := usecases.NewPackageTypeUsecase(h.Helper, *userid)
	items, _, err := u.GetAll(req)
	if err != nil {
		response.Error(c, http.StatusInternalServerError, err.Error())
		return
	}
	var responseDTO = make([]dto.CreatePackageTypeResponse, 0)
	for _, v := range items {
		var DTO dto.CreatePackageTypeResponse
		h.Helper.MapStructToStruct(v, "json", &DTO, "json")
		responseDTO = append(responseDTO, DTO)
	}
	response.Success(c, responseDTO)
}

func (h *PackageTypeHandler) GetList(c *gin.Context) {
	userid, err := h.Helper.GetAuthInfo(c.GetString("auth"))
	if err != nil {
		response.Error(c, http.StatusUnauthorized, err.Error())
		return
	}

	var req dto.DataTableRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "Request Format request invalid")
		return
	}
	u := usecases.NewPackageTypeUsecase(h.Helper, *userid)
	res, err := u.GetList(req)
	if err != nil {
		response.Error(c, http.StatusInternalServerError, err.Error())
		return
	}
	response.Success(c, res)
	return
}

func (h *PackageTypeHandler) GetByID(c *gin.Context) {
	userid, err := h.Helper.GetAuthInfo(c.GetString("auth"))
	if err != nil {
		response.Error(c, http.StatusUnauthorized, err.Error())
		return
	}
	id := c.Param("id")

	u := usecases.NewPackageTypeUsecase(h.Helper, *userid)
	item, err := u.GetByID(id)
	if err != nil {
		response.Error(c, http.StatusNotFound, "package type not found")
		return
	}

	response.Success(c, item)
}

func (h *PackageTypeHandler) Create(c *gin.Context) {
	var req dto.CreatePackageTypeRequest
	if err := c.BindJSON(&req); err != nil {
		response.Error(c, http.StatusBadRequest, err.Error())
		return
	}
	userid, err := h.Helper.GetAuthInfo(c.GetString("auth"))
	if err != nil {
		response.Error(c, http.StatusUnauthorized, err.Error())
		return
	}
	var merchantid *string = nil
	if userid.UsersRole_Relation == nil || userid.UsersRole_Relation.RoleKey != "SPM" {
		merchantid = userid.MerchantID
	}
	createdBy := userid.ID

	u := usecases.NewPackageTypeUsecase(h.Helper, *userid)
	item, err := u.Create(req.Name, createdBy, merchantid)
	if err != nil {
		response.Error(c, http.StatusInternalServerError, err.Error())
		return
	}
	var responseDTO dto.CreatePackageTypeResponse
	h.Helper.MapStructToStruct(item, "json", &responseDTO, "json")
	response.Created(c, responseDTO)
}

func (h *PackageTypeHandler) Update(c *gin.Context) {
	id := c.Param("id")

	var req dto.UpdatePackageTypeRequest
	if err := c.BindJSON(&req); err != nil {
		response.Error(c, http.StatusBadRequest, err.Error())
		return
	}
	userid, err := h.Helper.GetAuthInfo(c.GetString("auth"))
	if err != nil {
		response.Error(c, http.StatusUnauthorized, err.Error())
		return
	}
	updatedBy := userid.ID

	u := usecases.NewPackageTypeUsecase(h.Helper, *userid)
	item, err := u.Update(id, req.Name, updatedBy)
	if err != nil {
		response.Error(c, http.StatusInternalServerError, err.Error())
		return
	}
	var responseDTO dto.CreatePackageTypeResponse
	h.Helper.MapStructToStruct(item, "json", &responseDTO, "json")
	response.Success(c, responseDTO)
}

func (h *PackageTypeHandler) Delete(c *gin.Context) {
	userid, err := h.Helper.GetAuthInfo(c.GetString("auth"))
	if err != nil {
		response.Error(c, http.StatusUnauthorized, err.Error())
		return
	}
	id := c.Param("id")
	u := usecases.NewPackageTypeUsecase(h.Helper, *userid)
	if err := u.Delete(id); err != nil {
		response.Error(c, http.StatusInternalServerError, err.Error())
		return
	}

	response.Success(c, map[string]interface{}{"message": "package type deleted"})
}
