import { expect, tap } from '@git.zone/tstest/tapbundle';
import { CmusClient, CmusConfigFlow, CmusIntegration, CmusMapper, HomeAssistantCmusIntegration, cmusProfile, createCmusDiscoveryDescriptor, type ICmusSnapshot } from '../../ts/integrations/cmus/index.js';

const rawData = {
  state: 'paused',
  title: 'Track',
  volume: 65,
};

tap.test('exposes cmus simple-local profile and alias', async () => {
  const alias = new HomeAssistantCmusIntegration();

  expect(alias.domain).toEqual('cmus');
  expect(cmusProfile.displayName).toEqual('cmus');
  expect(cmusProfile.status).toEqual('control-runtime');
  expect(cmusProfile.serviceDomains).toEqual(['media_player']);
  expect(cmusProfile.metadata.requirements).toEqual(['pycmus==0.1.1']);
});

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

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

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

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

tap.test('maps cmus samples and exposes media control runtime services', async () => {
  const client = new CmusClient({ name: 'cmus Test', rawData });
  const snapshot = await client.getSnapshot();
  const devices = CmusMapper.toDevices(snapshot);
  const entities = CmusMapper.toEntities(snapshot);

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

  const runtime = await new CmusIntegration().setup({ name: 'cmus Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'media_player', service: 'status', target: {} });
  const statusSnapshot = status.data as ICmusSnapshot;

  expect(status.success).toBeTrue();
  expect(statusSnapshot.online).toBeTrue();

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

  const executorRuntime = await new CmusIntegration().setup({
    name: 'cmus 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_pause', 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_pause');
  await executorRuntime.destroy();
});

export default tap.start();
