import { expect, tap } from '@git.zone/tstest/tapbundle';
import { BlackbirdClient, BlackbirdConfigFlow, BlackbirdIntegration, BlackbirdMapper, HomeAssistantBlackbirdIntegration, blackbirdProfile, blackbirdSampleData, createBlackbirdDiscoveryDescriptor, type IBlackbirdSnapshot } from '../../ts/integrations/blackbird/index.js';

tap.test('matches manual Blackbird candidates and creates config flow output', async () => {
  const descriptor = createBlackbirdDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'blackbird-manual-match');
  const result = await matcher!.matches({ host: 'blackbird.local', name: 'Monoprice Blackbird Matrix Switch', metadata: { rawData: blackbirdSampleData } }, {});

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

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

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

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

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('blackbird');
  expect(devices[0].manufacturer).toEqual('Monoprice');
  expect(entities.length > 0).toBeTrue();
});

tap.test('exposes Blackbird control runtime and Home Assistant alias', async () => {
  const integration = new BlackbirdIntegration();
  const alias = new HomeAssistantBlackbirdIntegration();
  expect(integration.status).toEqual('control-runtime');
  expect(alias.domain).toEqual('blackbird');
  expect(blackbirdProfile.serviceDomains).toEqual(['switch', 'media_player']);
  expect(blackbirdProfile.metadata.sampleData).toEqual(blackbirdSampleData);

  const runtime = await integration.setup({ name: 'Blackbird Runtime', rawData: blackbirdSampleData }, {});
  const status = await runtime.callService!({ domain: 'media_player', service: 'status', target: {} });
  const snapshot = status.data as IBlackbirdSnapshot;

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

  const liveCommand = await runtime.callService!({ domain: 'media_player', service: 'select_source', target: {}, data: { source: 'HDMI2' } });
  expect(liveCommand.success).toBeFalse();
  await runtime.destroy();

  const executorRuntime = await integration.setup({
    name: 'Blackbird Executor',
    rawData: blackbirdSampleData,
    commandExecutor: {
      execute: async (requestArg) => ({ success: true, data: { domain: requestArg.domain, service: requestArg.service } }),
    },
  }, {});
  const executed = await executorRuntime.callService!({ domain: 'switch', service: 'toggle', target: {} });
  expect(executed.success).toBeTrue();
  expect((executed.data as { domain: string; service: string }).domain).toEqual('switch');
  expect((executed.data as { domain: string; service: string }).service).toEqual('toggle');
  await executorRuntime.destroy();
});

export default tap.start();
