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

tap.test('creates LinkPlay config from discovered endpoint', async () => {
  const flow = new LinkplayConfigFlow();
  const step = await flow.start({
    source: 'mdns',
    integrationDomain: 'linkplay',
    id: 'leader-uuid',
    host: 'living-room.local',
    port: 80,
    name: 'Living Room',
    manufacturer: 'Arylic',
    model: 'S50+',
    metadata: { project: 'ARYLIC_S50', protocol: 'http' },
  }, {});

  expect(step.kind).toEqual('form');
  const done = await step.submit!({ name: 'Living Room Speaker' });
  expect(done.kind).toEqual('done');
  expect(done.config?.host).toEqual('living-room.local');
  expect(done.config?.port).toEqual(80);
  expect(done.config?.protocol).toEqual('http');
  expect(done.config?.uniqueId).toEqual('leader-uuid');
  expect(done.config?.name).toEqual('Living Room Speaker');
});

tap.test('requires host and blocks WiiM candidates like Home Assistant', async () => {
  const flow = new LinkplayConfigFlow();
  const missingHost = await flow.start({ source: 'manual', integrationDomain: 'linkplay' }, {});
  const missingResult = await missingHost.submit!({});
  expect(missingResult.kind).toEqual('error');

  const wiim = await flow.start({ source: 'mdns', integrationDomain: 'linkplay', host: 'wiim.local', manufacturer: 'WiiM', model: 'WiiM Pro' }, {});
  const wiimResult = await wiim.submit!({});
  expect(wiimResult.kind).toEqual('error');
  expect(wiimResult.error?.includes('WiiM')).toBeTrue();
});

export default tap.start();
