import { expect, tap } from '@git.zone/tstest/tapbundle';
import { ClementineClient, ClementineConfigFlow, ClementineIntegration, ClementineMapper, HomeAssistantClementineIntegration, clementineProfile, createClementineDiscoveryDescriptor, type IClementineSnapshot } from '../../ts/integrations/clementine/index.js';

const rawData = {
  state: 'playing',
  title: 'Track',
  volume: 70,
};

tap.test('exposes Clementine Music Player simple-local profile and alias', async () => {
  const alias = new HomeAssistantClementineIntegration();

  expect(alias.domain).toEqual('clementine');
  expect(clementineProfile.displayName).toEqual('Clementine Music Player');
  expect(clementineProfile.status).toEqual('control-runtime');
  expect(clementineProfile.serviceDomains).toEqual(['media_player']);
  expect(clementineProfile.metadata.requirements).toEqual(['python-clementine-remote==1.0.1']);
});

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

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

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

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

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

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

  const runtime = await new ClementineIntegration().setup({ name: 'Clementine Music Player Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'media_player', service: 'status', target: {} });
  const statusSnapshot = status.data as IClementineSnapshot;

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

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

  const executorRuntime = await new ClementineIntegration().setup({
    name: 'Clementine Music Player 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();
