import { expect, tap } from '@git.zone/tstest/tapbundle';
import { DynaliteClient, DynaliteConfigFlow, DynaliteIntegration, DynaliteMapper, HomeAssistantDynaliteIntegration, dynaliteProfile, createDynaliteDiscoveryDescriptor, type IDynaliteSnapshot } from '../../ts/integrations/dynalite/index.js';

const rawData = {
  device: {
    id: 'dynalite-gateway-1',
    name: 'Dynalite Gateway',
    manufacturer: 'Dynalite',
    model: 'Dynalite gateway',
    host: '192.0.2.10',
    port: 12345,
  },
  entities: [
    { id: 'area_1_channel_1', name: 'Lobby Channel 1', platform: 'light', state: 180, unit: 'level', writable: true },
    { id: 'area_1_cover', name: 'Lobby Blind', platform: 'cover', state: 75, unit: '%', writable: true },
  ],
};

tap.test('matches manual Dynalite candidates and creates config flow output', async () => {
  const descriptor = createDynaliteDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'dynalite-manual-match');
  const result = await matcher!.matches({ host: '192.0.2.10', name: 'Philips Dynalite Gateway', metadata: { rawData } }, {});

  expect(result.matched).toBeTrue();
  expect(result.candidate?.integrationDomain).toEqual('dynalite');
  expect(result.candidate?.port).toEqual(12345);

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

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

tap.test('maps Dynalite raw snapshots to runtime devices and entities', async () => {
  const snapshot = DynaliteMapper.toSnapshotFromRaw({ name: 'Dynalite Test', rawData }, rawData);
  const devices = DynaliteMapper.toDevices(snapshot);
  const entities = DynaliteMapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(devices[0].integrationDomain).toEqual('dynalite');
  expect(devices[0].manufacturer).toEqual('Dynalite');
  expect(entities.some((entityArg) => entityArg.platform === 'cover')).toBeTrue();
  expect(entities.some((entityArg) => entityArg.platform === 'light')).toBeTrue();
});

tap.test('exposes Dynalite runtime services and executor-only controls', async () => {
  expect(new HomeAssistantDynaliteIntegration().domain).toEqual('dynalite');
  expect(dynaliteProfile.status).toEqual('control-runtime');
  expect(dynaliteProfile.metadata.configFlow).toBeTrue();
  expect(dynaliteProfile.metadata.requirements).toEqual(['dynalite-devices==0.1.47', 'dynalite-panel==0.0.4']);

  const client = new DynaliteClient({ name: 'Dynalite Client', rawData });
  expect((await client.getSnapshot()).source).toEqual('manual');

  const runtime = await new DynaliteIntegration().setup({ name: 'Dynalite Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'dynalite', service: 'status', target: {} });
  const snapshot = status.data as IDynaliteSnapshot;

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

  const controlCommand = await runtime.callService!({ domain: 'dynalite', service: 'request_area_preset', target: {}, data: { area: 1 } });
  expect(controlCommand.success).toBeFalse();
  expect(controlCommand.error?.includes('requires an injected client.execute() or commandExecutor')).toBeTrue();
  await runtime.destroy();
});

export default tap.start();
