import { expect, tap } from '@git.zone/tstest/tapbundle';
import { AndroidtvRemoteIntegration, type IAndroidtvRemoteCommand, type IAndroidtvRemoteConfig } from '../../ts/integrations/androidtv_remote/index.js';

const baseConfig = (commandsArg: IAndroidtvRemoteCommand[] = []): IAndroidtvRemoteConfig => ({
  host: '192.168.1.61',
  snapshot: {
    deviceInfo: {
      name: 'Living Room Google TV',
      host: '192.168.1.61',
      macAddress: 'AA:BB:CC:DD:EE:FF',
    },
    state: {
      available: true,
      isOn: false,
      currentApp: 'com.google.android.tvlauncher',
      volumeInfo: {
        level: 5,
        max: 10,
        muted: false,
      },
    },
    apps: [
      { id: 'com.netflix.ninja' },
      { id: 'com.google.android.tvlauncher', name: 'Launcher' },
    ],
  },
  executor: async (commandArg) => {
    commandsArg.push(commandArg);
  },
});

tap.test('models media, app, and remote commands through injected executor', async () => {
  const commands: IAndroidtvRemoteCommand[] = [];
  const runtime = await new AndroidtvRemoteIntegration().setup(baseConfig(commands), {});

  expect((await runtime.callService?.({ domain: 'media_player', service: 'turn_on', target: {} }))?.success).toBeTrue();
  expect(commands[0].action).toEqual('key_command');
  expect(commands[0].reason).toEqual('turn_on');
  expect(commands[0].keyCode).toEqual('POWER');
  expect(commands[0].direction).toEqual('SHORT');

  expect((await runtime.callService?.({ domain: 'media_player', service: 'select_source', target: {}, data: { source: 'Netflix' } }))?.success).toBeTrue();
  expect(commands[1].action).toEqual('launch_app');
  expect(commands[1].reason).toEqual('select_activity');
  expect(commands[1].appId).toEqual('com.netflix.ninja');

  expect((await runtime.callService?.({ domain: 'remote', service: 'send_command', target: {}, data: { command: ['left', 'center'], num_repeats: 2, hold_secs: 0.25 } }))?.success).toBeTrue();
  expect(commands[2].action).toEqual('remote_send_command');
  expect(commands[2].repeats).toEqual(2);
  expect(commands[2].holdSecs).toEqual(0.25);
  expect(commands[2].keys?.map((keyArg) => keyArg.keyCode)).toEqual(['DPAD_LEFT', 'DPAD_LEFT', 'DPAD_CENTER', 'DPAD_CENTER']);
  expect(commands[2].keys?.map((keyArg) => keyArg.direction)).toEqual(['START_LONG', 'END_LONG', 'START_LONG', 'END_LONG']);

  expect((await runtime.callService?.({ domain: 'androidtv_remote', service: 'finish_pairing', target: {}, data: { pin: '123456' } }))?.success).toBeTrue();
  expect(commands[3].action).toEqual('finish_pairing');
  expect(commands[3].reason).toEqual('finish_pairing');
  expect(commands[3].pin).toEqual('123456');
});

tap.test('returns explicit unsupported errors without an executor', async () => {
  const runtime = await new AndroidtvRemoteIntegration().setup({ host: '192.168.1.61' }, {});
  const result = await runtime.callService?.({ domain: 'media_player', service: 'volume_up', target: {} });

  expect(result?.success).toBeFalse();
  expect(result?.error).toContain('requires an injected executor');
});

export default tap.start();
