import { expect, tap } from '@git.zone/tstest/tapbundle';
import { BtHomeHub5Client, BtHomeHub5ConfigFlow, BtHomeHub5Integration, BtHomeHub5Mapper, HomeAssistantBtHomeHub5Integration, btHomeHub5Profile, createBtHomeHub5DiscoveryDescriptor, type IBtHomeHub5RawData, type IBtHomeHub5Snapshot } from '../../ts/integrations/bt_home_hub_5/index.js';

const rawData: IBtHomeHub5RawData = {
  clients: 7,
  wan_up: true,
  wifi_enabled: true
};

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

  expect(integration.domain).toEqual('bt_home_hub_5');
  expect(integration.displayName).toEqual('BT Home Hub 5');
  expect(integration.status).toEqual('read-only-runtime');
  expect(integration.metadata.iotClass).toEqual('local_polling');
  expect(integration.metadata.qualityScale).toEqual('legacy');
  expect(btHomeHub5Profile.manufacturer).toEqual('BT');
  expect(btHomeHub5Profile.model).toEqual('Home Hub 5');
  expect(btHomeHub5Profile.platforms).toEqual(['sensor', 'binary_sensor']);
  expect(btHomeHub5Profile.discoveryKeywords).toEqual(['bt', 'home hub', 'router']);
  expect(btHomeHub5Profile.metadata.requirements).toEqual(['bthomehub5-devicelist==0.1.1']);
  expect(btHomeHub5Profile.metadata.sample).toEqual(rawData);
});

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

  expect(result.matched).toBeTrue();
  expect(result.candidate?.integrationDomain).toEqual('bt_home_hub_5');
  expect(result.candidate?.manufacturer).toEqual('BT');
  expect(result.candidate?.model).toEqual('Home Hub 5');

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

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

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

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('bt_home_hub_5');
  expect(devices[0].manufacturer).toEqual('BT');
  expect(devices[0].model).toEqual('Home Hub 5');
  expect(entities.find((entityArg) => entityArg.name === 'Clients')?.state).toEqual(7);
  expect(entities.find((entityArg) => entityArg.name === 'Wan Up')?.platform).toEqual('binary_sensor');

  const runtime = await new BtHomeHub5Integration().setup({ name: 'BT Home Hub 5 Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'bt_home_hub_5', service: 'status', target: {} });
  const statusSnapshot = status.data as IBtHomeHub5Snapshot;

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

export default tap.start();
