import { expect, tap } from '@git.zone/tstest/tapbundle';
import { CiscoMobilityExpressClient, CiscoMobilityExpressConfigFlow, CiscoMobilityExpressIntegration, CiscoMobilityExpressMapper, HomeAssistantCiscoMobilityExpressIntegration, ciscoMobilityExpressProfile, createCiscoMobilityExpressDiscoveryDescriptor, type ICiscoMobilityExpressSnapshot } from '../../ts/integrations/cisco_mobility_express/index.js';

const rawData = {
  clients: 28,
  access_points: 4,
  online: true,
};

tap.test('exposes Cisco Mobility Express simple-local profile and alias', async () => {
  const alias = new HomeAssistantCiscoMobilityExpressIntegration();

  expect(alias.domain).toEqual('cisco_mobility_express');
  expect(ciscoMobilityExpressProfile.displayName).toEqual('Cisco Mobility Express');
  expect(ciscoMobilityExpressProfile.status).toEqual('read-only-runtime');
  expect(ciscoMobilityExpressProfile.platforms).toEqual(['sensor', 'binary_sensor']);
  expect(ciscoMobilityExpressProfile.metadata.requirements).toEqual(['ciscomobilityexpress==0.3.9']);
});

tap.test('matches manual Cisco Mobility Express candidates and creates config flow output', async () => {
  const descriptor = createCiscoMobilityExpressDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'cisco_mobility_express-manual-match');
  const result = await matcher!.matches({ host: 'cisco-mobility-express.local', name: 'Cisco Mobility Express', metadata: { rawData } }, {});

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

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

  const done = await (await new CiscoMobilityExpressConfigFlow().start(result.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.host).toEqual('cisco-mobility-express.local');
});

tap.test('maps Cisco Mobility Express samples and exposes read-only runtime services', async () => {
  const client = new CiscoMobilityExpressClient({ name: 'Cisco Mobility Express Test', rawData });
  const snapshot = await client.getSnapshot();
  const devices = CiscoMobilityExpressMapper.toDevices(snapshot);
  const entities = CiscoMobilityExpressMapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('cisco_mobility_express');
  expect(entities.length > 0).toBeTrue();

  const runtime = await new CiscoMobilityExpressIntegration().setup({ name: 'Cisco Mobility Express Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'cisco_mobility_express', service: 'status', target: {} });
  const statusSnapshot = status.data as ICiscoMobilityExpressSnapshot;

  expect(status.success).toBeTrue();
  expect(statusSnapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('Cisco Mobility Express Runtime');

  const unsupported = await runtime.callService!({ domain: 'cisco_mobility_express', service: 'reboot', target: {} });
  expect(unsupported.success).toBeFalse();
  await runtime.destroy();
});

export default tap.start();
