import { expect, tap } from '@git.zone/tstest/tapbundle';
import { AsuswrtConfigFlow, createAsuswrtDiscoveryDescriptor } from '../../ts/integrations/asuswrt/index.js';

tap.test('matches and validates manual ASUSWRT router entries', async () => {
  const descriptor = createAsuswrtDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[0];
  const result = await matcher.matches({
    host: '192.168.1.1',
    name: 'Main Router',
    model: 'RT-AX88U',
    macAddress: 'AA-BB-CC-DD-EE-FF',
  }, {});

  expect(result.matched).toBeTrue();
  expect(result.candidate?.integrationDomain).toEqual('asuswrt');
  expect(result.candidate?.port).toEqual(8443);
  expect(result.normalizedDeviceId).toEqual('aa:bb:cc:dd:ee:ff');

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

tap.test('accepts snapshot-only manual setup and rejects unrelated entries', async () => {
  const descriptor = createAsuswrtDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[0];
  const snapshotResult = await matcher.matches({
    snapshot: {
      connected: true,
      router: { name: 'Snapshot Router', labelMac: 'AABBCCDDEEFF' },
      devices: [],
      interfaces: [],
      sensors: {},
    },
  }, {});
  const unrelated = await matcher.matches({ name: 'Generic Switch', model: 'GS108' }, {});

  expect(snapshotResult.matched).toBeTrue();
  expect(snapshotResult.confidence).toEqual('certain');
  expect(unrelated.matched).toBeFalse();
});

tap.test('builds manual ASUSWRT config without claiming SSH/Telnet live support', async () => {
  const flow = new AsuswrtConfigFlow();
  const step = await flow.start({ source: 'manual', host: '192.168.1.1', metadata: { protocol: 'ssh' } }, {});
  const done = await step.submit!({ host: '192.168.1.1', protocol: 'ssh', username: 'admin', mode: 'ap' });

  expect(done.kind).toEqual('done');
  expect(done.config?.protocol).toEqual('ssh');
  expect(done.config?.mode).toEqual('ap');
  expect(done.config?.metadata?.liveSshTelnetImplemented).toBeFalse();
});

export default tap.start();
