package cross

import (
	"context"

	"github.com/tidbcloud/account/internel/dao/entity"
	"gorm.io/gorm"
)

func (e *CrossDao) IsUserInOrg(ctx context.Context, tx *gorm.DB, userID uint64, orgID uint64) (bool, error) {
	var roles []entity.RoleBinding
	err := tx.Where(
		"target_type = ? AND target_id = ? AND scope_type = ? AND scope_id = ? AND status = ?",
		entity.RoleBindingTargetUser,
		userID,
		entity.RoleBindingScopeOrg,
		orgID,
		entity.RoleBindingStatusActive,
	).Find(&roles).Error

	if err != nil {
		return false, err
	}

	for _, role := range roles {
		if role.Role != "" {
			return true, nil
		}
	}

	return false, nil
}
