package corporate_handlers

import (
	breadcrumadmin "cpone/component/breadcrumbadmin"
	navbarmenu "cpone/component/navbar"
	sidebaruserprofile "cpone/component/sidebar_user_profile"
	"cpone/models"
	"cpone/utils"
	corporate_mcudetail "cpone/views/corporate/mcu"
	corporate_tabmcudetail "cpone/views/corporate/mcu/mcutab"

	"github.com/golang-jwt/jwt/v5"
	"github.com/labstack/echo/v4"
	"go.uber.org/zap"
)

type McuDetailServices interface {
	GetBreadcrumb(id string) (models.BreadCrumbV1, error)
	GetTabNameList(id string) ([]models.McuTabModels, error)
}

type McuDetailHandler struct {
	McuDetailServices McuDetailServices
}

func NewMcuDetailHandler(mcud McuDetailServices) *McuDetailHandler {
	return &McuDetailHandler{
		McuDetailServices: mcud,
	}
}

func (mcud *McuDetailHandler) HandlerShowMcuDetailScreen(c echo.Context) error {
	logger, _ := zap.NewProduction()

	id := c.Param("id")
	logger.Info("params", zap.Any("id", id))

	userCok := c.Get("user").(*jwt.Token)
	claims := userCok.Claims.(jwt.MapClaims)
	name := claims["M_StaffName"].(string)
	position := claims["M_UserGroupDashboard"].(string)
	// logger.Info("jwt", zap.Any("name", name))

	user := models.User{
		UserID:       1,
		Username:     name,
		UserFullName: name,
		UserPosition: position,
	}

	dataBreadcrumb, err := mcud.McuDetailServices.GetBreadcrumb(id)
	if err != nil {
		defer logger.Sync()
		logger.Info("Error breadcrumb corp", zap.Any("error", err))
		return err
	}

	breadcrumb := breadcrumadmin.MainBreadcrumbAdminV1(dataBreadcrumb)
	navbaruser := navbarmenu.NavbarWithLogo(user)
	sidbaruser := sidebaruserprofile.Navbaruserprofile(user)

	// test := []models.McuTabModels{}
	tabname, err := mcud.McuDetailServices.GetTabNameList(id)
	if err != nil {
		return err
	}
	// logger.Info("List tab name", zap.Any("tav", tabname))

	content := corporate_mcudetail.McuDetailScreen(
		breadcrumb,
		// corporate_mcudetail.TabViewMcuDetail(id),
		corporate_mcudetail.McuDetailTabView(id, tabname),
	)
	css := corporate_mcudetail.CSSMcuDetail(
		corporate_tabmcudetail.CssKesimpulan(),
	)
	js := corporate_mcudetail.JSMcuDetail()

	view := corporate_mcudetail.ShowMcuDetail(
		dataBreadcrumb.Title,
		content,
		css,
		js,
		navbaruser,
		sidbaruser,
	)

	return utils.View(c, view)
}
