package restricted

import (
	"git.ma-al.com/goc_daniel/b2b/app/service/menuService"
	"git.ma-al.com/goc_daniel/b2b/app/utils/i18n"
	"git.ma-al.com/goc_daniel/b2b/app/utils/nullable"
	"git.ma-al.com/goc_daniel/b2b/app/utils/response"
	"git.ma-al.com/goc_daniel/b2b/app/utils/responseErrors"
	"github.com/gofiber/fiber/v3"
)

type MenuHandler struct {
	menuService *menuService.MenuService
}

func NewMenuHandler() *MenuHandler {
	menuService := menuService.New()
	return &MenuHandler{
		menuService: menuService,
	}
}

func MenuHandlerRoutes(r fiber.Router) fiber.Router {
	handler := NewMenuHandler()

	r.Get("/get-menu", handler.GetMenu)
	r.Get("/get-top-menu", handler.GetTopMenu)

	return r
}

func (h *MenuHandler) GetMenu(c fiber.Ctx) error {
	lang_id, ok := c.Locals("langID").(uint)
	if !ok {
		return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
			JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute)))
	}
	menu, err := h.menuService.GetMenu(lang_id)
	if err != nil {
		return c.Status(responseErrors.GetErrorStatus(err)).
			JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
	}

	return c.JSON(response.Make(&menu, 0, i18n.T_(c, response.Message_OK)))
}

func (h *MenuHandler) GetTopMenu(c fiber.Ctx) error {
	lang_id, ok := c.Locals("langID").(uint)
	if !ok {
		return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
			JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute)))
	}
	menu, err := h.menuService.GetTopMenu(lang_id)
	if err != nil {
		return c.Status(responseErrors.GetErrorStatus(err)).
			JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
	}

	return c.JSON(response.Make(&menu, len(menu), i18n.T_(c, response.Message_OK)))
}
