import { expect, tap } from '@git.zone/tstest/tapbundle';
import { Elkm1Client, Elkm1ConfigFlow, Elkm1Integration, Elkm1Mapper, HomeAssistantElkm1Integration, createElkm1DiscoveryDescriptor, elkm1Profile, type IElkm1Snapshot } from '../../ts/integrations/elkm1/index.js';

const rawData = {
  device: {
    id: 'elk-panel-1',
    name: 'ElkM1 Test Panel',
    serialNumber: '00409D123456',
  },
  entities: [
    { id: 'area_1', name: 'Area 1 Armed', platform: 'binary_sensor', state: false },
    { id: 'output_1', name: 'Output 1', platform: 'switch', state: true },
    { id: 'thermostat_temperature', name: 'Thermostat Temperature', platform: 'sensor', state: 21.5, unit: 'C' },
  ],
};

tap.test('matches manual Elk-M1 candidates and creates config flow output', async () => {
  const descriptor = createElkm1DiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'elkm1-manual-match');
  const result = await matcher!.matches({ host: 'elk-panel.local', port: 2601, name: 'Elk-M1 Control', metadata: { rawData } }, {});

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

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

  const done = await (await new Elkm1ConfigFlow().start(result.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.host).toEqual('elk-panel.local');
  expect(done.config?.port).toEqual(2601);
});

tap.test('maps Elk-M1 raw snapshots to runtime devices and entities', async () => {
  const client = new Elkm1Client({ name: 'ElkM1 Runtime', rawData });
  const snapshot = await client.getSnapshot();
  const devices = Elkm1Mapper.toDevices(snapshot);
  const entities = Elkm1Mapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('elkm1');
  expect(devices[0].manufacturer).toEqual('ELK Products, Inc.');
  expect(entities.length).toEqual(3);
});

tap.test('exposes Elk-M1 runtime services, HA alias, and executor-gated controls', async () => {
  const integration = new Elkm1Integration();
  const alias = new HomeAssistantElkm1Integration();
  expect(alias.domain).toEqual('elkm1');
  expect(integration.status).toEqual('control-runtime');
  expect(elkm1Profile.metadata.requirements).toEqual(['elkm1-lib==2.2.13']);
  expect(elkm1Profile.metadata.dependencies).toEqual(['network']);

  const runtime = await integration.setup({ name: 'ElkM1 Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'elkm1', service: 'status', target: {} });
  const snapshot = status.data as IElkm1Snapshot;

  expect(status.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('ElkM1 Test Panel');

  const command = await runtime.callService!({ domain: 'alarm_control_panel', service: 'alarm_arm_away', target: {}, data: { code: '1234' } });
  expect(command.success).toBeFalse();
  await runtime.destroy();
});

export default tap.start();
