import type { Message, Room } from '../../../../shared-kernel';
import { createDefaultNotificationSettings } from '../models/notification.model';
import { calculateUnreadForRoom } from './notification.logic';

function createRoom(overrides: Partial<Room> = {}): Room {
  return {
    id: 'room-1',
    name: 'Room',
    hostId: 'host',
    isPrivate: false,
    createdAt: 0,
    userCount: 1,
    channels: [{ id: 'text-1', name: 'lobby', type: 'text', position: 0 }],
    ...overrides
  };
}

function createMessage(overrides: Partial<Message> = {}): Message {
  return {
    id: 'message-1',
    roomId: 'room-1',
    senderId: 'bob',
    senderName: 'Bob',
    content: 'hello',
    timestamp: 100,
    reactions: [],
    isDeleted: false,
    ...overrides
  };
}

describe('calculateUnreadForRoom', () => {
  it('ignores messages whose channel is not part of the room catalog', () => {
    const room = createRoom();
    const settings = {
      ...createDefaultNotificationSettings(),
      roomBaselines: { 'room-1': 0 }
    };
    const messages = [createMessage({ id: 'm-1', channelId: 'general', timestamp: 200 })];
    const counts = calculateUnreadForRoom(room, messages, settings, new Set());

    expect(counts.roomCount).toBe(0);
    expect(counts.channelCounts['general']).toBeUndefined();
  });

  it('counts messages for known text channels', () => {
    const room = createRoom();
    const settings = {
      ...createDefaultNotificationSettings(),
      roomBaselines: { 'room-1': 0 }
    };
    const messages = [createMessage({ id: 'm-1', channelId: 'text-1', timestamp: 200 })];
    const counts = calculateUnreadForRoom(room, messages, settings, new Set());

    expect(counts.roomCount).toBe(1);
    expect(counts.channelCounts['text-1']).toBe(1);
  });

  it('treats empty channel ids as the default channel when the room has no catalog', () => {
    const room = createRoom({ channels: [] });
    const settings = {
      ...createDefaultNotificationSettings(),
      roomBaselines: { 'room-1': 0 }
    };
    const messages = [createMessage({ id: 'm-1', channelId: undefined, timestamp: 200 })];
    const counts = calculateUnreadForRoom(room, messages, settings, new Set());

    expect(counts.roomCount).toBe(1);
    expect(counts.channelCounts['general']).toBe(1);
  });
});
