package casbin_service

import (
	"bwallet/models"
)

type AdminRole struct {
	Id     int32
	Uid    int32
	RoleId int32

	PageNum  int
	PageSize int
}

func (r *AdminRole) GetAdminRole() (*models.Role, error) {
	role, err := models.GetRole(r.Id)
	if err != nil {
		return nil, err
	}

	return role, nil
}

func (r *AdminRole) GetAllAdminRoles() ([]*models.AdminRole, error) {
	roles, err := models.GetAdminRoles(r.PageNum, r.PageSize, r.getMaps())
	if err != nil {
		return nil, err
	}

	return roles, nil
}

func (r *AdminRole) AddAdminRole() error {
	models.RemoveAdminRole(r.Uid)

	role := map[string]interface{}{
		"uid":     r.Uid,
		"role_id": r.RoleId,
	}
	if err := models.AddAdminRole(role); err != nil {
		return err
	}

	return nil
}

func (r *AdminRole) DeleteAdminRole() error {
	return models.DeleteAdminRole(r.Id)
}

func (r *AdminRole) getMaps() (map[string]interface{}) {
	maps := make(map[string]interface{})

	if r.Id != 0 {
		maps["id"] = r.Id
	}

	if r.Uid != 0 {
		maps["uid"] = r.Uid
	}

	if r.RoleId != 0 {
		maps["role_id"] = r.RoleId
	}

	return maps
}
