import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HeosMapper, type IHeosSnapshot } from '../../ts/integrations/heos/index.js';

const snapshot: IHeosSnapshot = {
  system: {
    host: '192.168.1.80',
    currentHost: '192.168.1.80',
    signedInUsername: 'listener@example.com',
    isSignedIn: true,
  },
  players: [{
    name: 'Living Room',
    playerId: 101,
    model: 'Denon HEOS 7',
    serial: 'LR123',
    version: '3.40.0',
    ipAddress: '192.168.1.80',
    network: 'wired',
    state: 'play',
    volume: 45,
    muted: false,
    repeat: 'on_all',
    shuffle: true,
    groupId: 201,
    available: true,
    nowPlayingMedia: {
      type: 'station',
      station: 'Jazz Radio',
      albumId: 'fav-jazz',
      mediaId: 'station-1',
      sourceId: 3,
      duration: 180000,
      currentPosition: 30000,
      supportedControls: ['play', 'pause', 'stop'],
    },
  }, {
    name: 'Kitchen',
    playerId: 102,
    model: 'Denon HEOS 5',
    serial: 'KT456',
    version: '3.40.0',
    ipAddress: '192.168.1.81',
    network: 'wifi',
    state: 'pause',
    volume: 25,
    muted: true,
    groupId: 201,
    available: true,
    nowPlayingMedia: {
      type: 'station',
      station: 'HDMI ARC',
      mediaId: 'inputs/hdmi_arc_1',
      sourceId: 1027,
    },
  }],
  groups: [{
    name: 'Living Room + Kitchen',
    groupId: 201,
    leadPlayerId: 101,
    memberPlayerIds: [102],
    volume: 40,
    muted: false,
  }],
  favorites: {
    1: { sourceId: 1028, name: 'Jazz Radio', type: 'station', mediaId: 'fav-jazz', playable: true },
  },
  inputSources: [{
    sourceId: 1027,
    name: 'HDMI ARC',
    type: 'station',
    mediaId: 'inputs/hdmi_arc_1',
    playable: true,
  }],
};

tap.test('maps HEOS players and system snapshot to devices', async () => {
  const devices = HeosMapper.toDevices(snapshot);
  expect(devices.some((deviceArg) => deviceArg.id === 'heos.system.192_168_1_80')).toBeTrue();
  const player = devices.find((deviceArg) => deviceArg.id === 'heos.player.101');
  expect(player?.manufacturer).toEqual('Denon');
  expect(player?.state.some((stateArg) => stateArg.featureId === 'source' && stateArg.value === 'Jazz Radio')).toBeTrue();
});

tap.test('maps HEOS players groups sources and media entities', async () => {
  const entities = HeosMapper.toEntities(snapshot);
  const player = entities.find((entityArg) => entityArg.id === 'media_player.living_room');
  const kitchen = entities.find((entityArg) => entityArg.id === 'media_player.kitchen');
  const sources = entities.find((entityArg) => entityArg.id === 'sensor.heos_system_sources');
  const groups = entities.find((entityArg) => entityArg.id === 'sensor.heos_system_groups');
  const media = entities.find((entityArg) => entityArg.id === 'sensor.living_room_heos_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?.mediaDuration).toEqual(180);
  expect(player?.attributes?.mediaPosition).toEqual(30);
  expect(kitchen?.attributes?.source).toEqual('HDMI ARC');
  expect(sources?.state).toEqual(2);
  expect(groups?.state).toEqual(1);
  expect(media?.state).toEqual('Jazz Radio');
});

export default tap.start();
