import { expect, tap } from '@git.zone/tstest/tapbundle';
import { IbeaconClient, IbeaconConfigFlow, IbeaconIntegration, IbeaconMapper, HomeAssistantIbeaconIntegration, createIbeaconDiscoveryDescriptor, ibeaconProfile, type IIbeaconSnapshot, type TIbeaconRawData } from '../../ts/integrations/ibeacon/index.js';

const rawData: TIbeaconRawData = {
  device: {
    id: 'fda50693-a4e2-4fb1-afcf-c6eb07647825_10_42_AA:BB:CC:DD:EE:FF',
    name: 'Desk Beacon',
    manufacturer: 'Apple',
    model: 'iBeacon advertisement',
    serialNumber: 'fda50693-a4e2-4fb1-afcf-c6eb07647825_10_42',
    attributes: {
      address: 'AA:BB:CC:DD:EE:FF',
      source: 'bluetooth',
      uuid: 'fda50693-a4e2-4fb1-afcf-c6eb07647825',
      major: 10,
      minor: 42,
    },
  },
  entities: [
    { id: 'presence', name: 'Presence', platform: 'binary_sensor', state: true, deviceClass: 'presence' },
    { id: 'rssi', name: 'RSSI', platform: 'sensor', state: -63, unit: 'dBm', deviceClass: 'signal_strength', stateClass: 'measurement' },
    { id: 'power', name: 'Power', platform: 'sensor', state: -59, unit: 'dBm', deviceClass: 'signal_strength', stateClass: 'measurement' },
    { id: 'estimated_distance', name: 'Estimated Distance', platform: 'sensor', state: 1.8, unit: 'm', deviceClass: 'distance', stateClass: 'measurement' },
  ],
};

tap.test('matches manual iBeacon candidates and creates config flow output', async () => {
  const descriptor = createIbeaconDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'ibeacon-manual-match');
  const result = await matcher!.matches({ id: 'AA:BB:CC:DD:EE:FF', name: 'Desk iBeacon', metadata: { uuid: 'fda50693-a4e2-4fb1-afcf-c6eb07647825', rawData } }, {});

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

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

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

tap.test('maps iBeacon raw snapshots to runtime devices and entities', async () => {
  const client = new IbeaconClient({ name: 'iBeacon Runtime', rawData });
  const snapshot = await client.getSnapshot();
  const mappedSnapshot = IbeaconMapper.toSnapshotFromRaw({ name: 'iBeacon Runtime' }, rawData);
  const devices = IbeaconMapper.toDevices(mappedSnapshot);
  const entities = IbeaconMapper.toEntities(mappedSnapshot);

  expect(snapshot.online).toBeTrue();
  expect(mappedSnapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('ibeacon');
  expect(devices[0].manufacturer).toEqual('Apple');
  expect(entities.some((entityArg) => entityArg.id === 'binary_sensor.desk_beacon_presence')).toBeTrue();
  expect(entities.some((entityArg) => entityArg.id === 'sensor.desk_beacon_estimated_distance')).toBeTrue();
});

tap.test('exposes iBeacon read-only runtime, HA alias, and unsupported control', async () => {
  const integration = new IbeaconIntegration();
  const alias = new HomeAssistantIbeaconIntegration();
  expect(alias instanceof IbeaconIntegration).toBeTrue();
  expect(alias.domain).toEqual('ibeacon');
  expect(integration.status).toEqual('read-only-runtime');
  expect(ibeaconProfile.metadata.configFlow).toEqual(true);
  expect(ibeaconProfile.metadata.requirements).toEqual(['ibeacon-ble==1.2.0']);
  expect(ibeaconProfile.metadata.dependencies).toEqual(['bluetooth_adapters']);

  const runtime = await integration.setup({ name: 'iBeacon Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'ibeacon', service: 'status', target: {} });
  const refresh = await runtime.callService!({ domain: 'ibeacon', service: 'refresh', target: {} });
  const snapshot = status.data as IIbeaconSnapshot;

  expect(status.success).toBeTrue();
  expect(refresh.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('Desk Beacon');

  const command = await runtime.callService!({ domain: 'ibeacon', service: 'turn_on', target: {} });
  expect(command.success).toBeFalse();
  expect(command.error!).toContain('requires an injected client.execute() or commandExecutor');
  await runtime.destroy();
});

export default tap.start();
