import { expect, tap } from '@git.zone/tstest/tapbundle';
import { GoveeBleClient, GoveeBleConfigFlow, GoveeBleIntegration, GoveeBleMapper, HomeAssistantGoveeBleIntegration, createGoveeBleDiscoveryDescriptor, goveeBleProfile, type IGoveeBleSnapshot, type TGoveeBleRawData } from '../../ts/integrations/govee_ble/index.js';

const rawData: TGoveeBleRawData = {
  address: 'AA:BB:CC:DD:EE:FF',
  deviceType: 'H5075',
  name: 'Govee H5075',
  sensors: {
    temperature: 21.6,
    humidity: 48,
    battery: 91,
    rssi: -63,
  },
  binarySensors: {
    motion: false,
  },
};

tap.test('matches manual Govee BLE candidates and creates config flow output', async () => {
  const descriptor = createGoveeBleDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'govee_ble-manual-match');
  const result = await matcher!.matches({ source: 'manual', id: 'AA:BB:CC:DD:EE:FF', name: 'Govee H5075', metadata: { rawData } }, {});

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

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

  const done = await (await new GoveeBleConfigFlow().start(result.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.name).toEqual('Govee H5075');
  expect(done.config?.rawData).toEqual(rawData);
});

tap.test('maps Govee BLE raw snapshots to runtime devices and entities', async () => {
  const client = new GoveeBleClient({ name: 'Govee H5075', rawData });
  const snapshot = await client.getSnapshot();
  const mappedSnapshot = GoveeBleMapper.toSnapshotFromRaw({ name: 'Govee H5075' }, rawData);
  const devices = GoveeBleMapper.toDevices(mappedSnapshot);
  const entities = GoveeBleMapper.toEntities(mappedSnapshot);

  expect(snapshot.online).toBeTrue();
  expect(mappedSnapshot.device.serialNumber).toEqual('AA:BB:CC:DD:EE:FF');
  expect(devices[0].integrationDomain).toEqual('govee_ble');
  expect(devices[0].manufacturer).toEqual('Govee');
  expect(entities.some((entityArg) => entityArg.uniqueId.endsWith('_temperature') && entityArg.state === 21.6)).toBeTrue();
  expect(entities.some((entityArg) => entityArg.platform === 'binary_sensor' && entityArg.name === 'Motion')).toBeTrue();
});

tap.test('exposes Govee BLE read-only runtime, HA alias, and unsupported control', async () => {
  const integration = new GoveeBleIntegration();
  const alias = new HomeAssistantGoveeBleIntegration();
  expect(alias instanceof GoveeBleIntegration).toBeTrue();
  expect(alias.domain).toEqual('govee_ble');
  expect(integration.status).toEqual('read-only-runtime');
  expect(goveeBleProfile.metadata.configFlow).toEqual(true);
  expect(goveeBleProfile.metadata.requirements).toEqual(['govee-ble==1.2.0']);

  const runtime = await integration.setup({ name: 'Govee H5075', rawData }, {});
  const status = await runtime.callService!({ domain: 'govee_ble', service: 'status', target: {} });
  const refresh = await runtime.callService!({ domain: 'govee_ble', service: 'refresh', target: {} });
  const snapshot = status.data as IGoveeBleSnapshot;

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

  const command = await runtime.callService!({ domain: 'govee_ble', 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();
