import { expect, tap } from '@git.zone/tstest/tapbundle';
import { DormakabaDkeyClient, DormakabaDkeyConfigFlow, DormakabaDkeyIntegration, DormakabaDkeyMapper, HomeAssistantDormakabaDkeyIntegration, createDormakabaDkeyDiscoveryDescriptor, dormakabaDkeyProfile, type IDormakabaDkeySnapshot } from '../../ts/integrations/dormakaba_dkey/index.js';

const rawData = {
  battery_level: 88,
  door_position_open: false,
  security_locked: true,
  locked: true,
};

tap.test('matches manual Dormakaba dKey candidates and creates config flow output', async () => {
  const descriptor = createDormakabaDkeyDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'dormakaba_dkey-manual-match');
  const result = await matcher!.matches({ id: 'AA:BB:CC:DD:EE:FF', name: 'Dormakaba dKey', metadata: { rawData, address: 'AA:BB:CC:DD:EE:FF' } }, {});

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

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

  const done = await (await new DormakabaDkeyConfigFlow().start(result.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.name).toEqual('Dormakaba dKey');
  expect(done.config?.uniqueId).toEqual('AA:BB:CC:DD:EE:FF');
});

tap.test('maps Dormakaba dKey raw snapshots to runtime devices and entities', async () => {
  const client = new DormakabaDkeyClient({ name: 'Dormakaba dKey Test', uniqueId: 'AA_BB_CC_DD_EE_FF', rawData });
  const snapshot = await client.getSnapshot();
  const devices = DormakabaDkeyMapper.toDevices(snapshot);
  const entities = DormakabaDkeyMapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('dormakaba_dkey');
  expect(devices[0].manufacturer).toEqual('Dormakaba');
  expect(entities.length > 0).toBeTrue();
});

tap.test('exposes Dormakaba dKey runtime services without fake live control', async () => {
  expect(new HomeAssistantDormakabaDkeyIntegration().domain).toEqual('dormakaba_dkey');
  expect(dormakabaDkeyProfile.status).toEqual('control-runtime');
  expect(dormakabaDkeyProfile.metadata.configFlow).toBeTrue();
  expect(dormakabaDkeyProfile.metadata.dependencies).toEqual(['bluetooth_adapters']);
  expect(dormakabaDkeyProfile.metadata.requirements).toEqual(['py-dormakaba-dkey==1.0.6']);

  const runtime = await new DormakabaDkeyIntegration().setup({ name: 'Dormakaba dKey Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'dormakaba_dkey', service: 'status', target: {} });
  const snapshot = status.data as IDormakabaDkeySnapshot;

  expect(status.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('Dormakaba dKey Runtime');

  const liveCommand = await runtime.callService!({ domain: 'lock', service: 'unlock', target: {} });
  expect(liveCommand.success).toBeFalse();
  await runtime.destroy();
});

export default tap.start();
