import { expect, tap } from '@git.zone/tstest/tapbundle';
import { FullyKioskIntegration, createFullyKioskDiscoveryDescriptor } from '../../ts/integrations/fully_kiosk/index.js';

tap.test('matches manual and DHCP Fully Kiosk candidates', async () => {
  const descriptor = createFullyKioskDiscoveryDescriptor();
  const manualMatcher = descriptor.getMatchers()[0];
  const manual = await manualMatcher.matches({
    url: 'http://192.168.1.50:2323',
    password: 'secret',
    name: 'Wall Tablet',
  }, {});
  expect(manual.matched).toBeTrue();
  expect(manual.candidate?.integrationDomain).toEqual('fully_kiosk');
  expect(manual.candidate?.host).toEqual('192.168.1.50');
  expect(manual.candidate?.metadata?.password).toEqual('secret');

  const dhcpMatcher = descriptor.getMatchers()[1];
  const dhcp = await dhcpMatcher.matches({
    ip: '192.168.1.51',
    macaddress: 'AABBCCDDEEFF',
    registered_devices: true,
  }, {});
  expect(dhcp.matched).toBeTrue();
  expect(dhcp.candidate?.source).toEqual('dhcp');
  expect(dhcp.normalizedDeviceId).toEqual('aa:bb:cc:dd:ee:ff');

  const validator = descriptor.getValidators()[0];
  const valid = await validator.validate(manual.candidate!, {});
  expect(valid.matched).toBeTrue();
  expect(valid.confidence).toEqual('certain');
});

tap.test('config flow builds local REST config and requires password for live host', async () => {
  const integration = new FullyKioskIntegration();
  const step = await integration.configFlow.start({
    source: 'manual',
    integrationDomain: 'fully_kiosk',
    host: 'http://192.168.1.50:2323',
    name: 'Wall Tablet',
    macAddress: 'aa:bb:cc:dd:ee:ff',
  }, {});

  const missingPassword = await step.submit?.({ host: 'http://192.168.1.50:2323' });
  expect(missingPassword?.kind).toEqual('error');

  const done = await step.submit?.({ host: 'http://192.168.1.50:2323', password: 'secret', verifySsl: false });
  expect(done?.kind).toEqual('done');
  expect(done?.config?.host).toEqual('192.168.1.50');
  expect(done?.config?.port).toEqual(2323);
  expect(done?.config?.ssl).toBeFalse();
  expect(done?.config?.password).toEqual('secret');
  expect(done?.config?.macAddress).toEqual('aa:bb:cc:dd:ee:ff');
});

export default tap.start();
