import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HuaweiLteConfigFlow, HuaweiLteMapper, createHuaweiLteDiscoveryDescriptor, type IHuaweiLteRawData } from '../../ts/integrations/huawei_lte/index.js';

tap.test('matches Home Assistant Huawei LTE SSDP records and creates config flow output', async () => {
  const descriptor = createHuaweiLteDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'huawei_lte-ssdp-match');
  const result = await matcher!.matches({
    st: 'urn:schemas-upnp-org:device:InternetGatewayDevice:1',
    location: 'http://192.168.8.1/rootDesc.xml',
    upnp: {
      deviceType: 'urn:schemas-upnp-org:device:InternetGatewayDevice:1',
      manufacturer: 'Huawei Technologies Co., Ltd.',
      friendlyName: 'B535-232',
      modelName: 'B535-232',
      serialNumber: 'ABC123456789',
      presentationURL: 'http://192.168.8.1/',
      udn: 'uuid:huawei-b535',
    },
  }, {});

  expect(result.matched).toBeTrue();
  expect(result.confidence).toEqual('certain');
  expect(result.candidate?.integrationDomain).toEqual('huawei_lte');
  expect(result.candidate?.host).toEqual('192.168.8.1');
  expect(result.candidate?.port).toEqual(80);
  expect(result.candidate?.metadata?.url).toEqual('http://192.168.8.1/');

  const done = await (await new HuaweiLteConfigFlow().start(result.candidate!, {})).submit!({ username: 'admin', password: 'secret' });
  expect(done.kind).toEqual('done');
  expect(done.config?.url).toEqual('http://192.168.8.1/');
  expect(done.config?.username).toEqual('admin');
  expect(done.config?.password).toEqual('secret');
  expect(done.config?.uniqueId).toEqual('ABC123456789');
});

tap.test('matches manual snapshots without requiring a live router during setup', async () => {
  const rawData: Partial<IHuaweiLteRawData> = {
    deviceInformation: {
      DeviceName: 'B818-263',
      SerialNumber: 'SNAP123',
      SoftwareVersion: '11.0.1.1',
      MacAddress1: 'AABBCCDDEEFF',
    },
    monitoringStatus: {
      ConnectionStatus: '901',
      WifiStatus: '1',
    },
    dialupMobileDataswitch: {
      dataswitch: '1',
    },
  };
  const snapshot = HuaweiLteMapper.toSnapshot({ config: { name: 'Snapshot LTE' }, rawData, online: true, source: 'manual' });
  const descriptor = createHuaweiLteDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'huawei_lte-manual-match');
  const match = await matcher!.matches({ metadata: { snapshot } }, {});

  expect(match.matched).toBeTrue();
  expect(match.candidate?.host).toBeUndefined();
  expect(match.candidate?.metadata?.snapshot).toEqual(snapshot);

  const done = await (await new HuaweiLteConfigFlow().start(match.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.snapshot?.device.serialNumber).toEqual('SNAP123');
  expect(done.config?.host).toBeUndefined();
});

tap.test('rejects Huawei-looking candidates without a usable local source', async () => {
  const validation = await createHuaweiLteDiscoveryDescriptor().getValidators()[0].validate({
    source: 'manual',
    integrationDomain: 'huawei_lte',
    name: 'Huawei without URL',
  }, {});

  expect(validation.matched).toBeFalse();
  expect(validation.reason).toEqual('Huawei LTE candidate lacks a host, URL, injected client, snapshot, or raw data.');
});

export default tap.start();
