package casbin

import (
	"iris-demo/library/config"

	"github.com/casbin/casbin/v2"
	gormadapter "github.com/casbin/gorm-adapter/v3"
	_ "github.com/glebarez/sqlite"
	_ "github.com/go-sql-driver/mysql"
)

var enforcer *casbin.Enforcer

func init() {
	// a := gormadapter.NewAdapter("mysql", config.Config.GetString("database.msql"), true)
	a, _ := gormadapter.NewAdapter("sqlite3", config.Config.GetString("database.sqlite"), true)
	e, _ := casbin.NewEnforcer("./config/casbinmodel.conf", a)

	enforcer = e
}

func Enforce(user string, obj string, act string) (bool, error) {
	return enforcer.Enforce(user, obj, act)
}

func AddGroupingPolicy(user string, role string) (bool, error) {
	return enforcer.AddGroupingPolicy(user, role)
}

func RemoveGroupingPolicy(user string, role string) (bool, error) {
	return enforcer.RemoveGroupingPolicy(user, role)
}

func AddPolicy(role string, obj string, act string) (bool, error) {
	return enforcer.AddPolicy(role, obj, act)
}

func RemovePolicy(role string, obj string, act string) (bool, error) {
	return enforcer.RemovePolicy(role, obj, act)
}

func ClearPolicy() {
	enforcer.ClearPolicy()
}
