import { expect, tap } from '@git.zone/tstest/tapbundle';
import { BeewiSmartclimClient, BeewiSmartclimConfigFlow, BeewiSmartclimIntegration, BeewiSmartclimMapper, HomeAssistantBeewiSmartclimIntegration, beewiSmartclimProfile, createBeewiSmartclimDiscoveryDescriptor, type IBeewiSmartclimSnapshot } from '../../ts/integrations/beewi_smartclim/index.js';

const rawData = {
  temperature: 20.7,
  humidity: 48,
  battery: 83
};

tap.test('matches manual BeeWi SmartClim BLE sensor candidates and creates config flow output', async () => {
  const descriptor = createBeewiSmartclimDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'beewi_smartclim-manual-match');
  const result = await matcher!.matches({ host: 'beewi-smartclim.local', name: 'BeeWi SmartClim BLE sensor', metadata: { rawData } }, {});

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

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

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

tap.test('maps BeeWi SmartClim raw snapshots to BLE sensor devices and entities', async () => {
  const client = new BeewiSmartclimClient({ name: 'BeeWi SmartClim Test', rawData });
  const snapshot = await client.getSnapshot();
  const devices = BeewiSmartclimMapper.toDevices(snapshot);
  const entities = BeewiSmartclimMapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(snapshot.entities.find((entityArg) => entityArg.id === 'temperature')?.state).toEqual(20.7);
  expect(snapshot.entities.find((entityArg) => entityArg.id === 'battery')?.state).toEqual(83);
  expect(devices[0].manufacturer).toEqual('BeeWi');
  expect(entities.length).toEqual(3);
});

tap.test('exposes BeeWi SmartClim read-only runtime services and preserves Home Assistant alias', async () => {
  const integration = new HomeAssistantBeewiSmartclimIntegration();
  expect(integration).toBeInstanceOf(BeewiSmartclimIntegration);
  expect(integration.status).toEqual('read-only-runtime');
  expect(beewiSmartclimProfile.metadata.requirements).toEqual(['beewi-smartclim==0.0.10']);
  expect((beewiSmartclimProfile.metadata.runtime as { controls: boolean }).controls).toBeFalse();

  const runtime = await integration.setup({ name: 'BeeWi SmartClim Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'beewi_smartclim', service: 'status', target: {} });
  const snapshot = status.data as IBeewiSmartclimSnapshot;

  expect(status.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].model).toEqual('SmartClim');

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

export default tap.start();
