import { expect, tap } from '@git.zone/tstest/tapbundle';
import { AuroraAbbPoweroneClient, AuroraAbbPoweroneConfigFlow, AuroraAbbPoweroneIntegration, AuroraAbbPoweroneMapper, createAuroraAbbPoweroneDiscoveryDescriptor, type IAuroraAbbPoweroneSnapshot } from '../../ts/integrations/aurora_abb_powerone/index.js';

const rawData = {
  "power": 1620,
  "energy_today": 6.2,
  "online": true
};

tap.test('matches manual Aurora ABB PowerOne Solar PV candidates and creates config flow output', async () => {
  const descriptor = createAuroraAbbPoweroneDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'aurora_abb_powerone-manual-match');
  const result = await matcher!.matches({ host: 'aurora_abb_powerone.local', name: 'Aurora ABB PowerOne Solar PV', metadata: { rawData } }, {});

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

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

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

tap.test('maps Aurora ABB PowerOne Solar PV raw snapshots to runtime devices and entities', async () => {
  const client = new AuroraAbbPoweroneClient({ name: 'Aurora ABB PowerOne Solar PV Test', rawData });
  const snapshot = await client.getSnapshot();
  const devices = AuroraAbbPoweroneMapper.toDevices(snapshot);
  const entities = AuroraAbbPoweroneMapper.toEntities(snapshot);

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

tap.test('exposes Aurora ABB PowerOne Solar PV runtime services without fake live control', async () => {
  const runtime = await new AuroraAbbPoweroneIntegration().setup({ name: 'Aurora ABB PowerOne Solar PV Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'aurora_abb_powerone', service: 'status', target: {} });
  const snapshot = status.data as IAuroraAbbPoweroneSnapshot;

  expect(status.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('Aurora ABB PowerOne Solar PV Runtime');

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

  const executorRuntime = await new AuroraAbbPoweroneIntegration().setup({
    name: 'Aurora ABB PowerOne Solar PV Executor',
    rawData,
    commandExecutor: {
      execute: async (requestArg) => ({ success: true, data: { service: requestArg.service } }),
    },
  }, {});
  const executed = await executorRuntime.callService!({ domain: 'aurora_abb_powerone', service: 'turn_on', target: {} });
  expect(executed.success).toBeTrue();
  expect((executed.data as { service: string }).service).toEqual('turn_on');
  await executorRuntime.destroy();
});

export default tap.start();
