import { expect, tap } from '@git.zone/tstest/tapbundle';
import { DenonMapper, type IDenonSnapshot } from '../../ts/integrations/denon/index.js';

const snapshot: IDenonSnapshot = {
  receiverInfo: {
    host: '192.168.1.60',
    port: 23,
    name: 'Living Denon',
    manufacturer: 'Denon',
    modelName: 'DRA-N5',
    serialNumber: 'ABC12345',
  },
  state: {
    power: 'PWON',
    state: 'playing',
    volume: 27,
    volumeMax: 60,
    volumeLevel: 0.45,
    muted: false,
    source: 'SERVER',
    sourceName: 'Media server',
    sourceList: ['CD', 'Media server', 'TV'],
    mediaTitle: 'Track One\nArtist One',
    mediaMode: true,
  },
  sourceMap: { CD: 'CD', 'Media server': 'SERVER', TV: 'TV' },
  online: true,
  available: true,
  source: 'telnet',
};

tap.test('maps Denon snapshots to canonical devices', async () => {
  const devices = DenonMapper.toDevices(snapshot);

  expect(devices[0].id).toEqual('denon.receiver.abc12345');
  expect(devices[0].manufacturer).toEqual('Denon');
  expect(devices[0].state.some((stateArg) => stateArg.featureId === 'source' && stateArg.value === 'Media server')).toBeTrue();
  expect(devices[0].state.some((stateArg) => stateArg.featureId === 'volume' && stateArg.value === 45)).toBeTrue();
});

tap.test('maps Denon snapshots to media player, sensor, and mute entities', async () => {
  const entities = DenonMapper.toEntities(snapshot);
  const media = entities.find((entityArg) => entityArg.id === 'media_player.living_denon');
  const source = entities.find((entityArg) => entityArg.id === 'sensor.living_denon_source');
  const mute = entities.find((entityArg) => entityArg.id === 'switch.living_denon_mute');

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

export default tap.start();
