import { expect, tap } from '@git.zone/tstest/tapbundle';
import { EgardiaClient, EgardiaConfigFlow, EgardiaIntegration, EgardiaMapper, HomeAssistantEgardiaIntegration, createEgardiaDiscoveryDescriptor, egardiaProfile, type IEgardiaSnapshot } from '../../ts/integrations/egardia/index.js';

const rawData = {
  state: 'ARM',
  version: 'GATE-01',
  sensors: {
    front: { id: 'front', name: 'Front Door', type: 'Door Contact', state: true },
    hall: { id: 'hall', name: 'Hall', type: 'IR Sensor', state: false },
  },
};

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

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

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

  const done = await (await new EgardiaConfigFlow().start(result.candidate!, {})).submit!({ username: 'user', password: 'pass' });
  expect(done.kind).toEqual('done');
  expect(done.config?.host).toEqual('egardia.local');
});

tap.test('maps Egardia raw snapshots to alarm devices and binary sensor entities', async () => {
  const client = new EgardiaClient({ host: 'egardia.local', name: 'House Alarm', username: 'user', password: 'pass', rawData });
  const snapshot = await client.getSnapshot();
  const devices = EgardiaMapper.toDevices(snapshot);
  const entities = EgardiaMapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(devices[0].integrationDomain).toEqual('egardia');
  expect(devices[0].manufacturer).toEqual('Egardia');
  expect(entities.length).toEqual(3);
  expect(entities[0].state).toEqual('armed_away');
  expect(entities[1].platform).toEqual('binary_sensor');
  expect(entities[1].attributes?.deviceClass).toEqual('opening');
});

tap.test('exposes Egardia runtime, HA alias, and explicit unsupported control without executor', async () => {
  expect(new HomeAssistantEgardiaIntegration().domain).toEqual('egardia');
  expect(egardiaProfile.status).toEqual('control-runtime');
  expect(egardiaProfile.metadata.configFlow).toEqual(false);
  expect(egardiaProfile.metadata.requirements).toEqual(['pythonegardia==1.0.52']);

  const runtime = await new EgardiaIntegration().setup({ name: 'House Alarm', rawData }, {});
  const status = await runtime.callService!({ domain: 'egardia', service: 'status', target: {} });
  const snapshot = status.data as IEgardiaSnapshot;

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

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

export default tap.start();
