import { expect, tap } from '@git.zone/tstest/tapbundle';
import { DenonavrMapper, type IDenonavrSnapshot } from '../../ts/integrations/denonavr/index.js';

const snapshot: IDenonavrSnapshot = {
  receiverInfo: {
    host: '192.168.1.50',
    port: 80,
    name: 'Living Room AVR',
    manufacturer: 'Denon',
    modelName: 'AVR-X1700H',
    serialNumber: 'ABC12345',
    receiverType: 'avr-x',
  },
  zones: [{
    zone: 'Main',
    name: 'Main Zone',
    power: 'ON',
    state: 'playing',
    volumeDb: -35,
    muted: false,
    source: 'Media Server',
    sourceList: ['TV', 'Media Server', 'Bluetooth'],
    soundMode: 'DOLBY DIGITAL',
    soundModeRaw: 'DOLBY AUDIO - DOLBY DIGITAL',
    dynamicEq: true,
    ecoMode: 'Auto',
    media: {
      title: 'Track One',
      artist: 'Artist One',
      album: 'Album One',
      contentType: 'music',
    },
    available: true,
  }, {
    zone: 'Zone2',
    name: 'Patio',
    power: 'STANDBY',
    state: 'off',
    volumeLevel: 0.25,
    muted: true,
    source: 'Tuner',
    available: true,
  }],
};

tap.test('maps Denon AVR snapshots to canonical devices', async () => {
  const devices = DenonavrMapper.toDevices(snapshot);
  expect(devices[0].id).toEqual('denonavr.receiver.abc12345');
  expect(devices[0].manufacturer).toEqual('Denon');
  expect(devices[0].state.some((stateArg) => stateArg.featureId === 'main_source' && stateArg.value === 'Media Server')).toBeTrue();
  expect(devices[0].state.some((stateArg) => stateArg.featureId === 'zone2_power' && stateArg.value === 'off')).toBeTrue();
});

tap.test('maps Denon AVR zones to media, sensor, and switch entities', async () => {
  const entities = DenonavrMapper.toEntities(snapshot);
  const media = entities.find((entityArg) => entityArg.id === 'media_player.living_room_avr');
  const source = entities.find((entityArg) => entityArg.id === 'sensor.living_room_avr_source');
  const mute = entities.find((entityArg) => entityArg.id === 'switch.living_room_avr_zone2_mute');
  const dynamicEq = entities.find((entityArg) => entityArg.id === 'switch.living_room_avr_dynamic_eq');

  expect(media?.platform).toEqual('media_player');
  expect(media?.state).toEqual('playing');
  expect(media?.attributes?.volumeLevel).toEqual(0.45);
  expect(source?.state).toEqual('Media Server');
  expect(mute?.state).toEqual(true);
  expect(dynamicEq?.state).toEqual(true);
});

export default tap.start();
