import { expect, tap } from '@git.zone/tstest/tapbundle';
import { VlcTelnetClient, VlcTelnetConfigFlow, VlcTelnetIntegration, VlcTelnetMapper, createVlcTelnetDiscoveryDescriptor, type IVlcTelnetSnapshot } from '../../ts/integrations/vlc_telnet/index.js';

const rawData = {
  "state": "playing",
  "volume": 256,
  "title": "Sample"
};

tap.test('matches manual VLC media player via Telnet candidates and creates config flow output', async () => {
  const descriptor = createVlcTelnetDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'vlc_telnet-manual-match');
  const result = await matcher!.matches({ host: 'vlc_telnet.local', name: 'VLC media player via Telnet', metadata: { rawData } }, {});

  expect(result.matched).toBeTrue();
  expect(result.candidate?.integrationDomain).toEqual('vlc_telnet');

  const validation = await descriptor.getValidators()[0].validate(result.candidate!, {});
  expect(validation.matched).toBeTrue();

  const done = await (await new VlcTelnetConfigFlow().start(result.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.host).toEqual('vlc_telnet.local');
});

tap.test('maps VLC media player via Telnet raw snapshots to runtime devices and entities', async () => {
  const client = new VlcTelnetClient({ name: 'VLC media player via Telnet Test', rawData });
  const snapshot = await client.getSnapshot();
  const devices = VlcTelnetMapper.toDevices(snapshot);
  const entities = VlcTelnetMapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('vlc_telnet');
  expect(entities.length > 0).toBeTrue();
});

tap.test('exposes VLC media player via Telnet runtime services without fake live control', async () => {
  const runtime = await new VlcTelnetIntegration().setup({ name: 'VLC media player via Telnet Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'vlc_telnet', service: 'status', target: {} });
  const snapshot = status.data as IVlcTelnetSnapshot;

  expect(status.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('VLC media player via Telnet Runtime');

  const liveCommand = await runtime.callService!({ domain: 'vlc_telnet', service: 'turn_on', target: {} });
  expect(liveCommand.success).toBeFalse();
  await runtime.destroy();

  const executorRuntime = await new VlcTelnetIntegration().setup({
    name: 'VLC media player via Telnet Executor',
    rawData,
    commandExecutor: {
      execute: async (requestArg) => ({ success: true, data: { service: requestArg.service } }),
    },
  }, {});
  const executed = await executorRuntime.callService!({ domain: 'vlc_telnet', service: 'turn_on', target: {} });
  expect(executed.success).toBeTrue();
  expect((executed.data as { service: string }).service).toEqual('turn_on');
  await executorRuntime.destroy();
});

export default tap.start();
