import { expect, tap } from '@git.zone/tstest/tapbundle';
import { BangOlufsenClient, BangOlufsenConfigFlow, BangOlufsenIntegration, BangOlufsenMapper, HomeAssistantBangOlufsenIntegration, bangOlufsenProfile, createBangOlufsenDiscoveryDescriptor, type IBangOlufsenSnapshot } from '../../ts/integrations/bang_olufsen/index.js';

const rawData = {
  state: 'playing',
  source: 'Line-in',
  volume: 24
};

tap.test('matches manual Bang & Olufsen candidates and creates config flow output', async () => {
  const descriptor = createBangOlufsenDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'bang_olufsen-manual-match');
  const result = await matcher!.matches({ host: 'bang-olufsen.local', name: 'Bang & Olufsen', metadata: { rawData } }, {});

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

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

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

tap.test('maps Bang & Olufsen raw snapshots to runtime devices and entities', async () => {
  const client = new BangOlufsenClient({ name: 'Bang & Olufsen Test', rawData });
  const snapshot = await client.getSnapshot();
  const devices = BangOlufsenMapper.toDevices(snapshot);
  const entities = BangOlufsenMapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(snapshot.entities.find((entityArg) => entityArg.id === 'volume')?.state).toEqual(24);
  expect(devices[0].integrationDomain).toEqual('bang_olufsen');
  expect(entities.length > 0).toBeTrue();
});

tap.test('exposes Bang & Olufsen media runtime services through real executors only', async () => {
  const integration = new HomeAssistantBangOlufsenIntegration();
  expect(integration).toBeInstanceOf(BangOlufsenIntegration);
  expect(integration.status).toEqual('control-runtime');
  expect(bangOlufsenProfile.metadata.requirements).toEqual(['mozart-api==5.3.1.108.2']);

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

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

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

  const executorRuntime = await new BangOlufsenIntegration().setup({
    name: 'Bang & Olufsen Executor',
    rawData,
    commandExecutor: {
      execute: async (requestArg) => ({ success: true, data: { domain: requestArg.domain, service: requestArg.service } }),
    },
  }, {});
  const executed = await executorRuntime.callService!({ domain: 'media_player', service: 'media_play', target: {} });
  expect(executed.success).toBeTrue();
  expect((executed.data as { domain: string; service: string }).domain).toEqual('media_player');
  expect((executed.data as { domain: string; service: string }).service).toEqual('media_play');
  await executorRuntime.destroy();
});

export default tap.start();
