package domain

import (
	"context"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
)

type Role struct {
	Id        int64   `json:"id"`        // 角色ID
	CompanyId int64   `json:"companyId"` // 公司ID
	Name      string  `json:"name"`      // 角色名称
	Auths     []int64 `json:"auths"`     // 角色权限列表
	Remark    string  `json:"remark"`    // 备注
	Users     []User  `json:"users"`     // 绑定的用户

	CreatedAt int64 `json:"createdAt,omitempty"`
	UpdatedAt int64 `json:"updatedAt,omitempty"`
	DeletedAt int64 `json:"deletedAt,omitempty"`
	Version   int   `json:"version,omitempty"`
}

type RoleRepository interface {
	Insert(ctx context.Context, conn transaction.Conn, dm *Role) (*Role, error)
	Update(ctx context.Context, conn transaction.Conn, dm *Role) (*Role, error)
	UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *Role) (*Role, error)
	Delete(ctx context.Context, conn transaction.Conn, dm *Role) (*Role, error)
	FindOne(ctx context.Context, conn transaction.Conn, id int64) (*Role, error)
	Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*Role, error)
}

func (m *Role) Identify() interface{} {
	if m.Id == 0 {
		return nil
	}
	return m.Id
}

func (m *Role) GetAuth(id int64) *Auth {
	for _, auth := range Auths {
		if auth.Id == id {
			return &auth
		}
	}
	return nil
}

type Auth struct {
	Id       int64  `json:"id,omitempty"`   // 菜单ID
	ParentId int64  `json:"parentId"`       // 父级ID
	Name     string `json:"name,omitempty"` // 菜单名称
	Code     string `json:"code,omitempty"` // 菜单编码
}

var Auths = []Auth{
	NewAuth(1001, "邀请注册", "MINI_INVITE-REGISTRATION", 0),
	NewAuth(1002, "帖子定性", "SYSTEM_POST-JUDGMENT", 0),
	//NewAuth(1003, "圆桌判定", "SYSTEM_STUDENT-MANAGE", 0),
	//NewAuth(1004, "圆桌运营", "SYSTEM_FEEDBACK-MANAGE", 0),
}

func NewAuth(id int64, name, code string, pid int64) Auth {
	return Auth{
		Id:       id,
		Name:     name,
		Code:     code,
		ParentId: pid,
	}
}