package services

import (
	"fmt"
	"strconv"
	"time"

	"github.com/emarifer/go-templ-project-structure/db"
	"github.com/emarifer/go-templ-project-structure/models"
)

func NewServicesMasterMenuUserGroup(u MasterMenu, uStore db.MasterMenuUserGroupStore) *ServicesMasterMenuUserGroup {

	return &ServicesMasterMenuUserGroup{
		MasterMenu:               u,
		MasterMenuUserGroupStore: uStore,
	}
}

// MasterMenu struct
type Breadcrumb struct {
	IDBreadcrumb string `json:"id_breadcrumb"`
	Name         string `json:"name"`
	Url          string `json:"url"`
}

type ChildrenMenu struct {
	ChildrenParentID string `json:"children_parent_id"`
	ChildrenMenuID   string `json:"children_menu_id"`
	ChildrenMenuName string `json:"children_menu_name"`
	ChildrenMenuURL  string `json:"children_menu_url"`
	// Breadcrumb       []Breadcrumb `json:"breadcrumb"`
}

type MasterMenu struct {
	ID             string         `json:"id"`
	ParentMenuID   string         `json:"parent_menu_id"`
	ParentMenuName string         `json:"parent_menu_name"`
	ParentUrl      string         `json:"parent_url"`
	ParentIcon     string         `json:"parent_icon"`
	Children       []ChildrenMenu `json:"children"`
}

type ServicesMasterMenuUserGroup struct {
	MasterMenu               MasterMenu
	MasterMenuUserGroupStore db.MasterMenuUserGroupStore
}

func (su *ServicesMasterMenuUserGroup) 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 *ServicesMasterMenuUserGroup) 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 (ug *ServicesMasterMenuUserGroup) GetUserGroupByID(id string) (models.UserGroup, error) {
	listdata := []models.UserGroup{
		{UserGroupID: 1, UserGroupKode: "A", UserGroupName: "UG1"},
		{UserGroupID: 2, UserGroupKode: "B", UserGroupName: "UG2"},
		{UserGroupID: 3, UserGroupKode: "C", UserGroupName: "UG3"},
	}
	var data models.UserGroup
	idData, err := strconv.Atoi(id)
	if err != nil {
		return data, fmt.Errorf("pack %v: %w", "Error", err)
	}
	for _, v := range listdata {

		if v.UserGroupID == idData {
			data = v
		}
	}
	time.Sleep(5 * time.Second)
	return data, nil
}

// func (su *ServicesMasterMenuUserGroup) GetAllMasterMenus() ([]MasterMenu, error) {
// 	query := `SELECT id, MasterMenuname, email, created_at FROM MasterMenus ORDER BY created_at DESC`

// 	rows, err := su.MasterMenuUserGroupStore.Db.Query(query)
// 	if err != nil {
// 		return []MasterMenu{}, err
// 	}
// 	// We close the resource
// 	defer rows.Close()

// 	MasterMenus := []MasterMenu{}
// 	for rows.Next() {
// 		rows.Scan(
// 			&su.MasterMenu.ID,
// 			&su.MasterMenu.MasterMenuname,
// 			&su.MasterMenu.Email,
// 			&su.MasterMenu.CreatedAt,
// 		)

// 		MasterMenus = append(MasterMenus, su.MasterMenu)
// 	}

// 	return MasterMenus, nil
// }

// func (su *ServicesMasterMenuUserGroup) GetMasterMenuById(id int) (MasterMenu, error) {

// 	query := `SELECT id, MasterMenuname, email, created_at FROM MasterMenus
// 		WHERE id = ?`

// 	stmt, err := su.MasterMenuUserGroupStore.Db.Prepare(query)
// 	if err != nil {
// 		return MasterMenu{}, err
// 	}

// 	defer stmt.Close()

// 	su.MasterMenu.ID = id
// 	err = stmt.QueryRow(
// 		su.MasterMenu.ID,
// 	).Scan(
// 		&su.MasterMenu.ID,
// 		&su.MasterMenu.MasterMenuname,
// 		&su.MasterMenu.Email,
// 		&su.MasterMenu.CreatedAt,
// 	)
// 	if err != nil {
// 		return MasterMenu{}, err
// 	}

// 	return su.MasterMenu, nil
// }
