import { expect, tap } from '@git.zone/tstest/tapbundle';
import { EvilGeniusLabsClient, EvilGeniusLabsConfigFlow, EvilGeniusLabsIntegration, EvilGeniusLabsMapper, HomeAssistantEvilGeniusLabsIntegration, createEvilGeniusLabsDiscoveryDescriptor, evilGeniusLabsProfile, type IEvilGeniusLabsSnapshot } from '../../ts/integrations/evil_genius_labs/index.js';

const rawData = {
  device: {
    id: 'egl-wifi-123',
    name: 'Living Room Fibonacci',
    manufacturer: 'Evil Genius Labs',
    model: 'Fibonacci64',
  },
  entities: [
    {
      id: 'light',
      name: 'Living Room Fibonacci',
      platform: 'light',
      state: true,
      attributes: {
        brightness: 180,
        effect: 'Rainbow',
        rgbColor: [12, 34, 56],
      },
    },
  ],
};

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

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

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

  const done = await (await new EvilGeniusLabsConfigFlow().start(result.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.host).toEqual('evilgenius.local');
  expect(done.config?.path).toEqual('/all');
});

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

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('evil_genius_labs');
  expect(devices[0].manufacturer).toEqual('Evil Genius Labs');
  expect(entities[0].platform).toEqual('light');
});

tap.test('exposes Evil Genius Labs runtime services, HA alias, and executor-gated controls', async () => {
  const integration = new EvilGeniusLabsIntegration();
  const alias = new HomeAssistantEvilGeniusLabsIntegration();
  expect(alias.domain).toEqual('evil_genius_labs');
  expect(integration.status).toEqual('control-runtime');
  expect(evilGeniusLabsProfile.metadata.configFlow).toEqual(true);
  expect(evilGeniusLabsProfile.metadata.requirements).toEqual(['pyevilgenius==2.0.0']);
  expect(Object.prototype.hasOwnProperty.call(evilGeniusLabsProfile.metadata, 'qualityScale')).toBeTrue();
  expect(evilGeniusLabsProfile.metadata.qualityScale).toBeUndefined();

  const runtime = await integration.setup({ name: 'Evil Genius Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'light', service: 'status', target: {} });
  const snapshot = status.data as IEvilGeniusLabsSnapshot;

  expect(status.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('Living Room Fibonacci');

  const command = await runtime.callService!({ domain: 'light', service: 'turn_on', target: {} });
  expect(command.success).toBeFalse();
  expect(Boolean(command.error?.includes('requires an injected'))).toBeTrue();
  await runtime.destroy();
});

export default tap.start();
