package ui_service

import (
	rolesModule "evening_detective_server/internal/modules/roles"
	"slices"
)

var (
	permissions = map[string][]string{
		rolesModule.Admin: {
			"users_page",
		},
		rolesModule.Author: {
			"scenarios_page",
			"scenario_editor_page",
		},
		rolesModule.Organizer: {
			"scenarios_page",
			"games_page",
			"game_panel_page",
		},
		rolesModule.User: {
			"games_page",
			"catalog_page",
		},
	}
)

type UiService struct{}

func NewUiService() *UiService {
	return &UiService{}
}

func (s *UiService) GetPermissions(roles []string) []string {
	resMap := s.getPermissionsMap(roles)
	res := make([]string, 0, len(resMap))
	for permission := range resMap {
		res = append(res, permission)
	}
	return res
}

func (s *UiService) getPermissionsMap(roles []string) map[string]any {
	resMap := make(map[string]any, 100)

	if slices.Contains(roles, rolesModule.Admin) {
		for _, permissionsList := range permissions {
			for _, permission := range permissionsList {
				resMap[permission] = struct{}{}
			}
		}
		return resMap
	}

	for _, role := range roles {
		for _, permission := range permissions[role] {
			resMap[permission] = struct{}{}
		}
	}
	return resMap
}
