import { expect, tap } from '@git.zone/tstest/tapbundle';
import { EkeybionyxClient, EkeybionyxConfigFlow, EkeybionyxIntegration, EkeybionyxMapper, HomeAssistantEkeybionyxIntegration, createEkeybionyxDiscoveryDescriptor, ekeybionyxProfile, type IEkeybionyxSnapshot } from '../../ts/integrations/ekeybionyx/index.js';

const rawData = {
  device: {
    id: 'ekey-system-front-door',
    name: 'Front Door ekey bionyx',
    manufacturer: 'ekey',
    model: 'bionyx',
  },
  entities: [
    { id: 'front_door_event', name: 'Front Door Event', platform: 'sensor', state: 'event happened', attributes: { webhookId: 'webhook-front-door', ekeyId: 'ekey-front-door' } },
    { id: 'configured_webhooks', name: 'Configured Webhooks', platform: 'sensor', state: 1 },
  ],
};

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

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

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

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

tap.test('maps ekey bionyx local push snapshots to devices and entities', async () => {
  const client = new EkeybionyxClient({ name: 'ekey Runtime', rawData });
  const snapshot = await client.getSnapshot();
  const devices = EkeybionyxMapper.toDevices(snapshot);
  const entities = EkeybionyxMapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('ekeybionyx');
  expect(devices[0].manufacturer).toEqual('ekey');
  expect(entities.length).toEqual(2);
  expect(entities[0].state).toEqual('event happened');
});

tap.test('exposes ekey bionyx read-only runtime, HA alias, and unsupported control without executor', async () => {
  expect(new HomeAssistantEkeybionyxIntegration().domain).toEqual('ekeybionyx');
  expect(new HomeAssistantEkeybionyxIntegration().status).toEqual('read-only-runtime');
  expect(ekeybionyxProfile.metadata.qualityScale).toEqual('bronze');
  expect(ekeybionyxProfile.metadata.requirements).toEqual(['ekey-bionyxpy==1.0.1']);
  expect(ekeybionyxProfile.metadata.dependencies).toEqual(['application_credentials', 'http']);

  const runtime = await new EkeybionyxIntegration().setup({ name: 'ekey Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'ekeybionyx', service: 'status', target: {} });
  const snapshot = status.data as IEkeybionyxSnapshot;
  const refresh = await runtime.callService!({ domain: 'ekeybionyx', service: 'refresh', target: {} });

  expect(status.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect(refresh.success).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('Front Door ekey bionyx');

  const controlCommand = await runtime.callService!({ domain: 'ekeybionyx', service: 'turn_on', target: {} });
  expect(controlCommand.success).toBeFalse();
  await runtime.destroy();
});

export default tap.start();
