import { expect, tap } from '@git.zone/tstest/tapbundle';
import { ArrisTg2492lgClient, ArrisTg2492lgConfigFlow, ArrisTg2492lgIntegration, ArrisTg2492lgMapper, createArrisTg2492lgDiscoveryDescriptor, type IArrisTg2492lgSnapshot } from '../../ts/integrations/arris_tg2492lg/index.js';

const rawData = {
  "clients": 15,
  "wan_up": true,
  "wifi_enabled": true
};

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

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

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

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

tap.test('maps Arris TG2492LG raw snapshots to runtime devices and entities', async () => {
  const client = new ArrisTg2492lgClient({ name: 'Arris TG2492LG Test', rawData });
  const snapshot = await client.getSnapshot();
  const devices = ArrisTg2492lgMapper.toDevices(snapshot);
  const entities = ArrisTg2492lgMapper.toEntities(snapshot);

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

tap.test('exposes Arris TG2492LG runtime services without fake live control', async () => {
  const runtime = await new ArrisTg2492lgIntegration().setup({ name: 'Arris TG2492LG Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'arris_tg2492lg', service: 'status', target: {} });
  const snapshot = status.data as IArrisTg2492lgSnapshot;

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

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

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