import { expect, tap } from '@git.zone/tstest/tapbundle';
import { FingClient, FingConfigFlow, FingIntegration, FingMapper, HomeAssistantFingIntegration, createFingDiscoveryDescriptor, fingProfile, type IFingSnapshot } from '../../ts/integrations/fing/index.js';

const rawData = {
  device: {
    id: 'fing-agent-lab',
    name: 'Fing Agent Lab',
    manufacturer: 'Fing',
    model: 'Fing Agent',
    host: '192.168.1.10',
    port: 49090,
  },
  entities: [
    { id: 'phone_connected', name: 'Phone Connected', platform: 'binary_sensor', state: true, attributes: { mac: 'AA:BB:CC:DD:EE:01', ipAddress: '192.168.1.21', type: 'PHONE' } },
    { id: 'printer_connected', name: 'Printer Connected', platform: 'binary_sensor', state: false, attributes: { mac: 'AA:BB:CC:DD:EE:02', ipAddress: '192.168.1.35', type: 'PRINTER' } },
  ],
};

tap.test('matches manual Fing candidates and creates config flow output', async () => {
  const descriptor = createFingDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'fing-manual-match');
  const result = await matcher!.matches({ host: '192.168.1.10', name: 'Fing Agent', metadata: { rawData, apiKey: 'local-key' } }, {});

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

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

  const done = await (await new FingConfigFlow().start(result.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.host).toEqual('192.168.1.10');
  expect(done.config?.port).toEqual(49090);
  expect(done.config?.apiKey).toEqual('local-key');
});

tap.test('maps Fing raw snapshots to runtime devices and entities', async () => {
  const client = new FingClient({ rawData });
  const snapshot = await client.getSnapshot();
  const devices = FingMapper.toDevices(snapshot);
  const entities = FingMapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('fing');
  expect(devices[0].manufacturer).toEqual('Fing');
  expect(entities.length).toEqual(2);
  expect(entities[0].platform).toEqual('binary_sensor');
});

tap.test('exposes Fing read-only runtime, HA alias, and unsupported control', async () => {
  const integration = new FingIntegration();
  const alias = new HomeAssistantFingIntegration();
  expect(alias.domain).toEqual('fing');
  expect(integration.status).toEqual('read-only-runtime');
  expect(fingProfile.metadata.configFlow).toEqual(true);
  expect(fingProfile.metadata.requirements).toEqual(['fing_agent_api==1.1.0']);

  const runtime = await integration.setup({ name: 'Fing Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'fing', service: 'status', target: {} });
  const snapshot = status.data as IFingSnapshot;

  expect(status.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('Fing Agent Lab');

  const command = await runtime.callService!({ domain: 'fing', service: 'turn_on', target: {} });
  expect(command.success).toBeFalse();
  await runtime.destroy();
});

export default tap.start();
