import { expect, tap } from '@git.zone/tstest/tapbundle';
import { Concord232Client, Concord232ConfigFlow, Concord232Integration, Concord232Mapper, HomeAssistantConcord232Integration, concord232Profile, createConcord232DiscoveryDescriptor, type IConcord232Snapshot } from '../../ts/integrations/concord232/index.js';

const rawData = {
  "armed": false,
  "zones": 16,
  "fault": false
};

tap.test('exposes Concord232 profile and alias class', async () => {
  const integration = new HomeAssistantConcord232Integration();

  expect(integration.domain).toEqual('concord232');
  expect(integration.status).toEqual('control-runtime');
  expect(concord232Profile.displayName).toEqual('Concord232');
  expect(concord232Profile.manufacturer).toEqual('Interlogix');
  expect((concord232Profile.metadata.requirements as string[])[0]).toEqual('concord232==0.15.1');
});

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

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

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

  const done = await (await new Concord232ConfigFlow().start(result.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.host).toEqual('concord232.local');
});

tap.test('maps Concord232 snapshots and gates live control', async () => {
  const client = new Concord232Client({ name: 'Concord Runtime', rawData });
  const snapshot = await client.getSnapshot();
  const devices = Concord232Mapper.toDevices(snapshot);
  const entities = Concord232Mapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('concord232');
  expect(entities.length).toEqual(3);

  const runtime = await new Concord232Integration().setup({ name: 'Concord Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'concord232', service: 'status', target: {} });
  const statusSnapshot = status.data as IConcord232Snapshot;

  expect(status.success).toBeTrue();
  expect(statusSnapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('Concord Runtime');

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

  const executorRuntime = await new Concord232Integration().setup({
    name: 'Concord Executor',
    rawData,
    commandExecutor: {
      execute: async (requestArg) => ({ success: true, data: { service: requestArg.service } }),
    },
  }, {});
  const executed = await executorRuntime.callService!({ domain: 'alarm_control_panel', service: 'disarm', target: {} });
  expect(executed.success).toBeTrue();
  expect((executed.data as { service: string }).service).toEqual('disarm');
  await executorRuntime.destroy();
});

export default tap.start();
