import { expect, tap } from '@git.zone/tstest/tapbundle';
import { Edl21Client, Edl21ConfigFlow, Edl21Integration, Edl21Mapper, HomeAssistantEdl21Integration, createEdl21DiscoveryDescriptor, edl21Profile, type IEdl21Snapshot } from '../../ts/integrations/edl21/index.js';

const rawData = {
  serverId: '01 23 45 67 89 AB',
  valList: [
    { objName: '1-0:1.7.0*255', value: 512, unit: 'W' },
    { objName: '1-0:1.8.0*255', value: 12345, unit: 'Wh' },
    { objName: '1-0:96.50.1*1', value: 'ignored' },
  ],
};

tap.test('matches manual EDL21 candidates and creates config flow output', async () => {
  const descriptor = createEdl21DiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'edl21-manual-match');
  const result = await matcher!.matches({ name: 'EDL21 Smart Meter', metadata: { rawData, serial_port: '/dev/ttyUSB0' } }, {});

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

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

  const done = await (await new Edl21ConfigFlow().start(result.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.rawData).toEqual(rawData);
});

tap.test('maps EDL21 raw telegram snapshots to smart meter devices and entities', async () => {
  const client = new Edl21Client({ name: 'Utility Meter', serialPort: '/dev/ttyUSB0', rawData });
  const snapshot = await client.getSnapshot();
  const devices = Edl21Mapper.toDevices(snapshot);
  const entities = Edl21Mapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(devices[0].integrationDomain).toEqual('edl21');
  expect(devices[0].model).toEqual('Smart Meter');
  expect(entities.length).toEqual(2);
  expect(entities[0].attributes?.unit).toEqual('W');
  expect(entities[1].attributes?.deviceClass).toEqual('energy');
});

tap.test('exposes EDL21 read-only runtime, HA alias, and explicit unsupported control without executor', async () => {
  expect(new HomeAssistantEdl21Integration().domain).toEqual('edl21');
  expect(edl21Profile.status).toEqual('read-only-runtime');
  expect(edl21Profile.metadata.configFlow).toEqual(true);
  expect(edl21Profile.metadata.requirements).toEqual(['pysml==0.1.5']);

  const runtime = await new Edl21Integration().setup({ name: 'Utility Meter', rawData }, {});
  const status = await runtime.callService!({ domain: 'edl21', service: 'status', target: {} });
  const snapshot = status.data as IEdl21Snapshot;

  expect(status.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('Utility Meter');

  const controlCommand = await runtime.callService!({ domain: 'edl21', 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();
