import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HarmonyClient, HarmonyMapper, type IHarmonyConfig } from '../../ts/integrations/harmony/index.js';

const config: IHarmonyConfig = {
  host: '192.168.1.40',
  name: 'Living Room Harmony',
  remoteId: '123456789',
  state: {
    available: true,
    currentActivity: 'Watch TV',
    lastActivity: 'Listen to Music',
  },
  hubConfig: {
    global: {
      timeStampHash: 'abc;123456789',
    },
    activity: [
      { id: '-1', label: 'PowerOff' },
      { id: '111', label: 'Watch TV' },
      { id: '222', label: 'Listen to Music' },
    ],
    device: [
      {
        id: '333',
        label: 'Television',
        manufacturer: 'Sony',
        model: 'TV',
        controlGroup: [
          { name: 'Volume', function: [{ name: 'VolumeUp' }, { name: 'VolumeDown' }] },
        ],
      },
    ],
  },
};

tap.test('normalizes upstream-shaped hub config snapshots', async () => {
  const snapshot = await new HarmonyClient(config).getSnapshot();

  expect(snapshot.deviceInfo.name).toEqual('Living Room Harmony');
  expect(snapshot.activities.map((activityArg) => activityArg.label)).toEqual(['PowerOff', 'Watch TV', 'Listen to Music']);
  expect(snapshot.devices[0].commands?.map((commandArg) => commandArg.name)).toEqual(['VolumeUp', 'VolumeDown']);
  expect(snapshot.state.currentActivityId).toEqual('111');
});

tap.test('maps Harmony snapshots to media and select entities', async () => {
  const snapshot = await new HarmonyClient(config).getSnapshot();
  const devices = HarmonyMapper.toDevices(snapshot);
  const entities = HarmonyMapper.toEntities(snapshot);

  expect(devices[0].id).toEqual('harmony.device.123456789');
  expect(devices[0].online).toBeTrue();
  expect(devices[0].state.some((stateArg) => stateArg.featureId === 'activity' && stateArg.value === 'Watch TV')).toBeTrue();
  expect((devices[0].metadata?.activities as string[]).includes('Listen to Music')).toBeTrue();

  expect(entities[0].platform).toEqual('media_player');
  expect(entities[0].state).toEqual('on');
  expect(entities[0].attributes?.currentActivity).toEqual('Watch TV');
  expect(entities[1].platform).toEqual('select');
  expect(entities[1].state).toEqual('Watch TV');
  expect(entities[1].attributes?.options).toEqual(['power_off', 'Listen to Music', 'Watch TV']);
});

export default tap.start();
