import { expect, tap } from '@git.zone/tstest/tapbundle';
import { EnoceanClient, EnoceanConfigFlow, EnoceanIntegration, EnoceanMapper, HomeAssistantEnoceanIntegration, createEnoceanDiscoveryDescriptor, enoceanProfile, type IEnoceanSnapshot } from '../../ts/integrations/enocean/index.js';

const rawData = {
  gateway: {
    device: '/dev/ttyUSB0',
    manufacturer: 'EnOcean',
    model: 'USB 300',
    baseAddress: [0, 0, 0, 1],
  },
  telegrams: [
    { id: [1, 2, 3, 4], name: 'Window Handle', deviceClass: 'windowhandle', state: 'open' },
    { id: [5, 6, 7, 8], name: 'Room Temperature', deviceClass: 'temperature', state: 21.5 },
    { id: [9, 10, 11, 12], name: 'Relay Channel 0', platform: 'switch', state: true, channel: 0 },
  ],
};

tap.test('matches manual EnOcean candidates and creates config flow output', async () => {
  const descriptor = createEnoceanDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'enocean-manual-match');
  const result = await matcher!.matches({ name: 'EnOcean USB 300', metadata: { rawData } }, {});

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

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

  const done = await (await new EnoceanConfigFlow().start(result.candidate!, {})).submit!({ name: 'EnOcean USB 300' });
  expect(done.kind).toEqual('done');
  expect(done.config?.name).toEqual('EnOcean USB 300');
  expect(done.config?.transport).toEqual('snapshot');
});

tap.test('maps EnOcean raw snapshots to gateway devices and entities', async () => {
  const client = new EnoceanClient({ device: '/dev/ttyUSB0', name: 'EnOcean USB 300', rawData });
  const snapshot = await client.getSnapshot();
  const devices = EnoceanMapper.toDevices(snapshot);
  const entities = EnoceanMapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(devices[0].integrationDomain).toEqual('enocean');
  expect(devices[0].manufacturer).toEqual('EnOcean');
  expect(entities.length).toEqual(3);
  expect(entities[0].platform).toEqual('binary_sensor');
  expect(entities[0].state).toEqual(true);
  expect(entities[0].attributes?.deviceClass).toEqual('opening');
  expect(entities[2].platform).toEqual('switch');
});

tap.test('exposes EnOcean runtime, HA alias, and explicit unsupported control without executor', async () => {
  const integration = new EnoceanIntegration();
  const alias = new HomeAssistantEnoceanIntegration();
  expect(alias.domain).toEqual('enocean');
  expect(integration.status).toEqual('control-runtime');
  expect(enoceanProfile.metadata.dependencies).toEqual(['usb']);
  expect(enoceanProfile.metadata.requirements).toEqual(['enocean-async==0.4.2']);

  const runtime = await integration.setup({ device: '/dev/ttyUSB0', name: 'EnOcean USB 300', rawData }, {});
  const status = await runtime.callService!({ domain: 'enocean', service: 'status', target: {} });
  const snapshot = status.data as IEnoceanSnapshot;

  expect(status.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('EnOcean USB 300');

  const controlCommand = await runtime.callService!({ domain: 'switch', service: 'turn_on', target: {} });
  expect(controlCommand.success).toBeFalse();
  expect(controlCommand.error?.includes('requires an injected client.execute() or commandExecutor')).toBeTrue();
  await runtime.destroy();
});

export default tap.start();
