package corporate_services

import (
	"cpone/db"
	"cpone/models"
	dbx "cpone/package/database"
	"fmt"
	"math"

	"go.uber.org/zap"
)

type DashboardPicServices struct {
	DashboardPicStore db.AppStore
}

func NewDashboardPicServices(uStore db.AppStore) *DashboardPicServices {
	return &DashboardPicServices{
		DashboardPicStore: uStore,
	}
}

func (ea *DashboardPicServices) GetEmployeeAnalyticBreadcrumb(title string) (models.BreadCrumbV1, error) {
	breadcrumb := models.BreadCrumbV1{
		Title: title,
		Item: []models.ItemBreadCrumbV1{
			{
				Item: "Dashboard",
				Url:  "/corp/dashboard_pic",
			},
			{
				Item: "Employee Health Medical Analytic",
				Url:  "",
			},
		},
	}

	return breadcrumb, nil
}

func (ea *DashboardPicServices) DummyDataTest() ([]models.DashboardPic, error) {
	employees := []models.DashboardPic{
		{
			Mgm_McuID:                     1,
			Mgm_McuLabel:                  "Employee 1",
			Mgm_McuFlagRelasiBayarSendiri: "Yes",
			Mgm_McuBisaTambahPemeriksaan:  "No",
			Mgm_McuCorporateID:            101,
			Mgm_McuNumber:                 "EMP001",
			Mgm_McuNumberNational:         "NAT001",
			Mgm_McuNote:                   "Lorem ipsum dolor sit amet.",
			Mgm_McuStartDate:              "2024-01-01",
			Mgm_McuEndDate:                "2024-12-31",
			Mgm_McuIsActive:               "Y",
		},
		{
			Mgm_McuID:                     2,
			Mgm_McuLabel:                  "Employee 2",
			Mgm_McuFlagRelasiBayarSendiri: "No",
			Mgm_McuBisaTambahPemeriksaan:  "Yes",
			Mgm_McuCorporateID:            102,
			Mgm_McuNumber:                 "EMP002",
			Mgm_McuNumberNational:         "NAT002",
			Mgm_McuNote:                   "Lorem ipsum dolor sit amet consectetur.",
			Mgm_McuStartDate:              "2024-02-15",
			Mgm_McuEndDate:                "2024-11-30",
			Mgm_McuIsActive:               "Y",
		},
	}

	return employees, nil
}

func (ea *DashboardPicServices) GetCorporateName(userID string) (string, error) {
	var corpName string

	q := `
		SELECT
		CorporateName
		FROM mgm_mcuuser
		JOIN mgm_mcu ON Mgm_McuID = Mgm_McuUserMgm_McuID
		JOIN corporate ON CorporateID = Mgm_McuCorporateID
		WHERE Mgm_McuUserID = ?
	`
	if err := dbx.Handlex.Get(&corpName, q, userID); err != nil {
		return "PT. ABC", err
	}

	if corpName == "" {
		corpName = "Perusahaan A"
	}

	return corpName, nil
}

func (ea *DashboardPicServices) ListingDashboardPic(email string, search string, startdate string, enddate string, currentpage int, rowperpage int) ([]models.DashboardPic, int, error) {
	logger, _ := zap.NewProduction()
	var ret []models.DashboardPic
	var totalData int

	if len(startdate) == 0 {
		startdate = "1900-01-01"
	}
	if len(enddate) == 0 {
		enddate = "9999-12-12"
	}

	prmLabel := "%" + search + "%"
	offset := (currentpage - 1) * rowperpage
	// q := `
	// 	SELECT COUNT(*)
	// 	FROM mgm_mcu
	// 	WHERE Mgm_McuIsActive = 'Y'
	// 	AND Mgm_McuLabel LIKE ?
	// 	AND Mgm_McuStartDate >= ?
	// 	AND Mgm_McuEndDate <= ?
	// `
	// if err := dbx.Handlex.Get(&totalData, q, prmLabel, startdate, enddate); err != nil {
	// 	return nil, 0, fmt.Errorf("error query get total data: %v", err)
	// }
	logger.Info("Params", zap.Any("email", email))
	q := `
		SELECT
		Mgm_McuID,
		Mgm_McuLabel,
		Mgm_McuFlagRelasiBayarSendiri,
		Mgm_McuBisaTambahPemeriksaan,
		Mgm_McuCorporateID,
		Mgm_McuNumber,
		Mgm_McuNumberNational,
		Mgm_McuNote,
		DATE_FORMAT(Mgm_McuStartDate, '%d-%m-%Y') AS Mgm_McuStartDate,
		DATE_FORMAT(Mgm_McuEndDate, '%d-%m-%Y') AS Mgm_McuEndDate,
		Mgm_McuIsActive
		FROM mgm_mcu
		JOIN mgm_mcuuser ON Mgm_McuUserMgm_McuID = Mgm_McuID
		WHERE Mgm_McuUserEmail = ?
		AND Mgm_McuIsActive = 'Y'
		AND Mgm_McuLabel LIKE ?
		AND Mgm_McuStartDate >= ?
		AND Mgm_McuEndDate <= ?
		ORDER BY Mgm_McuID ASC
		LIMIT ? OFFSET ?
	`
	if err := dbx.Handlex.Select(&ret, q, email, prmLabel, startdate, enddate, rowperpage, offset); err != nil {
		return nil, 0, fmt.Errorf("error query get data: %v", err)
	}
	totalData = len(ret)
	logger.Info("Response", zap.Any("data", ret), zap.Any("total data", totalData))

	totalPage := int(math.Ceil(float64(totalData) / float64(rowperpage)))
	return ret, totalPage, nil
}
