import { expect, tap } from '@git.zone/tstest/tapbundle';
import { BtSmarthubClient, BtSmarthubConfigFlow, BtSmarthubIntegration, BtSmarthubMapper, HomeAssistantBtSmarthubIntegration, btSmarthubProfile, createBtSmarthubDiscoveryDescriptor, type IBtSmarthubRawData, type IBtSmarthubSnapshot } from '../../ts/integrations/bt_smarthub/index.js';

const rawData: IBtSmarthubRawData = {
  clients: 11,
  wan_up: true,
  wifi_enabled: true
};

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

  expect(integration.domain).toEqual('bt_smarthub');
  expect(integration.displayName).toEqual('BT Smart Hub');
  expect(integration.status).toEqual('read-only-runtime');
  expect(integration.metadata.iotClass).toEqual('local_polling');
  expect(integration.metadata.qualityScale).toEqual('legacy');
  expect(btSmarthubProfile.manufacturer).toEqual('BT');
  expect(btSmarthubProfile.model).toEqual('Smart Hub');
  expect(btSmarthubProfile.platforms).toEqual(['sensor', 'binary_sensor']);
  expect(btSmarthubProfile.discoveryKeywords).toEqual(['bt', 'smart hub', 'router']);
  expect(btSmarthubProfile.metadata.requirements).toEqual(['btsmarthub-devicelist==0.2.3']);
  expect(btSmarthubProfile.metadata.sample).toEqual(rawData);
});

tap.test('matches BT Smart Hub candidates and creates config flow output', async () => {
  const descriptor = createBtSmarthubDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'bt_smarthub-manual-match');
  const result = await matcher!.matches({ host: 'bt-smart-hub.local', name: 'BT Smart Hub', metadata: { rawData } }, {});

  expect(result.matched).toBeTrue();
  expect(result.candidate?.integrationDomain).toEqual('bt_smarthub');
  expect(result.candidate?.manufacturer).toEqual('BT');
  expect(result.candidate?.model).toEqual('Smart Hub');

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

  const done = await (await new BtSmarthubConfigFlow().start(result.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.host).toEqual('bt-smart-hub.local');
  expect(done.config?.rawData).toEqual(rawData);
});

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

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('bt_smarthub');
  expect(devices[0].manufacturer).toEqual('BT');
  expect(devices[0].model).toEqual('Smart Hub');
  expect(entities.find((entityArg) => entityArg.name === 'Clients')?.state).toEqual(11);
  expect(entities.find((entityArg) => entityArg.name === 'Wifi Enabled')?.platform).toEqual('binary_sensor');

  const runtime = await new BtSmarthubIntegration().setup({ name: 'BT Smart Hub Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'bt_smarthub', service: 'status', target: {} });
  const statusSnapshot = status.data as IBtSmarthubSnapshot;

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

export default tap.start();
