package ii

import (
	"testing"

	"git.simanc.com/software/golib/v4/features/mo"
)

type testUser mo.M

func (u testUser) Name() string         { return "" }
func (u testUser) UserName() string     { return "" }
func (u testUser) Flag() bool           { return true }
func (u testUser) IsSysadmin() bool     { return false }
func (u testUser) Company() mo.ObjectID { return mo.NilObjectID }
func (u testUser) CompanyALL() mo.A     { return mo.A{} }
func (u testUser) Group(name string) bool {
	groupList, ok := u["group"].(mo.A)
	if !ok {
		return false
	}
	for _, group := range groupList {
		if group == name {
			return true
		}
	}
	return false
}
func (u testUser) Role(groupName string) (string, bool) {
	roleMap, ok := u["role"].(mo.M)
	if !ok {
		return "", false
	}
	role, has := roleMap[groupName]
	if !has {
		return "", false
	}
	return role.(string), true
}
func (u testUser) Perms(_ string) ([]string, bool) { return []string{}, false }

func (u testUser) ID() mo.ObjectID {
	return mo.ID.New()
}
func (u testUser) Get(k string) any {
	v, ok := u[k]
	if !ok {
		return nil
	}
	return v
}

func TestLoadPerms(t *testing.T) {
	permission, err := LoadPerms("./_test/filter.json")
	if err != nil {
		t.Error(err)
		return
	}
	data := mo.M{
		"department": "department_test",
		"group":      mo.A{"GROUP.USER"},
		"role": mo.M{
			"GROUP.USER": "user",
		},
	}
	d, ok := permission.Has(NewNameMust("test.user"), testUser(data))
	if !ok {
		t.Error()
		return
	}
	t.Log(d)
}
