import { expect, tap } from '@git.zone/tstest/tapbundle';
import { CambridgeAudioClient, CambridgeAudioConfigFlow, CambridgeAudioIntegration, CambridgeAudioMapper, HomeAssistantCambridgeAudioIntegration, cambridgeAudioProfile, createCambridgeAudioDiscoveryDescriptor, type ICambridgeAudioSnapshot } from '../../ts/integrations/cambridge_audio/index.js';

const rawData = {
  state: 'playing',
  source: 'Spotify',
  volume: 30,
};

tap.test('matches manual Cambridge Audio candidates and exposes native profile metadata', async () => {
  const integration = new CambridgeAudioIntegration();
  const alias = new HomeAssistantCambridgeAudioIntegration();
  const descriptor = createCambridgeAudioDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'cambridge_audio-manual-match');
  const result = await matcher!.matches({ host: 'cambridge-audio.local', name: 'Cambridge Audio StreamMagic', metadata: { rawData } }, {});

  expect(alias.domain).toEqual('cambridge_audio');
  expect(integration.displayName).toEqual('Cambridge Audio');
  expect(cambridgeAudioProfile.manufacturer).toEqual('Cambridge Audio');
  expect(cambridgeAudioProfile.model).toEqual('StreamMagic player');
  expect(cambridgeAudioProfile.metadata.requirements).toEqual(['aiostreammagic==2.13.0']);
  expect(cambridgeAudioProfile.metadata.qualityScale).toEqual('platinum');
  expect(cambridgeAudioProfile.serviceDomains).toEqual(['media_player']);
  expect(cambridgeAudioProfile.controlServices?.includes('media_play')).toBeTrue();
  expect(result.matched).toBeTrue();
  expect(result.candidate?.integrationDomain).toEqual('cambridge_audio');

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

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

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

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(snapshot.rawData).toEqual(rawData);
  expect(devices[0].integrationDomain).toEqual('cambridge_audio');
  expect(devices[0].manufacturer).toEqual('Cambridge Audio');
  expect(devices[0].model).toEqual('StreamMagic player');
  expect(entities.some((entityArg) => entityArg.name === 'Source' && entityArg.state === 'Spotify')).toBeTrue();
});

tap.test('exposes Cambridge Audio media services without fake live control', async () => {
  const runtime = await new CambridgeAudioIntegration().setup({ name: 'Cambridge Audio Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'media_player', service: 'status', target: {} });
  const snapshot = status.data as ICambridgeAudioSnapshot;

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

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

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

export default tap.start();
