import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HikvisionConfigFlow, createHikvisionDiscoveryDescriptor } from '../../ts/integrations/hikvision/index.js';

tap.test('matches manual Hikvision camera/NVR entries and builds config flow output', async () => {
  const descriptor = createHikvisionDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[0];
  const match = await matcher.matches({ host: '192.168.1.40', name: 'Garage NVR', manufacturer: 'Hikvision', deviceType: 'NVR' }, {});

  expect(match.matched).toBeTrue();
  expect(match.candidate?.integrationDomain).toEqual('hikvision');
  expect(match.candidate?.host).toEqual('192.168.1.40');
  expect(match.candidate?.port).toEqual(80);

  const validation = await descriptor.getValidators()[0].validate(match.candidate!, {});
  expect(validation.matched).toBeTrue();

  const flow = new HikvisionConfigFlow();
  const step = await flow.start(match.candidate!, {});
  const done = await step.submit!({ username: 'admin', password: 'secret', protocol: 'https', port: 443 });
  expect(done.kind).toEqual('done');
  expect(done.config?.host).toEqual('192.168.1.40');
  expect(done.config?.protocol).toEqual('https');
  expect(done.config?.ssl).toBeTrue();
  expect(done.config?.username).toEqual('admin');
  expect(done.config?.rtspPort).toEqual(554);
});

tap.test('matches local SSDP Hikvision metadata', async () => {
  const descriptor = createHikvisionDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[2];
  const match = await matcher.matches({
    manufacturer: 'Hikvision',
    location: 'http://192.168.1.41:80/',
    upnp: {
      friendlyName: 'Driveway Hikvision',
      modelName: 'DS-2CD2143G0-I',
      serialNumber: 'HKV456',
    },
  }, {});

  expect(match.matched).toBeTrue();
  expect(match.candidate?.host).toEqual('192.168.1.41');
  expect(match.candidate?.manufacturer).toEqual('Hikvision');
  expect(match.candidate?.model).toEqual('DS-2CD2143G0-I');
});

export default tap.start();
