import { expect, tap } from '@git.zone/tstest/tapbundle';
import { DanfossAirClient, DanfossAirConfigFlow, DanfossAirIntegration, DanfossAirMapper, HomeAssistantDanfossAirIntegration, danfossAirProfile, createDanfossAirDiscoveryDescriptor, type IDanfossAirSnapshot } from '../../ts/integrations/danfoss_air/index.js';

const rawData = {
  exhaustTemperature: 21.4,
  outdoorTemperature: 7.8,
  supplyTemperature: 20.1,
  extractTemperature: 22,
  humidity: 44.5,
  filterPercent: 82,
  bypass: false,
  fan_step: 40,
  supply_fan_speed: 1600,
  exhaust_fan_speed: 1580,
  away_mode: false,
  boost: true,
  automatic_bypass: false,
  battery_percent: 91,
};

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

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

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

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

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

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

tap.test('exposes Danfoss Air delegated control runtime without fake live control', async () => {
  expect(new HomeAssistantDanfossAirIntegration().domain).toEqual('danfoss_air');
  expect(danfossAirProfile.status).toEqual('control-runtime');
  expect(danfossAirProfile.metadata.configFlow).toBeFalse();
  expect(danfossAirProfile.metadata.qualityScale).toEqual('legacy');
  expect(danfossAirProfile.metadata.requirements).toEqual(['pydanfossair==0.1.0']);

  const runtime = await new DanfossAirIntegration().setup({ name: 'Danfoss Air Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'switch', service: 'status', target: {} });
  const snapshot = status.data as IDanfossAirSnapshot;

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

  const controlCommand = await runtime.callService!({ domain: 'switch', service: 'turn_on', target: {} });
  expect(controlCommand.success).toBeFalse();
  expect(Boolean(controlCommand.error?.includes('requires an injected'))).toBeTrue();
  await runtime.destroy();
});

export default tap.start();
