import { expect, tap } from '@git.zone/tstest/tapbundle';
import { Dremel3dPrinterClient, Dremel3dPrinterConfigFlow, Dremel3dPrinterIntegration, Dremel3dPrinterMapper, HomeAssistantDremel3dPrinterIntegration, createDremel3dPrinterDiscoveryDescriptor, dremel3dPrinterProfile, type IDremel3dPrinterSnapshot } from '../../ts/integrations/dremel_3d_printer/index.js';

const rawData = {
  job_phase: 'building',
  progress: 42,
  door: false,
  running: true,
};

tap.test('matches manual Dremel candidates and creates config flow output', async () => {
  const descriptor = createDremel3dPrinterDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'dremel_3d_printer-manual-match');
  const result = await matcher!.matches({ host: 'dremel.local', name: 'Dremel 3D45', metadata: { rawData } }, {});

  expect(result.matched).toBeTrue();
  expect(result.candidate?.integrationDomain).toEqual('dremel_3d_printer');

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

  const done = await (await new Dremel3dPrinterConfigFlow().start(result.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.host).toEqual('dremel.local');
});

tap.test('maps Dremel raw snapshots to runtime devices and entities', async () => {
  const client = new Dremel3dPrinterClient({ name: 'Dremel Lab Printer', rawData });
  const snapshot = await client.getSnapshot();
  const devices = Dremel3dPrinterMapper.toDevices(snapshot);
  const entities = Dremel3dPrinterMapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('dremel_3d_printer');
  expect(devices[0].manufacturer).toEqual('Dremel');
  expect(entities.some((entityArg) => entityArg.name === 'Progress' && entityArg.state === 42)).toBeTrue();
});

tap.test('exposes Dremel runtime services, alias, and explicit gated control', async () => {
  expect(new HomeAssistantDremel3dPrinterIntegration().domain).toEqual('dremel_3d_printer');
  expect(dremel3dPrinterProfile.status).toEqual('control-runtime');
  expect(dremel3dPrinterProfile.metadata.qualityScale).toEqual('legacy');
  expect(dremel3dPrinterProfile.metadata.requirements).toEqual(['dremel3dpy==2.1.1']);

  const runtime = await new Dremel3dPrinterIntegration().setup({ name: 'Dremel Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'dremel_3d_printer', service: 'status', target: {} });
  const snapshot = status.data as IDremel3dPrinterSnapshot;

  expect(status.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('Dremel Runtime');

  const controlCommand = await runtime.callService!({ domain: 'button', service: 'press', target: {} });
  expect(controlCommand.success).toBeFalse();
  await runtime.destroy();
});

export default tap.start();
