import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HyperionMapper, type IHyperionRawData } from '../../ts/integrations/hyperion/index.js';

const rawData: IHyperionRawData = {
  sysInfo: { hyperion: { id: 'hyperion-uuid', version: '2.0.17' } },
  serverInfo: {
    hostname: 'hyperion-host',
    instance: [{ instance: 0, friendly_name: 'Living Hyperion', running: true }],
    components: [{ name: 'ALL', enabled: true }, { name: 'LEDDEVICE', enabled: true }],
    effects: [{ name: 'Rainbow swirl' }, { name: 'Warm mood blobs' }],
    adjustment: [{ id: 'default', brightness: 50 }],
    priorities: [{ priority: 128, componentId: 'EFFECT', origin: 'smarthome.exchange', owner: 'Rainbow swirl', visible: true }],
  },
};

tap.test('maps Hyperion snapshots to devices and entities', async () => {
  const snapshot = HyperionMapper.toSnapshot({ config: { host: 'hyperion.local', priority: 128 }, rawData, online: true, source: 'manual' });
  const devices = HyperionMapper.toDevices(snapshot);
  const entities = HyperionMapper.toEntities(snapshot);
  const light = entities.find((entityArg) => entityArg.platform === 'light');
  const visiblePriority = entities.find((entityArg) => entityArg.id.includes('visible_priority'));
  const ledSwitch = entities.find((entityArg) => entityArg.id.includes('component_leddevice'));

  expect(devices[0].id).toEqual('hyperion.instance.hyperion_uuid.0');
  expect(devices[0].online).toBeTrue();
  expect(light?.state).toEqual('on');
  expect(light?.attributes?.effect).toEqual('Rainbow swirl');
  expect(light?.attributes?.brightness).toEqual(128);
  expect(visiblePriority?.state).toEqual('Rainbow swirl');
  expect(ledSwitch?.state).toEqual('on');
});

tap.test('maps Home Assistant-style services to Hyperion JSON commands', async () => {
  const snapshot = HyperionMapper.toSnapshot({ config: { host: 'hyperion.local', priority: 128 }, rawData, online: true, source: 'manual' });
  const lightCommands = HyperionMapper.commandsForService(snapshot, { domain: 'light', service: 'turn_on', target: { entityId: 'light.living_hyperion_light' }, data: { rgb_color: [10, 20, 30] } });
  const switchCommands = HyperionMapper.commandsForService(snapshot, { domain: 'switch', service: 'turn_off', target: { entityId: 'switch.living_hyperion_component_leddevice' } });
  const snapshotCommand = HyperionMapper.commandForService(snapshot, { domain: 'hyperion', service: 'get_image_snapshot', target: {}, data: { instance: 0, format: 'JPG' } });

  expect(lightCommands[0].action).toEqual('set_color');
  expect(lightCommands[0].color).toEqual([10, 20, 30]);
  expect(switchCommands[0].action).toEqual('set_component');
  expect(switchCommands[0].component).toEqual('LEDDEVICE');
  expect(snapshotCommand?.action).toEqual('get_image_snapshot');
  expect(snapshotCommand?.format).toEqual('JPG');
});

export default tap.start();
