import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HitronCodaClient, HitronCodaConfigFlow, HitronCodaIntegration, HitronCodaMapper, HomeAssistantHitronCodaIntegration, createHitronCodaDiscoveryDescriptor, hitronCodaProfile, type IHitronCodaSnapshot, type THitronCodaRawData } from '../../ts/integrations/hitron_coda/index.js';

const rawData: THitronCodaRawData = [
  { macAddr: 'aa:bb:cc:dd:ee:01', hostName: 'phone', ipAddr: '192.0.2.10', type: 'wifi' },
  { macAddr: 'aa:bb:cc:dd:ee:02', hostName: 'laptop', ipAddr: '192.0.2.11', type: 'ethernet' },
];

tap.test('matches manual Hitron CODA candidates and creates config flow output', async () => {
  const descriptor = createHitronCodaDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'hitron_coda-manual-match');
  const result = await matcher!.matches({ source: 'manual', host: '192.0.2.1', name: 'CODA Router', metadata: { rawData } }, {});

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

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

  const done = await (await new HitronCodaConfigFlow().start(result.candidate!, {})).submit!({ username: 'cusadmin', password: 'secret' });
  expect(done.kind).toEqual('done');
  expect(done.config?.host).toEqual('192.0.2.1');
  expect(done.config?.username).toEqual('cusadmin');
  expect(done.config?.rawData).toEqual(rawData);
});

tap.test('maps Hitron CODA raw snapshots to runtime devices and entities', async () => {
  const client = new HitronCodaClient({ name: 'CODA Router', host: '192.0.2.1', rawData });
  const snapshot = await client.getSnapshot();
  const mappedSnapshot = HitronCodaMapper.toSnapshotFromRaw({ name: 'CODA Router', host: '192.0.2.1' }, rawData);
  const devices = HitronCodaMapper.toDevices(mappedSnapshot);
  const entities = HitronCodaMapper.toEntities(mappedSnapshot);

  expect(snapshot.online).toBeTrue();
  expect(mappedSnapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('hitron_coda');
  expect(devices[0].manufacturer).toEqual('Hitron');
  expect(entities.some((entityArg) => entityArg.id === 'sensor.coda_router_connected_devices')).toBeTrue();
  expect(entities.some((entityArg) => entityArg.id === 'binary_sensor.coda_router_client_aa_bb_cc_dd_ee_01')).toBeTrue();
});

tap.test('exposes Hitron CODA read-only runtime, HA alias, and unsupported control', async () => {
  const integration = new HitronCodaIntegration();
  const alias = new HomeAssistantHitronCodaIntegration();
  expect(alias instanceof HitronCodaIntegration).toBeTrue();
  expect(alias.domain).toEqual('hitron_coda');
  expect(integration.status).toEqual('read-only-runtime');
  expect(hitronCodaProfile.metadata.configFlow).toEqual(false);
  expect(hitronCodaProfile.metadata.qualityScale).toEqual('legacy');
  expect(hitronCodaProfile.metadata.requirements).toEqual([]);

  const runtime = await integration.setup({ name: 'CODA Router', host: '192.0.2.1', rawData }, {});
  const status = await runtime.callService!({ domain: 'hitron_coda', service: 'status', target: {} });
  const refresh = await runtime.callService!({ domain: 'hitron_coda', service: 'refresh', target: {} });
  const snapshot = status.data as IHitronCodaSnapshot;

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

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