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_patient "cpone/views/corporate/patient"
	"strconv"

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

type PatientService interface {
	GetDashboardPatientBreadcrumb(title string) (models.BreadCrumbV1, error)
	GetListtingData(email string, currentpage int, rowperpage int) ([]models.DashboardPatient, int, error)
	GetPatientData(email string) (string, error)
	GetListReportPesertaV4(orderheaderID string, staffname string) ([]models.TabViewReportMcu, error)
}

func NewPatientHandler(us PatientService) *PatientHandler {
	return &PatientHandler{
		PatientService: us,
	}
}

type PatientHandler struct {
	PatientService PatientService
}

func (ph *PatientHandler) LoadLocalData(c echo.Context) error {
	logger, _ := zap.NewProduction()
	listID := c.QueryParam("listID")
	paginationID := c.QueryParam("paginationID")
	dialogID := c.QueryParam("dialogID")
	dialogBodyID := c.QueryParam("dialogBodyID")
	userEmail := c.QueryParam("userEmail")
	logger.Info("params", zap.Any("email", userEmail))

	title, err := ph.PatientService.GetPatientData(userEmail)
	if err != nil {
		defer logger.Sync()
		logger.Info("Error get user", zap.Any("error", err))
		return err
	}

	dataBreadcrumb, err := ph.PatientService.GetDashboardPatientBreadcrumb(title)
	if err != nil {
		defer logger.Sync()
		logger.Info("Error breadcrumb", zap.Any("error", err))
		return err
	}
	defer logger.Sync()
	breadcrumb := breadcrumadmin.MainBreadcrumbAdminV1(dataBreadcrumb)

	listingdata, totalPage, err := ph.PatientService.GetListtingData(userEmail, 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_patient.ListingData(listingdata,
		listID,
		"/corp/dashboard_pat/open",
		"#"+dialogBodyID,
		"#listID, #paginationID, #dialogID, #dialogBodyID, #userEmail",
		"outerHTML",
	)

	paginationcomponent := pagination.PaginationV3(
		totalPage,
		1,
		"/corp/dashboard_pat/changepage",
		paginationID,
		"#listID, #paginationID, #loading-parent, #loading-child, #loading-spinner, #loadingcontent, #dialogID, #dialogBodyID, #userEmail",
		"#contentlayout",
		"outerHTML", "", "",
		corporate_patient.BeforeRequestContent(),
		corporate_patient.AfterRequestContent(),
	)

	tabData := []models.TabViewReportMcu{}
	tabcomponent := corporate_patient.TabReportView(tabData)

	bodyModal := corporate_patient.ModalBody(
		dialogBodyID,
		tabcomponent,
		corporate_patient.JsShowModal(""),
	)

	bodyClose := corporate_patient.ModalClose(
		"/corp/dashboard_pat/closemodal",
		"#"+dialogBodyID,
		"outerHTML",
		"#"+dialogID,
	)

	modalDialog := corporate_patient.DialogReport(
		"",
		"/corp/dashboard_pat/dialog",
		"#"+dialogBodyID,
		"outerHTML",
		"#listID, #paginationID, #dialogID, #userEmail, #dialogBodyID"+paginationID,
		dialogID,
		title,
		bodyModal,
		bodyClose,
	)

	content := corporate_patient.ContentP(listingcomponent, paginationcomponent)

	view := corporate_patient.ContentPage(
		breadcrumb,
		content,
		modalDialog,
	)

	return utils.View(c, view)
}

func (ph *PatientHandler) HandleShowPatient(c echo.Context) error {
	logger, _ := zap.NewProduction()

	listID := utils.GenerateRandomID("listid")
	paginationID := utils.GenerateRandomID("paginationid")
	dialogID := utils.GenerateRandomID("dialogid")
	dialogBodyID := utils.GenerateRandomID("dialogbodyid")

	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,
	}

	navbaruser := navbarmenu.NavbarWithLogo(user)
	sidbaruser := sidebaruserprofile.Navbaruserprofile(user)

	content := corporate_patient.DashboardPat(
		listID,
		paginationID,
		dialogID,
		dialogBodyID,
	)
	css := corporate_patient.CSSDashboardPat()
	js := corporate_patient.JSDashboardPat()

	view := corporate_patient.ShowDashboardPat("Dashboard Pasien", content, css, js, navbaruser, sidbaruser)
	return utils.View(c, view)
}

func (ph *PatientHandler) HandleChangePage(c echo.Context) error {
	logger, _ := zap.NewProduction()

	pageparam := c.QueryParam("page")
	listID := c.QueryParam("listID")
	paginationID := c.QueryParam("paginationID")
	dialogBodyID := c.QueryParam("dialogBodyID")
	userEmail := c.QueryParam("userEmail")
	logger.Info("params", zap.Any("userEmail", userEmail))

	// var retval []templ.Component
	page, err := strconv.Atoi(pageparam)
	if err != nil {
		defer logger.Sync()
		logger.Info("error convert page param")
		return err
	}

	listData, totalPage, err := ph.PatientService.GetListtingData(userEmail, page, 10)
	if err != nil {
		defer logger.Sync()
		logger.Info("error get data listing")
		return err
	}
	listcomponent := corporate_patient.ListingData(listData,
		listID,
		"/corp/dashboard_pat/open",
		"#"+dialogBodyID,
		"#listID, #paginationID, #dialogID, #dialogBodyID, #userEmail",
		"outerHTML",
	)

	paginationcomponent := pagination.PaginationV3(
		totalPage,
		page,
		"/corp/dashboard_pat/changepage",
		paginationID,
		"#listID, #paginationID, #loading-parent, #loading-child, #loading-spinner, #loadingcontent, #dialogID, #dialogBodyID, #userEmail, #currPage"+paginationID,
		"#contentlayout",
		"outerHTML", "", "",
		corporate_patient.BeforeRequestContent(),
		corporate_patient.AfterRequestContent(),
	)

	// retval = append(retval, listcomponent)
	// retval = append(retval, paginationcomponent)
	// return utils.ViewMulti(c, retval)

	content := corporate_patient.ContentP(listcomponent, paginationcomponent)
	return utils.View(c, content)
}

func (ph *PatientHandler) HandleOpenReport(c echo.Context) error {
	logger, _ := zap.NewProduction()

	id := c.QueryParam("id")
	dialogID := c.QueryParam("dialogID")
	dialogBodyID := c.QueryParam("dialogBodyID")
	logger.Info("params", zap.Any("id", id))

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

	newtabdata, err := ph.PatientService.GetListReportPesertaV4(id, staffname)
	if err != nil {
		tab := corporate_patient.TabReportView([]models.TabViewReportMcu{})
		mod := corporate_patient.ModalBody(
			dialogBodyID,
			tab,
			corporate_patient.JsShowModal(""),
		)
		return utils.View(c, mod)
	}

	tabcomponent := corporate_patient.TabReportView(newtabdata)
	modalBody := corporate_patient.ModalBody(
		dialogBodyID,
		tabcomponent,
		corporate_patient.JsShowModal("#"+dialogID),
	)

	return utils.View(c, modalBody)
}
