import { expect, tap } from '@git.zone/tstest/tapbundle';
import { SqueezeboxMapper, type ISqueezeboxSnapshot } from '../../ts/integrations/squeezebox/index.js';

const snapshot: ISqueezeboxSnapshot = {
  server: {
    id: 'lms-1',
    uuid: 'server-uuid-1',
    name: 'Home LMS',
    host: '192.168.1.40',
    version: '8.5.0',
    playerCount: 2,
    stats: { totalSongs: 1200, totalAlbums: 100 },
  },
  players: [{
    playerId: '00:04:20:aa:bb:01',
    name: 'Living Room',
    model: 'Squeezebox Radio',
    firmware: '8.5.0',
    connected: true,
    power: true,
    mode: 'play',
    volume: 45,
    muting: false,
    repeat: 'playlist',
    shuffle: 'song',
    title: 'Example Track',
    artist: 'Example Artist',
    album: 'Example Album',
    url: 'http://radio.example/stream.mp3',
    duration: 180,
    time: 30,
    syncGroup: ['00:04:20:aa:bb:02'],
  }, {
    playerId: '00:04:20:aa:bb:02',
    name: 'Kitchen',
    model: 'Squeezebox Touch',
    connected: true,
    power: true,
    mode: 'pause',
    volume: 25,
    muting: true,
    syncGroup: ['00:04:20:aa:bb:01'],
  }],
  favorites: [{
    id: 'fav-1',
    name: 'Jazz Radio',
    url: 'http://radio.example/stream.mp3',
    type: 'audio',
    playable: true,
  }],
  syncGroups: [{
    id: 'sync-main',
    name: 'Downstairs',
    leaderPlayerId: '00:04:20:aa:bb:01',
    playerIds: ['00:04:20:aa:bb:01', '00:04:20:aa:bb:02'],
  }],
  online: true,
  source: 'snapshot',
};

tap.test('maps Squeezebox server and players to devices', async () => {
  const devices = SqueezeboxMapper.toDevices(snapshot);
  const server = devices.find((deviceArg) => deviceArg.id === 'squeezebox.server.server_uuid_1');
  const player = devices.find((deviceArg) => deviceArg.id === 'squeezebox.player.00_04_20_aa_bb_01');
  expect(server?.state.some((stateArg) => stateArg.featureId === 'favorites' && stateArg.value === 1)).toBeTrue();
  expect(player?.manufacturer).toEqual('Logitech');
  expect(player?.state.some((stateArg) => stateArg.featureId === 'source' && stateArg.value === 'Jazz Radio')).toBeTrue();
});

tap.test('maps Squeezebox media entities favorites and sync groups', async () => {
  const entities = SqueezeboxMapper.toEntities(snapshot);
  const player = entities.find((entityArg) => entityArg.id === 'media_player.living_room');
  const favorites = entities.find((entityArg) => entityArg.id === 'sensor.home_lms_favorites');
  const syncGroups = entities.find((entityArg) => entityArg.id === 'sensor.home_lms_sync_groups');
  const media = entities.find((entityArg) => entityArg.id === 'sensor.living_room_squeezebox_media');

  expect(player?.state).toEqual('playing');
  expect(player?.attributes?.volumeLevel).toEqual(0.45);
  expect(player?.attributes?.source).toEqual('Jazz Radio');
  expect(player?.attributes?.groupMembers).toEqual(['media_player.living_room', 'media_player.kitchen']);
  expect(player?.attributes?.repeat).toEqual('all');
  expect(favorites?.state).toEqual(1);
  expect(syncGroups?.state).toEqual(1);
  expect(media?.state).toEqual('Example Track');
});

export default tap.start();
