import { expect, tap } from '@git.zone/tstest/tapbundle';
import { EverlightsClient, EverlightsConfigFlow, EverlightsIntegration, EverlightsMapper, HomeAssistantEverlightsIntegration, createEverlightsDiscoveryDescriptor, everlightsProfile, type IEverlightsSnapshot } from '../../ts/integrations/everlights/index.js';

const rawData = {
  device: {
    id: 'everlights-001122334455',
    name: 'EverLights 00:11:22:33:44:55',
    manufacturer: 'EverLights',
    model: 'EverLights controller',
    serialNumber: '00:11:22:33:44:55',
    host: '192.0.2.20',
    protocol: 'http',
  },
  entities: [
    { id: 'zone_1', name: 'Zone 1', platform: 'light', state: true, writable: true, attributes: { brightness: 255, hsColor: [120, 80], effect: 'Rainbow', effectList: ['Rainbow', 'Solid'] } },
    { id: 'zone_2', name: 'Zone 2', platform: 'light', state: false, writable: true, attributes: { brightness: 0, effectList: ['Rainbow', 'Solid'] } },
  ],
};

tap.test('matches manual EverLights candidates and creates config flow output', async () => {
  const descriptor = createEverlightsDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'everlights-manual-match');
  const result = await matcher!.matches({ host: '192.0.2.20', name: 'EverLights Controller', metadata: { rawData } }, {});

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

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

  const done = await (await new EverlightsConfigFlow().start(result.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.host).toEqual('192.0.2.20');
  expect(done.config?.transport).toEqual('snapshot');
});

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

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('everlights');
  expect(devices[0].manufacturer).toEqual('EverLights');
  expect(entities.length).toEqual(2);
});

tap.test('exposes EverLights runtime, HA alias, and unsupported control', async () => {
  const integration = new EverlightsIntegration();
  const alias = new HomeAssistantEverlightsIntegration();
  expect(alias.domain).toEqual('everlights');
  expect(integration.status).toEqual('control-runtime');
  expect(everlightsProfile.metadata.configFlow).toEqual(false);
  expect(everlightsProfile.metadata.requirements).toEqual(['pyeverlights==0.1.0']);

  const runtime = await integration.setup({ name: 'EverLights Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'everlights', service: 'status', target: {} });
  const snapshot = status.data as IEverlightsSnapshot;

  expect(status.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('EverLights 00:11:22:33:44:55');

  const command = await runtime.callService!({ domain: 'light', service: 'turn_on', target: {}, data: { brightness: 255 } });
  expect(command.success).toBeFalse();
  expect(Boolean(command.error)).toBeTrue();
  await runtime.destroy();
});

export default tap.start();
