import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HunterDouglasPowerViewConfigFlow, HunterDouglasPowerViewMapper, createHunterDouglasPowerViewDiscoveryDescriptor } from '../../ts/integrations/hunterdouglas_powerview/index.js';

tap.test('matches PowerView zeroconf records from the Home Assistant manifest', async () => {
  const descriptor = createHunterDouglasPowerViewDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'hunterdouglas_powerview-zeroconf-match');
  const g2 = await matcher!.matches({
    name: 'PowerView Hub._powerview._tcp.local.',
    type: '_powerview._tcp.local.',
    host: 'powerview.local',
    port: 80,
    txt: { serial: 'PV123' },
  }, {});
  const g3 = await matcher!.matches({
    name: 'PV G3._PowerView-G3._tcp.local.',
    serviceType: '_PowerView-G3._tcp.local.',
    addresses: ['192.168.1.50'],
  }, {});

  expect(g2.matched).toBeTrue();
  expect(g2.normalizedDeviceId).toEqual('PV123');
  expect(g2.candidate?.host).toEqual('powerview.local');
  expect(g3.matched).toBeTrue();
  expect(g3.candidate?.metadata?.apiVersion).toEqual(3);
});

tap.test('matches PowerView DHCP leases from manifest patterns', async () => {
  const descriptor = createHunterDouglasPowerViewDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'hunterdouglas_powerview-dhcp-match');
  const result = await matcher!.matches({
    hostname: 'hunter-hub',
    ip: '192.168.1.51',
    macaddress: '00:26:74:12:34:56',
  }, {});

  expect(result.matched).toBeTrue();
  expect(result.confidence).toEqual('certain');
  expect(result.candidate?.source).toEqual('dhcp');
  expect(result.candidate?.macAddress).toEqual('00:26:74:12:34:56');
});

tap.test('builds config flow output from manual snapshot data', async () => {
  const snapshot = HunterDouglasPowerViewMapper.toSnapshot({
    config: { name: 'Snapshot PowerView' },
    online: true,
    source: 'manual',
    rawData: {
      apiVersion: 2,
      hub: {
        userData: {
          hubName: Buffer.from('Snapshot Hub').toString('base64'),
          serialNumber: 'SERIAL123',
          macAddress: '002674123456',
          firmware: { mainProcessor: { revision: 2, subRevision: 1, build: 0, name: 'PV Hub2.0' } },
        },
      },
      shades: [{ id: 1, name: Buffer.from('Office Shade').toString('base64'), type: 1, capabilities: 0, positions: { posKind1: 1, position1: 65535 } }],
    },
  });
  const descriptor = createHunterDouglasPowerViewDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'hunterdouglas_powerview-manual-match');
  const match = await matcher!.matches({ metadata: { snapshot } }, {});
  const validation = await descriptor.getValidators()[0].validate(match.candidate!, {});
  const step = await new HunterDouglasPowerViewConfigFlow().start(match.candidate!, {});
  const done = await step.submit!({});

  expect(match.matched).toBeTrue();
  expect(validation.matched).toBeTrue();
  expect(done.kind).toEqual('done');
  expect(done.config?.snapshot?.hub.id).toEqual('SERIAL123');
  expect(done.config?.host).toBeUndefined();
});

export default tap.start();
