import { Types } from 'mongoose';
import { BlocksService } from './blocks.service';

describe('BlocksService list', () => {
  it('returns paginated blocked users without sensitive fields', async () => {
    const blockedId = new Types.ObjectId();
    const createdAt = new Date('2026-06-01T10:00:00.000Z');
    const blocksRepository = {
      findBlockedUsers: jest.fn().mockResolvedValue([
        {
          blockedId: {
            _id: blockedId,
            name: 'Artist',
            username: 'artist',
            avatar: '/uploads/avatar.jpg',
            stageName: 'Stage',
            email: 'hidden@example.com',
          },
          createdAt,
        },
      ]),
      countBlockedUsers: jest.fn().mockResolvedValue(1),
    };
    const service = new BlocksService(blocksRepository as any, {} as any);

    const result = await service.listBlockedUsers(new Types.ObjectId().toString(), {
      page: 1,
      limit: 20,
    });

    expect(result.items).toEqual([
      {
        userId: blockedId.toString(),
        name: 'Artist',
        username: 'artist',
        avatar: expect.any(String),
        stageName: 'Stage',
        blockedAt: createdAt.toISOString(),
      },
    ]);
    expect(JSON.stringify(result.items)).not.toContain('hidden@example.com');
    expect(result.pagination).toMatchObject({ page: 1, limit: 20, total: 1, totalPages: 1 });
  });

  it('unblock removes the user from future list results through repository deletion', async () => {
    const currentUserId = new Types.ObjectId().toString();
    const targetUserId = new Types.ObjectId().toString();
    const blocksRepository = {
      remove: jest.fn().mockResolvedValue(undefined),
    };
    const service = new BlocksService(blocksRepository as any, {} as any);

    await expect(service.unblock(currentUserId, targetUserId)).resolves.toEqual({
      blocked: false,
      targetUserId,
    });
    expect(blocksRepository.remove).toHaveBeenCalledWith(currentUserId, targetUserId);
  });
});
