import { expect, tap } from '@git.zone/tstest/tapbundle';
import { EgaugeClient, EgaugeConfigFlow, EgaugeIntegration, EgaugeMapper, HomeAssistantEgaugeIntegration, createEgaugeDiscoveryDescriptor, egaugeProfile, type IEgaugeSnapshot } from '../../ts/integrations/egauge/index.js';

const rawData = {
  device: {
    id: 'egauge-12345',
    name: 'eGauge Main Panel',
    manufacturer: 'eGauge Systems',
    model: 'eGauge Energy Monitor',
    serialNumber: '12345',
    host: 'egauge.local',
  },
  entities: [
    { id: 'main_power_power', name: 'Main Power', platform: 'sensor', state: 1234, unit: 'W', deviceClass: 'power', stateClass: 'measurement' },
    { id: 'main_power_energy', name: 'Main Energy', platform: 'sensor', state: 987654, unit: 'J', deviceClass: 'energy', stateClass: 'total_increasing' },
  ],
};

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

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

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

  const done = await (await new EgaugeConfigFlow().start(result.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.host).toEqual('egauge.local');
  expect(done.config?.rawData).toEqual(rawData);
});

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

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('egauge');
  expect(devices[0].manufacturer).toEqual('eGauge Systems');
  expect(entities.length).toEqual(2);
  expect(entities[0].platform).toEqual('sensor');
});

tap.test('exposes eGauge read-only runtime, HA alias, and unsupported control without executor', async () => {
  expect(new HomeAssistantEgaugeIntegration().domain).toEqual('egauge');
  expect(new HomeAssistantEgaugeIntegration().status).toEqual('read-only-runtime');
  expect(egaugeProfile.metadata.qualityScale).toEqual('bronze');
  expect(egaugeProfile.metadata.requirements).toEqual(['egauge-async==0.4.0']);

  const runtime = await new EgaugeIntegration().setup({ name: 'eGauge Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'egauge', service: 'status', target: {} });
  const snapshot = status.data as IEgaugeSnapshot;
  const refresh = await runtime.callService!({ domain: 'egauge', service: 'refresh', target: {} });

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

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

export default tap.start();
