import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HarmanKardonAvrMapper, type IHarmanKardonAvrSnapshot } from '../../ts/integrations/harman_kardon_avr/index.js';

const snapshot: IHarmanKardonAvrSnapshot = {
  deviceInfo: {
    host: '192.168.1.32',
    port: 10025,
    name: 'Living AVR',
    manufacturer: 'Harman Kardon',
    model: 'AVR-151S',
    serialNumber: 'HK12345',
    zone: 'Main Zone',
  },
  state: {
    power: 'ON',
    state: 'on',
    muted: true,
    source: 'TV',
    sourceList: ['Disc', 'TV', 'USB'],
    supportedFeatures: ['volume_step', 'volume_mute', 'turn_on', 'turn_off', 'select_source'],
  },
  online: true,
  available: true,
  source: 'tcp',
};

tap.test('maps Harman Kardon AVR snapshots to canonical devices', async () => {
  const devices = HarmanKardonAvrMapper.toDevices(snapshot);

  expect(devices[0].id).toEqual('harman_kardon_avr.receiver.hk12345');
  expect(devices[0].manufacturer).toEqual('Harman Kardon');
  expect(devices[0].state.some((stateArg) => stateArg.featureId === 'power' && stateArg.value === 'on')).toBeTrue();
  expect(devices[0].state.some((stateArg) => stateArg.featureId === 'source' && stateArg.value === 'TV')).toBeTrue();
});

tap.test('maps Harman Kardon AVR snapshots to media player, source sensor, and mute switch entities', async () => {
  const entities = HarmanKardonAvrMapper.toEntities(snapshot);
  const media = entities.find((entityArg) => entityArg.id === 'media_player.living_avr');
  const source = entities.find((entityArg) => entityArg.id === 'sensor.living_avr_source');
  const mute = entities.find((entityArg) => entityArg.id === 'switch.living_avr_mute');

  expect(media?.platform).toEqual('media_player');
  expect(media?.state).toEqual('on');
  expect(media?.attributes?.source).toEqual('TV');
  expect(media?.attributes?.isVolumeMuted).toBeTrue();
  expect(source?.state).toEqual('TV');
  expect(mute?.state).toBeTrue();
});

export default tap.start();
