package auth

import (
	"server/internal/responses"

	"github.com/gofiber/fiber/v3"
)

/* const (
	"superadmin" = SuperAdminPermission
	"admin" = AdminPermission
	"manager" = ManagerPermission
	"content_creator" = ContentCreatorPermission
	"user" = UserPermission
	"guest" = GuestPermission
) */

type Role struct {
	Name       Permission `json:"name"`
	Permission int        `json:"permission"`
}

var Roles = []Role{
	{"superadmin", SuperAdminPermission},
	{"admin", AdminPermission},
	{"manager", ManagerPermission},
	{"content_creator", ContentCreatorPermission},
	{"user", UserPermission},
	{"guest", GuestPermission},
}

// Typescript: type
type Permission string

// Typescript: enum=EnumPermission
const (
	RoleSuperAdmin     Permission = "superadmin"
	RoleAdmin          Permission = "admin"
	RoleManager        Permission = "manager"
	RoleContentCreator Permission = "content_creator"
	RoleUser           Permission = "user"
	RoleGuest          Permission = "guest"
)

type Permissions struct {
	SuperAdmin     Permission `json:"superadmin"`
	Admin          Permission `json:"admin"`
	Manager        Permission `json:"manager"`
	ContentCreator Permission `json:"content_creator"`
	User           Permission `json:"user"`
	Guest          Permission `json:"guest"`
}

const (
	SuperAdminPermission     = 0b1111111111111111
	AdminPermission          = 0b0111111111111111
	ManagerPermission        = 0b0010111111111111
	ContentCreatorPermission = 0b0001111111111111
	UserPermission           = 0b0000000000000011
	GuestPermission          = 0b0000000000000001
)

// Typescript: type
type AllRoles map[string]string

func GetRoles(c fiber.Ctx) error {
	a := make(AllRoles)
	a["RoleSuperAdmin"] = "superadmin"
	a["RoleAdmin"] = "admin"
	a["RoleManager"] = "manager"
	a["RoleContentCreator"] = "content_creator"
	a["RoleUser"] = "user"
	a["RoleGuest"] = "guest"

	return c.JSON(responses.Success(a))
}

func PermissionToString(p int) Permission {
	for _, role := range Roles {
		if role.Permission == p {
			return role.Name
		}
	}
	return "unknown"
}

func RoleToPermission(s Permission) int {
	for _, role := range Roles {
		if role.Name == s {
			return role.Permission
		}
	}
	return 0
}
