import { expect, tap } from '@git.zone/tstest/tapbundle';
import { createMikrotikDiscoveryDescriptor, MikrotikConfigFlow } from '../../ts/integrations/mikrotik/index.js';

tap.test('matches and validates manual Mikrotik RouterOS/API entries', async () => {
  const descriptor = createMikrotikDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[0];
  const result = await matcher.matches({
    host: '192.168.88.1',
    name: 'RouterOS Lab',
    model: 'hAP ax3',
    macAddress: 'AA-BB-CC-DD-EE-FF',
  }, {});

  expect(result.matched).toBeTrue();
  expect(result.candidate?.integrationDomain).toEqual('mikrotik');
  expect(result.candidate?.port).toEqual(8728);
  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?.liveRouterOsApiImplemented).toBeFalse();
});

tap.test('accepts snapshot-only manual setup and rejects unrelated entries', async () => {
  const descriptor = createMikrotikDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[0];
  const snapshotResult = await matcher.matches({
    snapshot: {
      connected: true,
      router: { name: 'Snapshot Router', serialNumber: 'MK654321' },
      resources: {},
      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 Mikrotik config without claiming live API support', async () => {
  const flow = new MikrotikConfigFlow();
  const step = await flow.start({ source: 'manual', host: '192.168.88.1', metadata: { verifySsl: true } }, {});
  const done = await step.submit!({ host: '192.168.88.1', verifySsl: true, username: 'admin' });

  expect(done.kind).toEqual('done');
  expect(done.config?.port).toEqual(8728);
  expect(done.config?.protocol).toEqual('routeros-api-ssl');
  expect(done.config?.metadata?.liveRouterOsApiImplemented).toBeFalse();
});

export default tap.start();
