package repository

import (
	"context"
	"testing"

	"github.com/user-management-system/internal/domain"
)

func TestRolePermissionRepository_GetPermissionsByIDs(t *testing.T) {
	db := setupTestDB(t)
	repo := NewRolePermissionRepository(db)
	ctx := context.Background()

	// 创建权限
	perm1 := &domain.Permission{Code: "perm1", Name: "权限1", Status: domain.PermissionStatusEnabled}
	perm2 := &domain.Permission{Code: "perm2", Name: "权限2", Status: domain.PermissionStatusEnabled}
	db.WithContext(ctx).Create(perm1)
	db.WithContext(ctx).Create(perm2)

	// 创建角色
	role := &domain.Role{Code: "test-role", Name: "测试角色", Status: domain.RoleStatusEnabled}
	db.WithContext(ctx).Create(role)

	// 分配权限
	repo.Create(ctx, &domain.RolePermission{RoleID: role.ID, PermissionID: perm1.ID})
	repo.Create(ctx, &domain.RolePermission{RoleID: role.ID, PermissionID: perm2.ID})

	// 测试批量获取权限
	perms, err := repo.GetPermissionsByIDs(ctx, []int64{perm1.ID, perm2.ID})
	if err != nil {
		t.Fatalf("GetPermissionsByIDs() error = %v", err)
	}
	if len(perms) != 2 {
		t.Errorf("len(perms) = %d, want 2", len(perms))
	}

	// 测试空列表
	emptyPerms, err := repo.GetPermissionsByIDs(ctx, []int64{})
	if err != nil {
		t.Fatalf("GetPermissionsByIDs() empty error = %v", err)
	}
	if len(emptyPerms) != 0 {
		t.Errorf("len(emptyPerms) = %d, want 0", len(emptyPerms))
	}

	// 测试不存在的ID
	nonExistentPerms, err := repo.GetPermissionsByIDs(ctx, []int64{9999})
	if err != nil {
		t.Fatalf("GetPermissionsByIDs() non-existent error = %v", err)
	}
	if len(nonExistentPerms) != 0 {
		t.Errorf("len(nonExistentPerms) = %d, want 0", len(nonExistentPerms))
	}
}
