import { expect, tap } from '@git.zone/tstest/tapbundle';
import { EnergeniePowerSocketsClient, EnergeniePowerSocketsConfigFlow, EnergeniePowerSocketsIntegration, EnergeniePowerSocketsMapper, HomeAssistantEnergeniePowerSocketsIntegration, createEnergeniePowerSocketsDiscoveryDescriptor, energeniePowerSocketsProfile, type IEnergeniePowerSocketsSnapshot } from '../../ts/integrations/energenie_power_sockets/index.js';

const rawData = {
  device_id: 'EGPS-1234',
  name: 'Desk Power Strip',
  manufacturer: 'Energenie',
  numberOfSockets: 4,
  sockets: [true, false, true, false],
  sw_version: '0.2.5',
};

tap.test('matches manual Energenie Power Sockets candidates and creates config flow output', async () => {
  const descriptor = createEnergeniePowerSocketsDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'energenie_power_sockets-manual-match');
  const result = await matcher!.matches({ name: 'Energenie Power Sockets', metadata: { rawData } }, {});

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

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

  const done = await (await new EnergeniePowerSocketsConfigFlow().start(result.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.name).toEqual('Energenie Power Sockets');
  expect(done.config?.rawData).toEqual(rawData);
});

tap.test('maps Energenie raw snapshots to switch devices and entities', async () => {
  const client = new EnergeniePowerSocketsClient({ deviceApiId: 'EGPS-1234', rawData });
  const snapshot = await client.getSnapshot();
  const devices = EnergeniePowerSocketsMapper.toDevices(snapshot);
  const entities = EnergeniePowerSocketsMapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(devices[0].integrationDomain).toEqual('energenie_power_sockets');
  expect(devices[0].manufacturer).toEqual('Energenie');
  expect(devices[0].features.length).toEqual(4);
  expect(devices[0].features[0].writable).toBeTrue();
  expect(entities.length).toEqual(4);
  expect(entities[0].platform).toEqual('switch');
  expect(entities[0].state).toBeTrue();
});

tap.test('exposes Energenie runtime, HA alias, and explicit unsupported control without executor', async () => {
  const integration = new EnergeniePowerSocketsIntegration();
  const alias = new HomeAssistantEnergeniePowerSocketsIntegration();
  expect(alias.domain).toEqual('energenie_power_sockets');
  expect(integration.status).toEqual('control-runtime');
  expect(energeniePowerSocketsProfile.metadata.configFlow).toEqual(true);
  expect(energeniePowerSocketsProfile.metadata.requirements).toEqual(['pyegps==0.2.5']);

  const runtime = await integration.setup({ deviceApiId: 'EGPS-1234', rawData }, {});
  const status = await runtime.callService!({ domain: 'energenie_power_sockets', service: 'status', target: {} });
  const snapshot = status.data as IEnergeniePowerSocketsSnapshot;

  expect(status.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('Desk Power Strip');

  const command = await runtime.callService!({ domain: 'switch', service: 'turn_off', target: { entityId: 'switch.desk_power_strip_socket_0' } });
  expect(command.success).toBeFalse();
  expect(command.error?.includes('requires an injected client.execute() or commandExecutor')).toBeTrue();
  await runtime.destroy();
});

export default tap.start();
