import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HeosIntegration, type IHeosRawCommandRequest, type IHeosSnapshot } from '../../ts/integrations/heos/index.js';

const snapshot: IHeosSnapshot = {
  system: { host: '192.168.1.80', currentHost: '192.168.1.80' },
  players: [{
    name: 'Living Room',
    playerId: 101,
    model: 'Denon HEOS 7',
    state: 'pause',
    volume: 20,
    groupId: 201,
    available: true,
  }, {
    name: 'Kitchen',
    playerId: 102,
    model: 'Denon HEOS 5',
    state: 'pause',
    volume: 30,
    groupId: 201,
    available: true,
  }],
  groups: [{ name: 'Downstairs', groupId: 201, leadPlayerId: 101, memberPlayerIds: [102] }],
  favorites: {
    1: { sourceId: 1028, name: 'Jazz Radio', type: 'station', mediaId: 'fav-jazz', playable: true },
  },
  inputSources: [{ sourceId: 1027, name: 'HDMI ARC', type: 'station', mediaId: 'inputs/hdmi_arc_1', playable: true }],
};

tap.test('models HEOS media player commands through an executor', async () => {
  const commands: IHeosRawCommandRequest[] = [];
  const runtime = await new HeosIntegration().setup({
    snapshot,
    commandExecutor: {
      execute: async (requestArg) => {
        commands.push(requestArg);
        return { command: requestArg.command, result: true, message: {} };
      },
    },
  }, {});

  const play = await runtime.callService!({ domain: 'media_player', service: 'media_play', target: { entityId: 'media_player.living_room' } });
  const volume = await runtime.callService!({ domain: 'media_player', service: 'volume_set', target: { entityId: 'media_player.living_room' }, data: { volume_level: 0.35 } });
  const source = await runtime.callService!({ domain: 'media_player', service: 'select_source', target: { entityId: 'media_player.living_room' }, data: { source: 'Jazz Radio' } });
  const join = await runtime.callService!({ domain: 'media_player', service: 'join', target: { entityId: 'media_player.living_room' }, data: { group_members: ['media_player.kitchen'] } });
  const groupVolume = await runtime.callService!({ domain: 'heos', service: 'group_volume_set', target: { entityId: 'media_player.living_room' }, data: { volume_level: 0.5 } });

  expect(play.success).toBeTrue();
  expect(volume.success).toBeTrue();
  expect(source.success).toBeTrue();
  expect(join.success).toBeTrue();
  expect(groupVolume.success).toBeTrue();
  expect(commands.map((commandArg) => commandArg.command)).toEqual([
    'player/set_play_state',
    'player/set_volume',
    'browse/play_preset',
    'group/set_group',
    'group/set_volume',
  ]);
  expect(commands[0].parameters).toEqual({ pid: 101, state: 'play' });
  expect(commands[1].parameters).toEqual({ pid: 101, level: 35 });
  expect(commands[2].parameters).toEqual({ pid: 101, preset: 1 });
  expect(commands[3].parameters).toEqual({ pid: '101,102' });
  expect(commands[4].parameters).toEqual({ gid: 201, level: 50 });
});

tap.test('does not report live command success for static snapshots without transport', async () => {
  const runtime = await new HeosIntegration().setup({ snapshot }, {});
  const result = await runtime.callService!({ domain: 'media_player', service: 'media_play', target: { entityId: 'media_player.living_room' } });
  expect(result.success).toBeFalse();
  expect(result.error?.includes('config.host or commandExecutor')).toBeTrue();
});

export default tap.start();
