import { expect, tap } from '@git.zone/tstest/tapbundle';
import { EufylifeBleClient, EufylifeBleConfigFlow, EufylifeBleIntegration, EufylifeBleMapper, HomeAssistantEufylifeBleIntegration, createEufylifeBleDiscoveryDescriptor, eufylifeBleProfile, type IEufylifeBleSnapshot } from '../../ts/integrations/eufylife_ble/index.js';

const rawData = {
  device: {
    id: 'eufy-scale-aabbccddeeff',
    name: 'Eufy Smart Scale',
    manufacturer: 'Eufy',
    model: 'eufy T9148',
    serialNumber: 'AA:BB:CC:DD:EE:FF',
    protocol: 'local',
  },
  entities: [
    { id: 'weight', name: 'Weight', platform: 'sensor', state: 72.4, unit: 'kg', deviceClass: 'weight', stateClass: 'measurement' },
    { id: 'real_time_weight', name: 'Real-time weight', platform: 'sensor', state: 72.1, unit: 'kg', deviceClass: 'weight', stateClass: 'measurement' },
    { id: 'heart_rate', name: 'Heart rate', platform: 'sensor', state: 68, unit: 'bpm' },
  ],
};

tap.test('matches manual EufyLife BLE candidates and creates config flow output', async () => {
  const descriptor = createEufylifeBleDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'eufylife_ble-manual-match');
  const result = await matcher!.matches({ id: 'AA:BB:CC:DD:EE:FF', name: 'eufy T9148', metadata: { address: 'AA:BB:CC:DD:EE:FF', model: 'eufy T9148', rawData } }, {});

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

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

  const done = await (await new EufylifeBleConfigFlow().start(result.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.uniqueId).toEqual('AA:BB:CC:DD:EE:FF');
  expect(done.config?.metadata?.model).toEqual('eufy T9148');
});

tap.test('maps EufyLife BLE raw snapshots to runtime devices and entities', async () => {
  const client = new EufylifeBleClient({ name: 'EufyLife Runtime', rawData });
  const snapshot = await client.getSnapshot();
  const devices = EufylifeBleMapper.toDevices(snapshot);
  const entities = EufylifeBleMapper.toEntities(snapshot);

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

tap.test('exposes EufyLife BLE read-only runtime, HA alias, and unsupported control', async () => {
  const integration = new EufylifeBleIntegration();
  const alias = new HomeAssistantEufylifeBleIntegration();
  expect(alias.domain).toEqual('eufylife_ble');
  expect(integration.status).toEqual('read-only-runtime');
  expect(eufylifeBleProfile.metadata.configFlow).toEqual(true);
  expect(eufylifeBleProfile.metadata.requirements).toEqual(['eufylife-ble-client==0.1.8']);

  const runtime = await integration.setup({ name: 'EufyLife Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'eufylife_ble', service: 'status', target: {} });
  const snapshot = status.data as IEufylifeBleSnapshot;

  expect(status.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('Eufy Smart Scale');

  const command = await runtime.callService!({ domain: 'eufylife_ble', service: 'turn_on', target: {} });
  expect(command.success).toBeFalse();
  expect(Boolean(command.error)).toBeTrue();
  await runtime.destroy();
});

export default tap.start();
