import { expect, tap } from '@git.zone/tstest/tapbundle';
import { BluemaestroClient, BluemaestroConfigFlow, BluemaestroIntegration, BluemaestroMapper, HomeAssistantBluemaestroIntegration, bluemaestroProfile, bluemaestroSampleData, createBluemaestroDiscoveryDescriptor, type IBluemaestroSnapshot } from '../../ts/integrations/bluemaestro/index.js';

tap.test('matches manual BlueMaestro candidates and creates config flow output', async () => {
  const descriptor = createBluemaestroDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'bluemaestro-manual-match');
  const result = await matcher!.matches({ name: 'BlueMaestro', metadata: { rawData: bluemaestroSampleData } }, {});

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

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

  const done = await (await new BluemaestroConfigFlow().start(result.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.rawData).toEqual(bluemaestroSampleData);
});

tap.test('maps BlueMaestro raw snapshots to runtime devices and entities', async () => {
  const client = new BluemaestroClient({ name: 'BlueMaestro Test', rawData: bluemaestroSampleData });
  const snapshot = await client.getSnapshot();
  const devices = BluemaestroMapper.toDevices(snapshot);
  const entities = BluemaestroMapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('bluemaestro');
  expect(devices[0].manufacturer).toEqual('BlueMaestro');
  expect(entities.length).toEqual(3);
});

tap.test('exposes BlueMaestro read-only runtime and Home Assistant alias', async () => {
  const integration = new BluemaestroIntegration();
  const alias = new HomeAssistantBluemaestroIntegration();
  expect(integration.status).toEqual('read-only-runtime');
  expect(alias.domain).toEqual('bluemaestro');
  expect(bluemaestroProfile.controlServices).toEqual([]);
  expect(bluemaestroProfile.metadata.dependencies).toEqual(['bluetooth_adapters']);
  expect(bluemaestroProfile.metadata.sampleData).toEqual(bluemaestroSampleData);

  const runtime = await integration.setup({ name: 'BlueMaestro Runtime', rawData: bluemaestroSampleData }, {});
  const status = await runtime.callService!({ domain: 'bluemaestro', service: 'status', target: {} });
  const snapshot = status.data as IBluemaestroSnapshot;

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

  const unsupportedDomain = await runtime.callService!({ domain: 'sensor', service: 'status', target: {} });
  expect(unsupportedDomain.success).toBeFalse();

  const unsupportedService = await runtime.callService!({ domain: 'bluemaestro', service: 'calibrate', target: {} });
  expect(unsupportedService.success).toBeFalse();
  await runtime.destroy();
});

export default tap.start();
