package cart

import (
	"git.gocasts.ir/ebhomengo/niki/domain/shoppingbasket/service"
	"git.gocasts.ir/ebhomengo/niki/pkg/claim"
	httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
	"git.gocasts.ir/ebhomengo/niki/types"
	"github.com/labstack/echo/v4"
	"net/http"
	"strconv"
)

type Handler struct {
	svc service.Service
}

func NewHandler(svc service.Service) Handler {
	return Handler{svc: svc}
}

func (h Handler) AddToBasket(c echo.Context) error {
	claims := claim.GetClaimsFromEchoContext(c)

	var req service.AddToCartRequest
	if err := c.Bind(&req); err != nil {
		return c.JSON(http.StatusBadRequest, map[string]string{
			"error": "invalid request body",
		})
	}

	req.UserID = types.ID(claims.UserID)
	if err := h.svc.AddToBasket(c.Request().Context(), req); err != nil {
		msg, code := httpmsg.Error(err)
		return c.JSON(code, msg)
	}

	return c.NoContent(http.StatusNoContent)
}

func (h Handler) GetCart(c echo.Context) error {
	claims := claim.GetClaimsFromEchoContext(c)

	res, err := h.svc.GetCart(c.Request().Context(), types.ID(claims.UserID))
	if err != nil {
		msg, code := httpmsg.Error(err)
		return c.JSON(code, msg)
	}

	return c.JSON(http.StatusOK, res)
}

func (h Handler) RemoveCart(c echo.Context) error {
	claims := claim.GetClaimsFromEchoContext(c)

	cartIDStr := c.Param("cart_id")
	if cartIDStr == "" {
		return c.JSON(http.StatusBadRequest, map[string]string{
			"error": "cart id required",
		})
	}

	cartID, err := strconv.Atoi(cartIDStr)
	if err != nil || cartID < 1 {
		return c.JSON(http.StatusBadRequest, map[string]string{
			"error": "invalid cart id",
		})
	}

	if err := h.svc.ClearCart(c.Request().Context(), types.ID(cartID), types.ID(claims.UserID)); err != nil {
		msg, code := httpmsg.Error(err)
		return c.JSON(code, msg)
	}

	return c.NoContent(http.StatusNoContent)
}

func (h Handler) RemoveItem(c echo.Context) error {
	cartIDStr := c.Param("cart_id")
	itemIDStr := c.Param("item_id")

	if cartIDStr == "" {
		return c.JSON(http.StatusBadRequest, echo.Map{
			"error": "cart id required",
		})
	}

	cID, err := strconv.Atoi(cartIDStr)
	if err != nil {
		return c.JSON(http.StatusBadRequest, map[string]string{
			"error": "invalid product id",
		})
	}

	if itemIDStr == "" {
		return c.JSON(http.StatusBadRequest, echo.Map{
			"error": "item id required",
		})
	}
	iID, err := strconv.Atoi(itemIDStr)
	if err != nil {
		return c.JSON(http.StatusBadRequest, map[string]string{
			"error": "invalid product id",
		})
	}

	var req service.RemoveFromCartRequest

	req.CartID = types.ID(cID)
	req.ItemID = types.ID(iID)

	if err := h.svc.RemoveFromCart(c.Request().Context(), req); err != nil {
		msg, code := httpmsg.Error(err)
		return c.JSON(code, msg)
	}

	return c.NoContent(http.StatusNoContent)
}

func (h Handler) UpdateQuantity(c echo.Context) error {
	var req service.UpdateQuantityRequest
	if err := c.Bind(&req); err != nil {
		return c.JSON(http.StatusBadRequest, map[string]string{
			"error": "invalid request body",
		})
	}

	if err := h.svc.UpdateQuantity(c.Request().Context(), req); err != nil {
		msg, code := httpmsg.Error(err)
		return c.JSON(code, msg)
	}

	return c.NoContent(http.StatusNoContent)
}

func (h Handler) UpdateCartStatus(c echo.Context) error {
	var req service.UpdateCartStatusRequest
	if err := c.Bind(&req); err != nil {
		return c.JSON(http.StatusBadRequest, map[string]string{
			"error": "invalid request body",
		})
	}

	if err := h.svc.UpdateCartStatus(c.Request().Context(), req); err != nil {
		msg, code := httpmsg.Error(err)
		return c.JSON(code, msg)
	}

	return c.NoContent(http.StatusNoContent)
}
