import {
  describe,
  expect,
  it
} from 'vitest';

import {
  messageToRow,
  rowToMessage,
  rowToUser,
  userToRow
} from './mobile-sqlite-row-mapper.rules';

describe('mobile-sqlite-row-mapper.rules', () => {
  it('round-trips message fields including JSON metadata', () => {
    const message = {
      id: 'm1',
      roomId: 'r1',
      channelId: 'general',
      senderId: 'u1',
      senderName: 'Alice',
      content: 'hello',
      timestamp: 100,
      editedAt: 101,
      isDeleted: false,
      replyToId: 'm0',
      linkMetadata: [{ url: 'https://example.com', title: 'Example' }],
      kind: 'user' as const,
      reactions: []
    };
    const row = messageToRow(message);
    const restored = rowToMessage(row, [{ id: 'rx1', messageId: 'm1', oderId: 'u1', userId: 'u1', emoji: '👍', timestamp: 102 }]);

    expect(restored.id).toBe('m1');
    expect(restored.linkMetadata?.[0]?.title).toBe('Example');
    expect(restored.reactions).toHaveLength(1);
  });

  it('maps booleans to integers for SQLite storage', () => {
    const user = userToRow({
      id: 'u1',
      oderId: 'u1',
      username: 'alice',
      displayName: 'Alice',
      status: 'online',
      role: 'member',
      joinedAt: 1,
      isOnline: true,
      isAdmin: false,
      isRoomOwner: true,
      voiceState: { muted: false, deafened: false, speaking: false }
    });

    expect(user.isOnline).toBe(1);
    expect(user.isAdmin).toBe(0);
    expect(user.isRoomOwner).toBe(1);
    expect(user.voiceState).toContain('muted');

    const restored = rowToUser(user);

    expect(restored.isOnline).toBe(true);
    expect(restored.voiceState).toEqual({ muted: false, deafened: false, speaking: false });
  });
});
