package roles

import (
	"log"

	"gorm.io/gorm"

	"server/internal/http/controllers"
	"server/internal/models"
)

func CheckUserRoleConsistency(db *gorm.DB, resolver *controllers.RoleResolver) {
	var list []models.User
	if err := db.Select("email", "roles").Find(&list).Error; err != nil {
		log.Printf("warning: cannot verify user roles: %v", err)
		return
	}

	for _, u := range list {
		for _, r := range u.Roles {
			if !resolver.RoleDefined(r) {
				log.Printf("inconsistency: user %s has undefined role %q", u.Email, r)
			}
		}
	}
}
