package moda

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 ModaHandlerInterface interface {
	Router()
}

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

func NewModaHandler(helper helpers.HelperInterface, g *gin.RouterGroup) ModaHandlerInterface {
	return &ModaHandler{Helper: helper, Group: g}
}

func (h *ModaHandler) Router() {
	r := h.Group.Group("modas")
	{
		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 *ModaHandler) 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.ModaFilterAllRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		response.Error(c, http.StatusBadRequest, err.Error())
		return
	}
	u := usecases.NewModaUsecase(h.Helper, *userid)
	items, _, err := u.GetAll(req)
	if err != nil {
		response.Error(c, http.StatusInternalServerError, err.Error())
		return
	}
	var responseDTO = make([]dto.CreateModaResponse, 0)
	for _, v := range items {
		var DTO dto.CreateModaResponse
		h.Helper.MapStructToStruct(v, "json", &DTO, "json")
		responseDTO = append(responseDTO, DTO)
	}
	response.Success(c, responseDTO)
}

func (h *ModaHandler) 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.NewModaUsecase(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 *ModaHandler) 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.NewModaUsecase(h.Helper, *userid)
	item, err := u.GetByID(id)
	if err != nil {
		response.Error(c, http.StatusNotFound, "moda not found")
		return
	}

	response.Success(c, item)
}

func (h *ModaHandler) Create(c *gin.Context) {
	var req dto.CreateModaRequest
	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.NewModaUsecase(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.CreateModaResponse
	h.Helper.MapStructToStruct(item, "json", &responseDTO, "json")
	response.Created(c, responseDTO)
}

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

	var req dto.UpdateModaRequest
	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.NewModaUsecase(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.CreateModaResponse
	h.Helper.MapStructToStruct(item, "json", &responseDTO, "json")
	response.Success(c, responseDTO)
}

func (h *ModaHandler) 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.NewModaUsecase(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": "moda deleted"})
}
