package perms_connectors

import (
	"oc-auth/conf"

	"cloud.o-forge.io/core/oc-lib/tools"
)

type Permission struct {
	Object        string      `json:"object,omitempty"`
	Relation      string      `json:"relation,omitempty"`
	Subject       string      `json:"subject,omitempty"`
	SubPermission *Permission `json:"sub_perm,omitempty"`
}

func (p Permission) Namespace() string {
	return "open-cloud"
}

func (k Permission) Scope() string {
	return "oc-auth"
}

type PermConnector interface {
	Status() tools.State
	SetClient(scope string)
	CheckPermission(perm Permission, permDependancies *Permission, internal bool) bool
	BindRole(userID string, roleID string) (string, int, error)
	BindGroup(userID string, groupID string) (string, int, error)
	BindPermission(roleID string, permID string, relation string) (*Permission, int, error)

	UnBindRole(userID string, roleID string) (string, int, error)
	UnBindGroup(userID string, groupID string) (string, int, error)
	UnBindPermission(roleID string, permID string, relation string) (*Permission, int, error)

	CreateRole(roleID string) (string, int, error)
	CreateGroup(groupID string) (string, int, error)
	CreatePermission(permID string, relation string, internal bool) (string, int, error)
	DeleteRole(roleID string) (string, int, error)
	DeleteGroup(groupID string) (string, int, error)
	DeletePermission(permID string, relation string, internal bool) (string, int, error)

	GetRoleByUser(userID string) ([]string, error)
	GetGroupByUser(userID string) ([]string, error)
	GetPermissionByRole(roleID string) ([]Permission, error)
	GetPermissionByUser(userID string, internal bool) ([]Permission, error)

	GetRole(roleID string) ([]string, error)
	GetGroup(groupID string) ([]string, error)
	GetPermission(permID string, relation string) ([]Permission, error)
}

var c = map[string]PermConnector{
	"keto": KetoConnector{},
}

func GetPermissionConnector(scope string) PermConnector {
	return c[conf.GetConfig().PermissionConnectorHost]
}
