import { expect, tap } from '@git.zone/tstest/tapbundle';
import { DlinkConfigFlow, createDlinkDiscoveryDescriptor, type IDlinkSnapshot } from '../../ts/integrations/dlink/index.js';

const snapshot: IDlinkSnapshot = {
  connected: true,
  device: {
    id: 'plug-1',
    name: 'Kitchen Plug',
    host: '192.168.1.44',
    modelName: 'DSP-W215',
    manufacturer: 'D-Link',
  },
  switch: {
    state: 'OFF',
    available: true,
    temperature: 29,
    totalConsumption: 12.5,
  },
  events: [],
};

tap.test('matches D-Link DHCP candidates by Home Assistant hostname rule', async () => {
  const descriptor = createDlinkDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'dlink-dhcp-match');
  const result = await matcher?.matches({
    ipAddress: '192.168.1.44',
    hostname: 'DSP-W215',
    macAddress: 'AA-BB-CC-DD-EE-FF',
  }, {});

  expect(result?.matched).toBeTrue();
  expect(result?.confidence).toEqual('certain');
  expect(result?.candidate?.integrationDomain).toEqual('dlink');
  expect(result?.candidate?.host).toEqual('192.168.1.44');
  expect(result?.candidate?.macAddress).toEqual('aa:bb:cc:dd:ee:ff');
  expect(result?.candidate?.metadata?.discoveryProtocol).toEqual('dhcp');
});

tap.test('matches manual D-Link snapshot entries', async () => {
  const descriptor = createDlinkDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'dlink-manual-match');
  const result = await matcher?.matches({
    snapshot,
  }, {});

  expect(result?.matched).toBeTrue();
  expect(result?.confidence).toEqual('certain');
  expect(result?.candidate?.host).toEqual('192.168.1.44');
  expect(result?.candidate?.metadata?.snapshot).toEqual(snapshot);
});

tap.test('builds config flow output and validates required local inputs', async () => {
  const flow = new DlinkConfigFlow();
  const step = await flow.start({
    source: 'dhcp',
    integrationDomain: 'dlink',
    id: 'aa:bb:cc:dd:ee:ff',
    host: '192.168.1.44',
    name: 'Kitchen Plug',
    metadata: { hostname: 'DSP-W215' },
  }, {});

  const done = await step.submit?.({ password: '123456', useLegacyProtocol: true, nativeHttpEnabled: true, timeoutMs: '2500' });
  expect(done?.kind).toEqual('done');
  expect(done?.config?.host).toEqual('192.168.1.44');
  expect(done?.config?.username).toEqual('admin');
  expect(done?.config?.password).toEqual('123456');
  expect(done?.config?.useLegacyProtocol).toBeTrue();
  expect(done?.config?.timeoutMs).toEqual(2500);

  const missingPassword = await step.submit?.({});
  expect(missingPassword?.kind).toEqual('error');
  expect(missingPassword?.error || '').toInclude('password');

  const snapshotDone = await step.submit?.({ snapshotJson: JSON.stringify(snapshot) });
  expect(snapshotDone?.kind).toEqual('done');
  expect(snapshotDone?.config?.snapshot?.device.modelName).toEqual('DSP-W215');

  const invalidSnapshot = await step.submit?.({ password: '123456', snapshotJson: '{"connected":true}' });
  expect(invalidSnapshot?.kind).toEqual('error');
});

export default tap.start();
