package permission

import (
	"context"

	"gateway/internal/svc"
	"gateway/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type DeleteRoleLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// NewDeleteRoleLogic returns the role deleter.
func NewDeleteRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteRoleLogic {
	return &DeleteRoleLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// DeleteRole removes a role; is_system roles refuse, as do roles with
// active assignments.
func (l *DeleteRoleLogic) DeleteRole(req *types.DeleteRoleByIDReq) error {
	if l.svcCtx.PermissionRole == nil {
		return errb.SysNotImplemented("permission module not configured")
	}
	actor, err := ActorFromContext(l.ctx)
	if err != nil {
		return errb.AuthUnauthorized(err.Error()).WithCause(err)
	}
	return l.svcCtx.PermissionRole.Delete(l.ctx, actor.TenantID, req.ID)
}
