import { expect, tap } from '@git.zone/tstest/tapbundle';
import { EmulatedHueClient, EmulatedHueConfigFlow, EmulatedHueIntegration, EmulatedHueMapper, HomeAssistantEmulatedHueIntegration, createEmulatedHueDiscoveryDescriptor, emulatedHueProfile, type IEmulatedHueSnapshot } from '../../ts/integrations/emulated_hue/index.js';

const rawData = {
  config: {
    name: 'HASS BRIDGE',
    ipaddress: '192.0.2.15:8300',
    mac: '00:00:00:00:00:00',
  },
  states: [
    { entity_id: 'light.kitchen', state: 'on', attributes: { friendly_name: 'Kitchen Hue', brightness: 128, supported_color_modes: ['brightness'] } },
    { entity_id: 'media_player.living_room', state: 'off', attributes: { friendly_name: 'Living Room', volume_level: 0.4 } },
  ],
};

tap.test('matches manual Emulated Hue candidates and creates config flow output', async () => {
  const descriptor = createEmulatedHueDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'emulated_hue-manual-match');
  const result = await matcher!.matches({ host: '192.0.2.15', name: 'HASS BRIDGE', metadata: { rawData } }, {});

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

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

  const done = await (await new EmulatedHueConfigFlow().start(result.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.host).toEqual('192.0.2.15');
  expect(done.config?.port).toEqual(8300);
});

tap.test('maps Emulated Hue raw HA states to bridge devices and entities', async () => {
  const client = new EmulatedHueClient({ rawData });
  const snapshot = await client.getSnapshot();
  const devices = EmulatedHueMapper.toDevices(snapshot);
  const entities = EmulatedHueMapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(devices[0].integrationDomain).toEqual('emulated_hue');
  expect(devices[0].manufacturer).toEqual('Home Assistant');
  expect(devices[0].name).toEqual('HASS BRIDGE');
  expect(entities.length).toEqual(2);
  expect(entities[0].platform).toEqual('light');
  expect(entities[0].state).toEqual(true);
  expect(entities[0].attributes?.brightness).toEqual(128);
});

tap.test('exposes Emulated Hue runtime services, HA alias, and executor-gated control', async () => {
  const integration = new EmulatedHueIntegration();
  const alias = new HomeAssistantEmulatedHueIntegration();
  expect(alias.domain).toEqual('emulated_hue');
  expect(integration.status).toEqual('control-runtime');
  expect(emulatedHueProfile.metadata.configFlow).toEqual(false);
  expect(emulatedHueProfile.metadata.dependencies).toEqual(['network']);
  expect(emulatedHueProfile.metadata.afterDependencies).toEqual(['http']);

  const runtime = await integration.setup({ rawData }, {});
  const status = await runtime.callService!({ domain: 'emulated_hue', service: 'status', target: {} });
  const snapshot = status.data as IEmulatedHueSnapshot;

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

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

export default tap.start();
