package basic_pricing

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

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

func NewBasicPricingHandler(helper helpers.HelperInterface, g *gin.RouterGroup) BasicPricingHandlerInterface {
	return &BasicPricingHandler{Helper: helper, Group: g}
}

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

func (h *BasicPricingHandler) 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.BasicPricingFilterAllRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		response.Error(c, http.StatusBadRequest, err.Error())
		return
	}
	u := usecases.NewBasicPricingUsecase(h.Helper, *userid)
	pricings, _, err := u.GetAll(req)
	if err != nil {
		response.Error(c, http.StatusInternalServerError, err.Error())
		return
	}
	response.Success(c, pricings)
}

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

	response.Success(c, pricing)
}

func (h *BasicPricingHandler) Create(c *gin.Context) {
	var req dto.CreateBasicPricingRequest
	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.NewBasicPricingUsecase(h.Helper, *userid)
	pricing, err := u.Create(req, createdBy, merchantid)
	if err != nil {
		response.Error(c, http.StatusInternalServerError, err.Error())
		return
	}
	response.Created(c, pricing)
}

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

	var req dto.CreateBasicPricingRequest
	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.NewBasicPricingUsecase(h.Helper, *userid)
	pricing, err := u.Update(id, req, updatedBy)
	if err != nil {
		response.Error(c, http.StatusInternalServerError, err.Error())
		return
	}
	response.Success(c, pricing)
}

func (h *BasicPricingHandler) 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.NewBasicPricingUsecase(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": "basic pricing deleted"})
}

func (h *BasicPricingHandler) GetMatch(c *gin.Context) {
	var req dto.BasicPricingMatchRequest
	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
	}

	u := usecases.NewBasicPricingUsecase(h.Helper, *userid)
	pricing := u.GetMatch(req.ID)
	if pricing == nil {
		response.Error(c, http.StatusNotFound, "basic pricing not found")
		return
	}
	response.Success(c, pricing)
}
