import { expect, tap } from '@git.zone/tstest/tapbundle';
import { SoundtouchMapper, type ISoundtouchSnapshot } from '../../ts/integrations/soundtouch/index.js';

const snapshot: ISoundtouchSnapshot = {
  config: {
    deviceId: 'DEVICE-123',
    name: 'Living Room',
    type: 'SoundTouch 20',
    host: '192.168.1.50',
    port: 8090,
    networks: [{ type: 'SMSC', macAddress: '00:11:22:33:44:55', ipAddress: '192.168.1.50' }],
    components: [{ category: 'PRODUCT', softwareVersion: '27.0.1', serialNumber: 'B123' }],
  },
  status: {
    source: 'INTERNET_RADIO',
    playStatus: 'PLAY_STATE',
    stationName: 'Jazz Radio',
    track: 'Example Track',
    artist: 'Example Artist',
    album: 'Example Album',
    duration: 180,
    position: 30,
    contentItem: { source: 'INTERNET_RADIO', type: 'uri', location: 'station-1', name: 'Jazz Radio', isPresetable: true },
  },
  volume: { actual: 42, target: 42, muted: false },
  presets: [{ presetId: '1', name: 'Jazz Radio', source: 'INTERNET_RADIO', type: 'uri', location: 'station-1', isPresetable: true }],
  sourceList: ['AUX', 'BLUETOOTH'],
  zone: { masterId: 'DEVICE-123', isMaster: true, slaves: [{ deviceId: 'DEVICE-456', ipAddress: '192.168.1.51', role: 'SLAVE' }] },
  available: true,
};

tap.test('maps SoundTouch snapshots to canonical media devices', async () => {
  const devices = SoundtouchMapper.toDevices(snapshot);
  expect(devices[0].id).toEqual('soundtouch.player.device_123');
  expect(devices[0].manufacturer).toEqual('Bose Corporation');
  expect(devices[0].features.some((featureArg) => featureArg.id === 'presets')).toBeTrue();
  expect(devices[0].state.some((stateArg) => stateArg.featureId === 'volume' && stateArg.value === 42)).toBeTrue();
});

tap.test('maps SoundTouch media player presets sources and media entities', async () => {
  const entities = SoundtouchMapper.toEntities(snapshot);
  const player = entities.find((entityArg) => entityArg.id === 'media_player.living_room');
  const presets = entities.find((entityArg) => entityArg.id === 'sensor.living_room_soundtouch_presets');
  const sources = entities.find((entityArg) => entityArg.id === 'sensor.living_room_soundtouch_sources');
  const media = entities.find((entityArg) => entityArg.id === 'sensor.living_room_soundtouch_media');

  expect(player?.state).toEqual('playing');
  expect(player?.attributes?.volumeLevel).toEqual(0.42);
  expect(player?.attributes?.source).toEqual('INTERNET_RADIO');
  expect(player?.attributes?.mediaTitle).toEqual('Jazz Radio');
  expect(player?.attributes?.soundtouchGroup).toEqual(['DEVICE-123', 'DEVICE-456']);
  expect(presets?.state).toEqual(1);
  expect(sources?.state).toEqual(2);
  expect(media?.state).toEqual('Jazz Radio');
});

export default tap.start();
