import { expect, tap } from '@git.zone/tstest/tapbundle';
import { ZiggoMediaboxXlClient, ZiggoMediaboxXlConfigFlow, ZiggoMediaboxXlIntegration, ZiggoMediaboxXlMapper, createZiggoMediaboxXlDiscoveryDescriptor, type IZiggoMediaboxXlSnapshot } from '../../ts/integrations/ziggo_mediabox_xl/index.js';

const rawData = {
  "power": true,
  "channel": "NPO 1",
  "volume": 16
};

tap.test('matches manual Ziggo Mediabox XL candidates and creates config flow output', async () => {
  const descriptor = createZiggoMediaboxXlDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'ziggo_mediabox_xl-manual-match');
  const result = await matcher!.matches({ host: 'ziggo_mediabox_xl.local', name: 'Ziggo Mediabox XL', metadata: { rawData } }, {});

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

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

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

tap.test('maps Ziggo Mediabox XL raw snapshots to runtime devices and entities', async () => {
  const client = new ZiggoMediaboxXlClient({ name: 'Ziggo Mediabox XL Test', rawData });
  const snapshot = await client.getSnapshot();
  const devices = ZiggoMediaboxXlMapper.toDevices(snapshot);
  const entities = ZiggoMediaboxXlMapper.toEntities(snapshot);

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

tap.test('exposes Ziggo Mediabox XL runtime services without fake live control', async () => {
  const runtime = await new ZiggoMediaboxXlIntegration().setup({ name: 'Ziggo Mediabox XL Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'ziggo_mediabox_xl', service: 'status', target: {} });
  const snapshot = status.data as IZiggoMediaboxXlSnapshot;

  expect(status.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('Ziggo Mediabox XL Runtime');

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

  const executorRuntime = await new ZiggoMediaboxXlIntegration().setup({
    name: 'Ziggo Mediabox XL Executor',
    rawData,
    commandExecutor: {
      execute: async (requestArg) => ({ success: true, data: { service: requestArg.service } }),
    },
  }, {});
  const executed = await executorRuntime.callService!({ domain: 'ziggo_mediabox_xl', 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();
