package public_handlers

import (
	public_services "cpone/services/public"
	public_landingpage "cpone/views/public/landingpage"

	"github.com/a-h/templ"
	"go.uber.org/zap"

	"github.com/labstack/echo/v4"
)

type LandingPageService interface {
	GetClientService() ([]public_services.ClientService, error)
	GetClientAdvantage() ([]public_services.AdvantageClient, error)
	GetPromotionList() ([]public_services.Promotion, error)
	GetFooterNavList() ([]public_services.FooterNav, error)
}

func NewLandingPageHandler(us LandingPageService) *LandingPageHandler {
	return &LandingPageHandler{
		LandingPageService: us,
	}
}

type LandingPageHandler struct {
	LandingPageService LandingPageService
}

func (uh *LandingPageHandler) ShowLandingPage(c echo.Context) error {
	udata, err := uh.LandingPageService.GetClientService()
	if err != nil {
		// fmt.Println(err)
		return err
	}
	adData, err := uh.LandingPageService.GetClientAdvantage()
	if err != nil {
		// fmt.Println(err)
		return err
	}
	lpData, err := uh.LandingPageService.GetPromotionList()
	if err != nil {
		// fmt.Println(err)
		return err
	}
	ftNav, err := uh.LandingPageService.GetFooterNavList()
	if err != nil {
		return err
	}
	logger, _ := zap.NewProduction()
	defer logger.Sync()
	logger.Info("User registration successful",
		zap.String("username", "john_doe"),
		zap.Int("id", 12345),
		zap.Any("data", udata),
	)
	// fmt.Printf("%+v\n", udata)

	helo := public_landingpage.ShowLandingPage("Hello World",
		public_landingpage.MainLandingPage(public_landingpage.ListMedicalService(udata),
			public_landingpage.ListAdvantage(adData), public_landingpage.ListPromotion(lpData),
			public_landingpage.FooterSection(ftNav)),
		public_landingpage.CssLandingPage(),
		public_landingpage.JsLandingPage())
	return uh.View(c, helo)
}

func (uh *LandingPageHandler) View(c echo.Context, cmp templ.Component) error {
	c.Response().Header().Set(echo.HeaderContentType, echo.MIMETextHTML)

	return cmp.Render(c.Request().Context(), c.Response().Writer)
}
