package corporate_handlers

import (
	breadcrumadmin "cpone/component/breadcrumbadmin"
	navbarmenu "cpone/component/navbar"
	"cpone/component/pagination"
	sidebaruserprofile "cpone/component/sidebar_user_profile"
	"cpone/models"
	"cpone/utils"
	corporate_dashboardpic "cpone/views/corporate/dashboardpic"
	"net/http"
	"strconv"

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

type DashboardPicServices interface {
	GetEmployeeAnalyticBreadcrumb(title string) (models.BreadCrumbV1, error)
	GetCorporateName(userID string) (string, error)
	DummyDataTest() ([]models.DashboardPic, error)
	ListingDashboardPic(email string, search string, startdate string, enddate string, currentpage int, rowperpage int) ([]models.DashboardPic, int, error)
}

type DashboardPicHandler struct {
	DashboardPicServices DashboardPicServices
}

func NewDashboardPicHandler(ea DashboardPicServices) *DashboardPicHandler {
	return &DashboardPicHandler{
		DashboardPicServices: ea,
	}
}

func (ea *DashboardPicHandler) HandleShowEmployeeAnalyticScreen(c echo.Context) error {
	logger, _ := zap.NewProduction()

	listID := utils.GenerateRandomID("listid")
	paginationID := utils.GenerateRandomID("paginationid")

	userCok := c.Get("user").(*jwt.Token)
	claims := userCok.Claims.(jwt.MapClaims)
	userID := claims["M_UserID"].(string)
	name := claims["M_StaffName"].(string)
	position := claims["M_UserGroupDashboard"].(string)
	email := claims["M_UserEmail"].(string)

	title, err := ea.DashboardPicServices.GetCorporateName(userID)
	if err != nil {
		defer logger.Sync()
		logger.Info("error get corporate name", zap.Any("error", err))
		return err
	}

	convr, _ := strconv.Atoi(userID)

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

	dataBreadcrumb, err := ea.DashboardPicServices.GetEmployeeAnalyticBreadcrumb(title)
	if err != nil {
		defer logger.Sync()
		logger.Info("Error breadcrumb dev", zap.Any("error", err))
		return err
	}
	defer logger.Sync()

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

	listingData, totalPage, err := ea.DashboardPicServices.ListingDashboardPic(email, "", "", "", 1, 10)
	if err != nil {
		defer logger.Sync()
		logger.Info("Error get listing data", zap.Any("error", err), zap.Any("total", totalPage))
		return err
	}
	listingcomponent := corporate_dashboardpic.ListingData(listingData, listID)

	paginationcomponent := pagination.PaginationV3(
		totalPage,
		1,
		"/corp/dashboard_pic/changepage",
		paginationID,
		"#listID, #paginationID, #searchID, #startdateID, #enddateID",
		"#"+paginationID,
		"outerHTML", "", "",
		corporate_dashboardpic.BeforeRequestContent(),
		corporate_dashboardpic.AfterRequestContent(),
	)

	filtercomponent := corporate_dashboardpic.FilterListing(
		"/corp/dashboard_pic/filter",
		"input changed delay:500ms",
		"#searchID, #startdateID, #enddateID, #listID, #paginationID",
		"outerHTML",
		"#"+paginationID,
		corporate_dashboardpic.BeforeRequestContent(),
		corporate_dashboardpic.AfterRequestContent(),
	)

	content := corporate_dashboardpic.DashboardPic(
		listID,
		paginationID,
		breadcrumb,
		filtercomponent,
		listingcomponent,
		paginationcomponent,
	)
	css := corporate_dashboardpic.CSSDashboardPIC()
	js := corporate_dashboardpic.JSDashboardPIC()

	view := corporate_dashboardpic.ShowDashboardPic(title, content, css, js, navbaruser, sidbaruser)
	return utils.View(c, view)
}

func (ea *DashboardPicHandler) HandlePagination(c echo.Context) error {

	pageparam := c.QueryParam("page")
	listID := c.QueryParam("listID")
	paginationID := c.QueryParam("paginationID")
	search := c.QueryParam("search")
	startdate := c.QueryParam("startdate")
	enddate := c.QueryParam("enddate")

	userCok := c.Get("user").(*jwt.Token)
	claims := userCok.Claims.(jwt.MapClaims)
	email := claims["M_UserEmail"].(string)

	var retVal []templ.Component
	logger, _ := zap.NewProduction()
	page, err := strconv.Atoi(pageparam)
	if err != nil {
		defer logger.Sync()
		logger.Info("Error convert page param")
		return err
	}

	listdata, totalPage, err := ea.DashboardPicServices.ListingDashboardPic(email, search, startdate, enddate, page, 10)
	if err != nil {
		defer logger.Sync()
		logger.Info("Error convert page param")
		return err
	}
	listcomponent := corporate_dashboardpic.ListingData(listdata, listID)

	paginationcomponent := pagination.PaginationV3(
		totalPage,
		page,
		"/corp/dashboard_pic/changepage",
		paginationID,
		"#listID, #paginationID, #searchID, #startdateID, #enddateID",
		"#"+paginationID,
		"outerHTML", "", "",
		corporate_dashboardpic.BeforeRequestContent(),
		corporate_dashboardpic.AfterRequestContent(),
	)

	retVal = append(retVal, listcomponent)
	retVal = append(retVal, paginationcomponent)
	return utils.ViewMulti(c, retVal)
}

func (ea *DashboardPicHandler) HandleFilter(c echo.Context) error {
	logger, _ := zap.NewProduction()

	search := c.QueryParam("search")
	startdate := c.QueryParam("startdate")
	enddate := c.QueryParam("enddate")
	listID := c.QueryParam("listID")
	paginationID := c.QueryParam("paginationID")

	userCok := c.Get("user").(*jwt.Token)
	claims := userCok.Claims.(jwt.MapClaims)
	email := claims["M_UserEmail"].(string)

	dataList, totalPage, err := ea.DashboardPicServices.ListingDashboardPic(email, search, startdate, enddate, 1, 10)
	if err != nil {
		defer logger.Sync()
		logger.Info("Error Get Listing Data", zap.Any("error", err))
		return err
	}

	listcomponent := corporate_dashboardpic.ListingData(dataList, listID)
	paginationcomponent := pagination.PaginationV3(
		totalPage, 1,
		"/corp/dashboard_pic/changepage",
		paginationID,
		"#listID, #paginationID, #searchID, #startdateID, #enddateID",
		"#"+paginationID, "outerHTML", "", "",
		corporate_dashboardpic.BeforeRequestContent(),
		corporate_dashboardpic.AfterRequestContent(),
	)

	var retval []templ.Component
	retval = append(retval, listcomponent)
	retval = append(retval, paginationcomponent)
	return utils.ViewMulti(c, retval)
}

func (ea *DashboardPicHandler) HandleToDetail(c echo.Context) error {
	logger, _ := zap.NewProduction()
	id := c.Param("id")

	logger.Info("params", zap.Any("id", id))

	url := "/corp/dashboard_pic/detail/" + id
	c.Response().Header().Set("HX-Redirect", url)

	return c.String(http.StatusOK, url)
}
