package mcu_corporate_handlers

import (
	"cpone/component/chart"
	"cpone/models"
	"cpone/utils"
	corporate_mcudetail "cpone/views/corporate/mcu/mcutab"
	"strings"

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

type TabKepersertaanServices interface {
	GetTotalKepesertaanMCU(mcuID string) (models.Piechart, error)
	GetKepesertaanByGender(mcuID string) (models.Piechart, error)
	GetKepesertaanByUmur(mcuID string) (models.Piechart, error)
	GetChartRenderPeserta(mcuID string) ([]models.McuTabModels, error)
}

type TabKepersertaanHandler struct {
	TabKepersertaanServices TabKepersertaanServices
}

func NewTabKepersertaanHandler(tkh TabKepersertaanServices) *TabKepersertaanHandler {
	return &TabKepersertaanHandler{
		TabKepersertaanServices: tkh,
	}
}

func (tkh *TabKepersertaanHandler) HandleShowTabKepesertaan(c echo.Context) error {
	logger, _ := zap.NewProduction()
	logger.Info("Tab Kepesertaan")

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

	tablist, err := tkh.TabKepersertaanServices.GetChartRenderPeserta(id)
	if err != nil {
		return err
	}

	chartsxs := []templ.Component{}
	chartsmd := []templ.Component{}
	chartslg := []templ.Component{}

	for _, d := range tablist {
		chartsxs = append(chartsxs, chart.ShowChartResponsiveWidth(
			strings.ToLower(strings.ReplaceAll(d.Mcu_ImageGrafikName+"xs", " ", "")),
			d.Mcu_ImageGrafikJsonRender,
			"450px",
		))
		chartsmd = append(chartsmd, chart.ShowChartResponsiveWidth(
			strings.ToLower(strings.ReplaceAll(d.Mcu_ImageGrafikName+"md", " ", "")),
			d.Mcu_ImageGrafikJsonRender,
			"450px",
		))
		chartslg = append(chartslg, chart.ShowChartResponsiveWidth(
			strings.ToLower(strings.ReplaceAll(d.Mcu_ImageGrafikName+"lg", " ", "")),
			d.Mcu_ImageGrafikJsonRender,
			"450px",
		))
	}

	content := corporate_mcudetail.TabKepesertaan(
		chartsxs,
		chartsmd,
		chartslg,
	)

	return utils.View(c, content)
}
