import { expect, tap } from '@git.zone/tstest/tapbundle';
import { AdvantageAirClient, AdvantageAirConfigFlow, AdvantageAirIntegration, AdvantageAirMapper, createAdvantageAirDiscoveryDescriptor, type IAdvantageAirSnapshot } from '../../ts/integrations/advantage_air/index.js';

const rawData = {
  "zones": 5,
  "temperature": 22.4,
  "mode": "cool"
};

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

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

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

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

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

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

tap.test('exposes Advantage Air runtime services without fake live control', async () => {
  const runtime = await new AdvantageAirIntegration().setup({ name: 'Advantage Air Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'advantage_air', service: 'status', target: {} });
  const snapshot = status.data as IAdvantageAirSnapshot;

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

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

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

export default tap.start();
