import { expect, tap } from '@git.zone/tstest/tapbundle';
import { ComfoconnectClient, ComfoconnectConfigFlow, ComfoconnectIntegration, ComfoconnectMapper, HomeAssistantComfoconnectIntegration, comfoconnectProfile, createComfoconnectDiscoveryDescriptor, type IComfoconnectSnapshot } from '../../ts/integrations/comfoconnect/index.js';

const rawData = {
  "fan_speed": 2,
  "supply_temperature": 19.8,
  "filter_alarm": false
};

tap.test('exposes Zehnder ComfoAir Q profile and alias class', async () => {
  const integration = new HomeAssistantComfoconnectIntegration();

  expect(integration.domain).toEqual('comfoconnect');
  expect(integration.status).toEqual('control-runtime');
  expect(comfoconnectProfile.displayName).toEqual('Zehnder ComfoAir Q');
  expect(comfoconnectProfile.manufacturer).toEqual('Zehnder');
  expect((comfoconnectProfile.metadata.requirements as string[])[0]).toEqual('pycomfoconnect==0.5.1');
});

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

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

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

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

tap.test('maps Zehnder ComfoAir Q snapshots and gates live control', async () => {
  const client = new ComfoconnectClient({ name: 'ComfoAir Runtime', rawData });
  const snapshot = await client.getSnapshot();
  const devices = ComfoconnectMapper.toDevices(snapshot);
  const entities = ComfoconnectMapper.toEntities(snapshot);

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

  const runtime = await new ComfoconnectIntegration().setup({ name: 'ComfoAir Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'comfoconnect', service: 'status', target: {} });
  const statusSnapshot = status.data as IComfoconnectSnapshot;

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

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

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

export default tap.start();
