package dev_services

import (
	"cpone/db"
	"cpone/models"
	"fmt"
	"math"
	"strconv"
	"strings"

	dbx "cpone/package/database"

	"go.uber.org/zap"
)

func NewServicesGroupResult(uStore db.AppStore) *ServicesGroupResult {

	return &ServicesGroupResult{
		GroupResultStore: uStore,
	}
}

type ServicesGroupResult struct {
	GroupResultStore db.AppStore
}

func (su *ServicesGroupResult) GetMasterMenus() ([]MasterMenu, error) {

	// dummyBreadcrumb := []Breadcrumb{
	// 	{
	// 		Name: "Dashboard",
	// 		Url:  "/client/dashboard",
	// 	},
	// 	{
	// 		Name: "Front Office",
	// 		Url:  "/client/usermanagement",
	// 	},
	// }

	dummyMenu := []MasterMenu{
		// 1. Dashboard
		{
			ID:             "1",
			ParentMenuID:   "0",
			ParentMenuName: "Dashboard",
			ParentUrl:      "/client/md/dashboard",
			ParentIcon:     "",
			Children: []ChildrenMenu{
				{
					ChildrenParentID: "1",
					ChildrenMenuID:   "1",
					ChildrenMenuName: "Master User",
					ChildrenMenuURL:  "/client/md/user",
					// Breadcrumb: []Breadcrumb{
					// 	{
					// 		IDBreadcrumb: "1",
					// 		Title:        "Dashboard",
					// 		IsActive:     "Y",
					// 	},
					// },
				},
			},
		},

		// 2. Front Office
		{
			ID:             "2",
			ParentMenuID:   "1",
			ParentMenuName: "Front Office",
			ParentUrl:      "#",
			ParentIcon:     "",
			Children: []ChildrenMenu{
				{
					ChildrenParentID: "1",
					ChildrenMenuID:   "1",
					ChildrenMenuName: "Registrasi",
					ChildrenMenuURL:  "/fo/registrasi",
					// Breadcrumb: []Breadcrumb{
					// 	{
					// 		IDBreadcrumb: "1",
					// 		Title:        "Dashboard",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "2",
					// 		Title:        "Master",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "3",
					// 		Title:        "User",
					// 		IsActive:     "Y",
					// 	},
					// },
				},
			},
		},

		// 3. Station
		{
			ID:             "3",
			ParentMenuID:   "2",
			ParentMenuName: "Station",
			ParentUrl:      "#",
			ParentIcon:     "",
			Children: []ChildrenMenu{
				{
					ChildrenParentID: "2",
					ChildrenMenuID:   "1",
					ChildrenMenuName: "Sample Station",
					ChildrenMenuURL:  "/station/sample",
					// Breadcrumb: []Breadcrumb{
					// 	{
					// 		IDBreadcrumb: "1",
					// 		Title:        "Dashboard",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "2",
					// 		Title:        "Master",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "3",
					// 		Title:        "User",
					// 		IsActive:     "Y",
					// 	},
					// },
				},
			},
		},

		// 4. Management MCU
		{
			ID:             "4",
			ParentMenuID:   "3",
			ParentMenuName: "Management MCU",
			ParentUrl:      "#",
			ParentIcon:     "",
			Children: []ChildrenMenu{
				{
					ChildrenParentID: "3",
					ChildrenMenuID:   "1",
					ChildrenMenuName: "Setup MCU",
					ChildrenMenuURL:  "/mcu/setup",
					// Breadcrumb: []Breadcrumb{
					// 	{
					// 		IDBreadcrumb: "1",
					// 		Title:        "Dashboard",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "2",
					// 		Title:        "Master",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "3",
					// 		Title:        "User",
					// 		IsActive:     "Y",
					// 	},
					// },
				},
				{
					ChildrenParentID: "3",
					ChildrenMenuID:   "2",
					ChildrenMenuName: "Pre-Registrasi",
					ChildrenMenuURL:  "/mcu/preregister",
					// Breadcrumb: []Breadcrumb{
					// 	{
					// 		IDBreadcrumb: "1",
					// 		Title:        "Dashboard",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "2",
					// 		Title:        "Master",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "3",
					// 		Title:        "User",
					// 		IsActive:     "Y",
					// 	},
					// },
				},
			},
		},

		// 5. Master Data
		{
			ID:             "5",
			ParentMenuID:   "4",
			ParentMenuName: "Master Data",
			ParentUrl:      "#",
			ParentIcon:     "",
			Children: []ChildrenMenu{
				// * Master User
				{
					ChildrenParentID: "4",
					ChildrenMenuID:   "1",
					ChildrenMenuName: "Master User",
					ChildrenMenuURL:  "/client/md/user",
					// Breadcrumb: []Breadcrumb{
					// 	{
					// 		IDBreadcrumb: "1",
					// 		Title:        "Dashboard",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "2",
					// 		Title:        "Master",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "3",
					// 		Title:        "User",
					// 		IsActive:     "Y",
					// 	},
					// },
				},

				// * Master User Group
				{
					ChildrenParentID: "4",
					ChildrenMenuID:   "2",
					ChildrenMenuName: "Master User Group",
					ChildrenMenuURL:  "/client/md/usergroup",
					// Breadcrumb: []Breadcrumb{
					// 	{
					// 		IDBreadcrumb: "1",
					// 		Title:        "Dashboard",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "2",
					// 		Title:        "Master",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "3",
					// 		Title:        "User Group",
					// 		IsActive:     "Y",
					// 	},
					// },
				},

				// * Master Corporate
				{
					ChildrenParentID: "4",
					ChildrenMenuID:   "3",
					ChildrenMenuName: "Corporate",
					ChildrenMenuURL:  "/client/md/corp",
					// Breadcrumb: []Breadcrumb{
					// 	{
					// 		IDBreadcrumb: "1",
					// 		Title:        "Dashboard",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "2",
					// 		Title:        "Master",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "3",
					// 		Title:        "User Group",
					// 		IsActive:     "Y",
					// 	},
					// },
				},

				// * Test
				{
					ChildrenParentID: "4",
					ChildrenMenuID:   "4",
					ChildrenMenuName: "Test",
					ChildrenMenuURL:  "/client/md/test",
					// Breadcrumb: []Breadcrumb{
					// 	{
					// 		IDBreadcrumb: "1",
					// 		Title:        "Dashboard",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "2",
					// 		Title:        "Master",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "3",
					// 		Title:        "User Group",
					// 		IsActive:     "Y",
					// 	},
					// },
				},

				// * Bahan
				{
					ChildrenParentID: "4",
					ChildrenMenuID:   "5",
					ChildrenMenuName: "Bahan",
					ChildrenMenuURL:  "/client/md/bahan",
					// Breadcrumb: []Breadcrumb{
					// 	{
					// 		IDBreadcrumb: "1",
					// 		Title:        "Dashboard",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "2",
					// 		Title:        "Master",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "3",
					// 		Title:        "User Group",
					// 		IsActive:     "Y",
					// 	},
					// },
				},

				// * Sample Type
				{
					ChildrenParentID: "4",
					ChildrenMenuID:   "6",
					ChildrenMenuName: "Sample Type",
					ChildrenMenuURL:  "/client/sample/type",
					// Breadcrumb: []Breadcrumb{
					// 	{
					// 		IDBreadcrumb: "1",
					// 		Title:        "Dashboard",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "2",
					// 		Title:        "Master",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "3",
					// 		Title:        "User Group",
					// 		IsActive:     "Y",
					// 	},
					// },
				},

				// * Sample Station
				{
					ChildrenParentID: "4",
					ChildrenMenuID:   "7",
					ChildrenMenuName: "Sample Station",
					ChildrenMenuURL:  "/station/sample",
					// Breadcrumb: []Breadcrumb{
					// 	{
					// 		IDBreadcrumb: "1",
					// 		Title:        "Dashboard",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "2",
					// 		Title:        "Master",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "3",
					// 		Title:        "User Group",
					// 		IsActive:     "Y",
					// 	},
					// },
				},

				// * Result Non Lab
				{
					ChildrenParentID: "4",
					ChildrenMenuID:   "8",
					ChildrenMenuName: "Result Non Lab",
					ChildrenMenuURL:  "/client/md/resultnonlab",
					// Breadcrumb: []Breadcrumb{
					// 	{
					// 		IDBreadcrumb: "1",
					// 		Title:        "Dashboard",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "2",
					// 		Title:        "Master",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "3",
					// 		Title:        "User Group",
					// 		IsActive:     "Y",
					// 	},
					// },
				},

				// * Template Fisik
				{
					ChildrenParentID: "4",
					ChildrenMenuID:   "9",
					ChildrenMenuName: "Template Fisik",
					ChildrenMenuURL:  "/client/md/template-fisik",
					// Breadcrumb: []Breadcrumb{
					// 	{
					// 		IDBreadcrumb: "1",
					// 		Title:        "Dashboard",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "2",
					// 		Title:        "Master",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "3",
					// 		Title:        "User Group",
					// 		IsActive:     "Y",
					// 	},
					// },
				},

				// * Pasien
				{
					ChildrenParentID: "4",
					ChildrenMenuID:   "10",
					ChildrenMenuName: "Pasien",
					ChildrenMenuURL:  "/client/md/pasien",
					// Breadcrumb: []Breadcrumb{
					// 	{
					// 		IDBreadcrumb: "1",
					// 		Title:        "Dashboard",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "2",
					// 		Title:        "Master",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "3",
					// 		Title:        "User Group",
					// 		IsActive:     "Y",
					// 	},
					// },
				},

				// * Dokter
				{
					ChildrenParentID: "4",
					ChildrenMenuID:   "11",
					ChildrenMenuName: "Dokter",
					ChildrenMenuURL:  "/client/md/dokter",
					// Breadcrumb: []Breadcrumb{
					// 	{
					// 		IDBreadcrumb: "1",
					// 		Title:        "Dashboard",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "2",
					// 		Title:        "Master",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "3",
					// 		Title:        "User Group",
					// 		IsActive:     "Y",
					// 	},
					// },
				},

				// * Paket
				{
					ChildrenParentID: "4",
					ChildrenMenuID:   "12",
					ChildrenMenuName: "Paket",
					ChildrenMenuURL:  "/client/md/paket",
					// Breadcrumb: []Breadcrumb{
					// 	{
					// 		IDBreadcrumb: "1",
					// 		Title:        "Dashboard",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "2",
					// 		Title:        "Master",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "3",
					// 		Title:        "User Group",
					// 		IsActive:     "Y",
					// 	},
					// },
				},

				// * Harga Single
				{
					ChildrenParentID: "4",
					ChildrenMenuID:   "13",
					ChildrenMenuName: "Harga Single",
					ChildrenMenuURL:  "/client/md/hargasingle",
					// Breadcrumb: []Breadcrumb{
					// 	{
					// 		IDBreadcrumb: "1",
					// 		Title:        "Dashboard",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "2",
					// 		Title:        "Master",
					// 		IsActive:     "N",
					// 	},
					// 	{
					// 		IDBreadcrumb: "3",
					// 		Title:        "User Group",
					// 		IsActive:     "Y",
					// 	},
					// },
				},
			},
		},
	}

	return dummyMenu, nil
}

func (ug *ServicesGroupResult) GetUserGroup() ([]models.UserGroup, error) {
	data := []models.UserGroup{
		{UserGroupID: 1, UserGroupKode: "A", UserGroupName: "UG1"},
		{UserGroupID: 2, UserGroupKode: "B", UserGroupName: "UG2"},
		{UserGroupID: 3, UserGroupKode: "C", UserGroupName: "UG3"},
	}
	return data, nil
}

func (su *ServicesGroupResult) GetListBreadCrumb(title string) (models.BreadCrumbV1, error) {
	var ret models.BreadCrumbV1

	dummyBreadcrumb := []models.BreadCrumbV1{
		{
			Title: "Master User Group",
			Item: []models.ItemBreadCrumbV1{
				{
					Item: "Dashboard",
					Url:  "/dev/dashboard",
				},
				{
					Item: "Master",
					Url:  "/dev/master",
				},
				{
					Item: "User Group",
					Url:  "",
				},
			},
		},

		{
			Title: "Master User",
			Item: []models.ItemBreadCrumbV1{
				{
					Item: "Dashboard",
					Url:  "/dev/dashboard",
				},
				{
					Item: "Master",
					Url:  "/dev/master",
				},
				{
					Item: "User",
					Url:  "",
				},
			},
		},

		{
			Title: "Master Group Result",
			Item: []models.ItemBreadCrumbV1{
				{
					Item: "Dashboard",
					Url:  "/dev/dashboard",
				},
				{
					Item: "Master",
					Url:  "/dev/master",
				},
				{
					Item: "Group Result",
					Url:  "",
				},
			},
		},
	}

	for _, breadcrumb := range dummyBreadcrumb {
		// if utils.Contains(breadcrumb.Title, title) {
		// 	ret = breadcrumb
		// 	return ret, nil
		// }
		if breadcrumb.Title == title {
			ret = breadcrumb
			return ret, nil
		}
	}

	return ret, nil
}

// (LAB, NONLAB, FISIK, DOCTOR)
// hardcore group result resume mcu
func (ug *ServicesGroupResult) GetGroupResultResumeMcu() ([]models.GroupResultResumeMcu, error) {
	data := []models.GroupResultResumeMcu{
		{
			ID:   1,
			Text: "LAB",
		},
		{
			ID:   2,
			Text: "NONLAB",
		},
		{
			ID:   3,
			Text: "FISIK",
		},
		{
			ID:   4,
			Text: "DOCTOR",
		},
	}
	return data, nil
}

// versi 2
func (su *ServicesGroupResult) GetListGroupResult(search string, currentPage int, rowPerPage int) ([]models.GroupResult, int, error) {
	var groupresultList []models.GroupResult

	logger, _ := zap.NewProduction()
	defer logger.Sync()

	var totalData int
	offset := (currentPage - 1) * rowPerPage

	prm := "%" + strings.TrimSpace(search) + "%"
	querytotal := `
		SELECT COUNT(*) 
		FROM group_result
		WHERE Group_ResultIsActive = 'Y'
		AND (Group_ResultName LIKE ? OR Group_ResultResumeMcu LIKE ?) 
	`

	logger.Info("QUERY SEARCH INITIAL TOTAL COUNT",
		zap.String("query search", querytotal),
	)

	if err := dbx.Handlex.Get(&totalData, querytotal, prm, prm); err != nil {
		return nil, 0, fmt.Errorf("error querying database: %v", err)
	}

	query := `
		SELECT
			ROW_NUMBER() OVER () AS nomor,
			Group_ResultID,
			IFNULL(Group_ResultName, '') AS Group_ResultName,
			IFNULL(Group_ResultFlagPerTest, '') AS Group_ResultFlagPerTest,
			IFNULL(Group_ResultFlagNonLab, '') AS Group_ResultFlagNonLab,
			IFNULL(Group_ResultResumeMcu, '') AS Group_ResultResumeMcu,
			Group_ResultIsActive
		FROM group_result
		WHERE Group_ResultIsActive = 'Y'
		AND (Group_ResultName LIKE ? OR Group_ResultResumeMcu LIKE ?) 
		ORDER BY Group_ResultID ASC
		LIMIT ? OFFSET ?
	`

	logger.Info("QUERY SEARCH INITIAL",
		zap.String("query search", query),
	)

	if err := dbx.Handlex.Select(&groupresultList, query, prm, prm, rowPerPage, offset); err != nil {
		return nil, 0, fmt.Errorf("error querying database: %v", err)
	}

	totalPage := int(math.Ceil(float64(totalData) / float64(rowPerPage)))

	return groupresultList, totalPage, nil
}

// GET BY ID
func (ug *ServicesGroupResult) GetGroupResultByIDV1(id string) (models.GroupResult, error) {
	logger, _ := zap.NewProduction()

	var data models.GroupResult
	err := dbx.Handlex.Get(&data, `SELECT 
	Group_ResultID,
    Group_ResultName,
    CASE 
        WHEN Group_ResultFlagPerTest = 'Y' THEN 'on' 
        ELSE Group_ResultFlagPerTest 
    END AS Group_ResultFlagPerTest,
    CASE 
        WHEN Group_ResultFlagNonLab = 'Y' THEN 'on' 
        ELSE Group_ResultFlagNonLab 
    END AS Group_ResultFlagNonLab,
    Group_ResultResumeMcu,
    Group_ResultIsActive
	FROM group_result
	WHERE Group_ResultID = ?`, id)

	if err != nil {
		defer logger.Sync()
		logger.Error("Error get data gr by id vs",
			zap.String("ID", id),
			zap.Error(err),
		)
		return data, fmt.Errorf("QUERY_FAILED")
	}
	return data, nil
}

// ADD DATA
func (ug *ServicesGroupResult) AddGroupResult(groupresultname string, groupresultflagpertest string, groupresultflagnonlab string, groupresultresumemcu string) (models.GroupResult, error) {
	logger, _ := zap.NewProduction()
	var data models.GroupResult
	var datacek []models.GroupResult
	// var groupresultflagpertestConvert string = ""
	// var groupresultflagnonlabConvert string = ""
	// var groupresultresumemcuConvert string = ""

	prm := "%" + strings.TrimSpace(groupresultname) + "%"
	qryCek := `SELECT Group_ResultName FROM group_result WHERE Group_ResultName LIKE ? AND Group_ResultIsActive = 'Y'`
	err := dbx.Handlex.Select(&datacek, qryCek, prm)

	if err != nil {
		defer logger.Sync()
		logger.Error("Error cek data by groupresultname",
			zap.String("groupresultname", groupresultname),
			zap.Any("datacek", datacek),
		)
		return data, fmt.Errorf("QUERY_FAILED")
	}
	if len(datacek) > 0 {
		defer logger.Sync()
		logger.Error("Group Result Name already taken by another data",
			zap.String("groupresultname", groupresultname),
		)
		return data, fmt.Errorf("group result name sudah dipakai")
	}
	qry := `INSERT INTO group_result
			(
				Group_ResultName,
				Group_ResultFlagPerTest,
				Group_ResultFlagNonLab,
				Group_ResultResumeMcu
			)
			VALUES (
				?,
				?,
				?,
				?
			);`
	// time.Sleep(5 * time.Second)
	rst := dbx.Handlex.MustExec(qry, groupresultname, groupresultflagpertest, groupresultflagnonlab, groupresultresumemcu)
	insertedID, err := rst.LastInsertId()
	if err != nil {
		defer logger.Sync()
		logger.Error("Error Insert group result",
			zap.Any("groupresultname", groupresultname),
			zap.Any("groupresultflagpertest", groupresultflagpertest),
			zap.Any("groupresultflagnonlab", groupresultflagnonlab),
			zap.Any("groupresultresumemcu", groupresultresumemcu),
		)
		return data, fmt.Errorf("QUERY_FAILED")
	}
	s := strconv.Itoa(int(insertedID))
	data, err = ug.GetGroupResultByIDV1(s)
	if err != nil {
		defer logger.Sync()
		logger.Error("Error get usergroup by id",
			zap.Any("groupresultname", groupresultname),
			zap.Any("groupresultflagpertest", groupresultflagpertest),
			zap.Any("groupresultflagnonlab", groupresultflagnonlab),
			zap.Any("groupresultresumemcu", groupresultresumemcu),
		)
		return data, fmt.Errorf("QUERY_FAILED")
	}
	return data, nil
}

// EDIT DATA
func (ug *ServicesGroupResult) EditGroupResult(id string, groupresultname string, groupresultflagpertest string, groupresultflagnonlab string, groupresultresumemcu string) (models.GroupResult, error) {
	logger, _ := zap.NewProduction()
	var data models.GroupResult
	var datacek []models.GroupResult

	prm := "%" + strings.TrimSpace(groupresultname) + "%"
	qryCek := `SELECT Group_ResultName FROM group_result WHERE Group_ResultName LIKE ? AND Group_ResultIsActive = 'Y' AND Group_ResultID <> ?`
	err := dbx.Handlex.Select(&datacek, qryCek, prm, id)

	if err != nil {
		defer logger.Sync()
		logger.Error("Error cek data by code",
			zap.String("groupresultname", groupresultname),
			zap.Any("datacek", datacek),
		)
		return data, fmt.Errorf("QUERY_FAILED")
	}
	if len(datacek) > 0 {
		defer logger.Sync()
		logger.Error("Code already taken by another data",
			zap.String("groupresultname", groupresultname),
		)
		return data, fmt.Errorf("group result name sudah dipakai")
	}

	qry := `UPDATE group_result
			SET Group_ResultName = ?,
			Group_ResultFlagPerTest = ?,
			Group_ResultFlagNonLab = ?,
			Group_ResultResumeMcu = ?
			WHERE Group_ResultID = ?`
	// time.Sleep(5 * time.Second)
	rst := dbx.Handlex.MustExec(qry, groupresultname, groupresultflagpertest, groupresultflagnonlab, groupresultresumemcu, id)
	_, err = rst.RowsAffected()
	if err != nil {
		defer logger.Sync()
		logger.Error("Error Update group result",
			zap.Any("groupresultname", groupresultname),
			zap.Any("groupresultflagpertest", groupresultflagpertest),
			zap.Any("groupresultflagnonlab", groupresultflagnonlab),
			zap.Any("groupresultresumemcu", groupresultresumemcu),
		)
		return data, fmt.Errorf("QUERY_FAILED")
	}

	data, err = ug.GetGroupResultByIDV1(id)
	if err != nil {
		defer logger.Sync()
		logger.Error("Error get group result by id",
			zap.Any("groupresultname", groupresultname),
			zap.Any("groupresultflagpertest", groupresultflagpertest),
			zap.Any("groupresultflagnonlab", groupresultflagnonlab),
			zap.Any("groupresultresumemcu", groupresultresumemcu),
		)
		return data, fmt.Errorf("QUERY_FAILED")
	}
	return data, nil
}

// DELETE DATA
func (ug *ServicesGroupResult) DeleteGroupResult(id string) (models.GroupResult, error) {
	logger, _ := zap.NewProduction()
	var data models.GroupResult
	qry := `UPDATE group_result
			SET Group_ResultIsActive = 'N'
			WHERE Group_ResultID = ?;`
	// time.Sleep(5 * time.Second)
	rst := dbx.Handlex.MustExec(qry, id)
	_, err := rst.RowsAffected()
	if err != nil {
		defer logger.Sync()
		logger.Error("Error delete group result",
			zap.String("id", id),
		)
		return data, fmt.Errorf("QUERY_FAILED")
	}

	data, err = ug.GetGroupResultByIDV1(id)
	if err != nil {
		defer logger.Sync()
		logger.Error("Error get group result by id",
			zap.String("id", id),
		)
		return data, fmt.Errorf("QUERY_FAILED")
	}
	return data, nil
}
