import { expect, tap } from '@git.zone/tstest/tapbundle';
import { FritzConfigFlow, createFritzDiscoveryDescriptor } from '../../ts/integrations/fritz/index.js';

tap.test('matches and validates manual FRITZ entries', async () => {
  const descriptor = createFritzDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[0];
  const result = await matcher.matches({
    host: '192.168.178.1',
    name: 'Home Fritz',
    model: 'FRITZ!Box 7590',
    serialNumber: 'AABBCCDDEEFF',
  }, {});

  expect(result.matched).toBeTrue();
  expect(result.candidate?.integrationDomain).toEqual('fritz');
  expect(result.candidate?.port).toEqual(49000);
  expect(result.normalizedDeviceId).toEqual('AABBCCDDEEFF');

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

tap.test('accepts snapshot-only setup and rejects unrelated manual entries', async () => {
  const descriptor = createFritzDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[0];
  const snapshotResult = await matcher.matches({
    snapshot: {
      connected: true,
      router: { name: 'Snapshot Fritz', serialNumber: 'AABBCCDDEEFF', model: 'FRITZ!Box 7530' },
      devices: [],
      interfaces: [],
      connection: {},
      sensors: {},
      wifiNetworks: [],
      portForwards: [],
      callDeflections: [],
    },
  }, {});
  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('matches Home Assistant supported FRITZ SSDP candidates', async () => {
  const descriptor = createFritzDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[1];
  const result = await matcher.matches({
    st: 'urn:schemas-upnp-org:device:fritzbox:1',
    ssdpLocation: 'http://192.168.178.1:49000/rootDesc.xml',
    upnp: {
      friendlyName: 'FRITZ!Box 7590',
      modelName: 'FRITZ!Box 7590',
      UDN: 'uuid:abcdef01-2345-6789-abcd-ef0123456789',
    },
  }, {});

  expect(result.matched).toBeTrue();
  expect(result.confidence).toEqual('certain');
  expect(result.candidate?.source).toEqual('ssdp');
  expect(result.candidate?.host).toEqual('192.168.178.1');
  expect(result.candidate?.metadata?.upstreamSupportsZeroconf).toBeFalse();
});

tap.test('matches supplied FRITZ zeroconf candidates without claiming upstream zeroconf manifest support', async () => {
  const descriptor = createFritzDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[2];
  const result = await matcher.matches({
    host: 'fritz.box',
    name: 'FRITZ!Box 7530',
    model: 'FRITZ!Box 7530',
    serviceType: '_http._tcp.local',
  }, {});

  expect(result.matched).toBeTrue();
  expect(result.candidate?.source).toEqual('mdns');
  expect(result.candidate?.metadata?.upstreamSupportsZeroconf).toBeFalse();
});

tap.test('builds FRITZ config flow without claiming live TR-064 validation', async () => {
  const flow = new FritzConfigFlow();
  const step = await flow.start({ source: 'ssdp', host: '192.168.178.1', metadata: { ssl: true } }, {});
  const done = await step.submit!({ host: '192.168.178.1', ssl: true, username: 'homeassistant', password: 'secret', featureDeviceTracking: true });

  expect(done.kind).toEqual('done');
  expect(done.config?.port).toEqual(49443);
  expect(done.config?.ssl).toBeTrue();
  expect(done.config?.metadata?.liveTr064Implemented).toBeFalse();
});

export default tap.start();
