import { expect, tap } from '@git.zone/tstest/tapbundle';
import { EmoncmsClient, EmoncmsConfigFlow, EmoncmsIntegration, EmoncmsMapper, HomeAssistantEmoncmsIntegration, createEmoncmsDiscoveryDescriptor, emoncmsProfile, type IEmoncmsSnapshot } from '../../ts/integrations/emoncms/index.js';

const rawData = {
  device: {
    id: 'emoncms-local',
    name: 'Emoncms Local',
  },
  entities: [
    { id: 'feed_1_power', name: 'Feed 1 Power', platform: 'sensor', state: 421.2, unit: 'W' },
    { id: 'feed_2_energy', name: 'Feed 2 Energy', platform: 'sensor', state: 18.4, unit: 'kWh' },
  ],
};

tap.test('matches manual Emoncms candidates and creates config flow output', async () => {
  const descriptor = createEmoncmsDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'emoncms-manual-match');
  const result = await matcher!.matches({ host: 'emoncms.local', name: 'Emoncms', metadata: { rawData } }, {});

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

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

  const done = await (await new EmoncmsConfigFlow().start(result.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.host).toEqual('emoncms.local');
  expect(done.config?.path).toEqual('/feed/list.json');
});

tap.test('maps Emoncms raw snapshots to runtime devices and entities', async () => {
  const client = new EmoncmsClient({ name: 'Emoncms Runtime', rawData });
  const snapshot = await client.getSnapshot();
  const devices = EmoncmsMapper.toDevices(snapshot);
  const entities = EmoncmsMapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('emoncms');
  expect(devices[0].manufacturer).toEqual('OpenEnergyMonitor');
  expect(entities.length).toEqual(2);
});

tap.test('exposes Emoncms read-only runtime, HA alias, and unsupported control', async () => {
  const integration = new EmoncmsIntegration();
  const alias = new HomeAssistantEmoncmsIntegration();
  expect(alias.domain).toEqual('emoncms');
  expect(integration.status).toEqual('read-only-runtime');
  expect(emoncmsProfile.metadata.configFlow).toEqual(true);
  expect(emoncmsProfile.metadata.requirements).toEqual(['pyemoncms==0.1.3']);

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

  expect(status.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('Emoncms Local');

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

export default tap.start();
