import { expect, tap } from '@git.zone/tstest/tapbundle';
import { AmcrestConfigFlow, createAmcrestDiscoveryDescriptor } from '../../ts/integrations/amcrest/index.js';

tap.test('matches manual Amcrest host entries and configures flow', async () => {
  const descriptor = createAmcrestDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[0];
  const match = await matcher.matches({ host: '192.168.1.30', name: 'Front Door' }, {});

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

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

  const flow = new AmcrestConfigFlow();
  const step = await flow.start(match.candidate!, {});
  const done = await step.submit!({ username: 'admin', password: 'secret', streamSource: 'rtsp', resolution: 'low' });
  expect(done.kind).toEqual('done');
  expect(done.config?.host).toEqual('192.168.1.30');
  expect(done.config?.username).toEqual('admin');
  expect(done.config?.streamSource).toEqual('rtsp');
  expect(done.config?.resolution).toEqual('low');
});

tap.test('matches local SSDP camera metadata', async () => {
  const descriptor = createAmcrestDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[2];
  const match = await matcher.matches({
    manufacturer: 'Amcrest',
    location: 'http://192.168.1.31:80/',
    upnp: {
      friendlyName: 'Garage Amcrest',
      modelName: 'IP8M-2496',
      serialNumber: 'AMC456',
    },
  }, {});

  expect(match.matched).toBeTrue();
  expect(match.candidate?.host).toEqual('192.168.1.31');
  expect(match.candidate?.manufacturer).toEqual('Amcrest');
  expect(match.candidate?.model).toEqual('IP8M-2496');
});

export default tap.start();
