import { expect, tap } from '@git.zone/tstest/tapbundle';
import { BthomeClient, BthomeConfigFlow, BthomeIntegration, BthomeMapper, HomeAssistantBthomeIntegration, bthomeProfile, createBthomeDiscoveryDescriptor, type IBthomeRawData, type IBthomeSnapshot } from '../../ts/integrations/bthome/index.js';

const rawData: IBthomeRawData = {
  temperature: 20.4,
  humidity: 45,
  battery: 86
};

tap.test('exposes BTHome simple-local profile and Home Assistant alias', async () => {
  const integration = new HomeAssistantBthomeIntegration();

  expect(integration.domain).toEqual('bthome');
  expect(integration.displayName).toEqual('BTHome');
  expect(integration.status).toEqual('read-only-runtime');
  expect(integration.metadata.iotClass).toEqual('local_push');
  expect(bthomeProfile.manufacturer).toEqual('BTHome');
  expect(bthomeProfile.model).toEqual('BLE advertisement sensor');
  expect(bthomeProfile.platforms).toEqual(['sensor', 'binary_sensor']);
  expect(bthomeProfile.discoveryKeywords).toEqual(['bthome', 'bluetooth', 'ble']);
  expect(bthomeProfile.metadata.requirements).toEqual(['bthome-ble==3.17.0']);
  expect(bthomeProfile.metadata.dependencies).toEqual(['bluetooth_adapters']);
  expect(bthomeProfile.metadata.sample).toEqual(rawData);
});

tap.test('matches BTHome snapshot candidates and creates config flow output', async () => {
  const descriptor = createBthomeDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'bthome-manual-match');
  const result = await matcher!.matches({ name: 'BTHome', metadata: { rawData } }, {});

  expect(result.matched).toBeTrue();
  expect(result.candidate?.integrationDomain).toEqual('bthome');
  expect(result.candidate?.manufacturer).toEqual('BTHome');
  expect(result.candidate?.model).toEqual('BLE advertisement sensor');

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

  const done = await (await new BthomeConfigFlow().start(result.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.transport).toEqual('snapshot');
  expect(done.config?.rawData).toEqual(rawData);
});

tap.test('maps BTHome raw data to read-only runtime state', async () => {
  const client = new BthomeClient({ name: 'BTHome Test', rawData });
  const snapshot = await client.getSnapshot();
  const devices = BthomeMapper.toDevices(snapshot);
  const entities = BthomeMapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('bthome');
  expect(devices[0].manufacturer).toEqual('BTHome');
  expect(devices[0].model).toEqual('BLE advertisement sensor');
  expect(entities.find((entityArg) => entityArg.name === 'Temperature')?.state).toEqual(20.4);
  expect(entities.find((entityArg) => entityArg.name === 'Battery')?.state).toEqual(86);

  const runtime = await new BthomeIntegration().setup({ name: 'BTHome Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'bthome', service: 'status', target: {} });
  const statusSnapshot = status.data as IBthomeSnapshot;

  expect(status.success).toBeTrue();
  expect(statusSnapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('BTHome Runtime');
  expect((await runtime.callService!({ domain: 'bthome', service: 'turn_on', target: {} })).success).toBeFalse();
  await runtime.destroy();
});

export default tap.start();
