import { expect, tap } from '@git.zone/tstest/tapbundle';
import { BoschAlarmClient, BoschAlarmConfigFlow, BoschAlarmIntegration, BoschAlarmMapper, HomeAssistantBoschAlarmIntegration, boschAlarmProfile, createBoschAlarmDiscoveryDescriptor, type IBoschAlarmSnapshot } from '../../ts/integrations/bosch_alarm/index.js';

const rawData = {
  armed: false,
  alarm: false,
  zones: 12,
};

tap.test('defines the Bosch Alarm simple-local profile and alias', async () => {
  const integration = new HomeAssistantBoschAlarmIntegration();

  expect(integration).toBeInstanceOf(BoschAlarmIntegration);
  expect(integration.status).toEqual('control-runtime');
  expect(boschAlarmProfile.metadata.requirements).toEqual(['bosch-alarm-mode2==0.4.10']);
  expect(boschAlarmProfile.metadata.sample).toEqual(rawData);
  expect(boschAlarmProfile.serviceDomains).toEqual(['alarm_control_panel']);
  expect(boschAlarmProfile.controlServices).toEqual(['arm_away', 'arm_home', 'disarm']);
});

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

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

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

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

tap.test('maps Bosch Alarm samples and gates live alarm control', async () => {
  const client = new BoschAlarmClient({ name: 'Bosch Alarm Test', rawData });
  const snapshot = await client.getSnapshot();
  const devices = BoschAlarmMapper.toDevices(snapshot);
  const entities = BoschAlarmMapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('bosch_alarm');
  expect(entities.some((entityArg) => entityArg.id === 'binary_sensor.bosch_alarm_test_armed')).toBeTrue();

  const runtime = await new BoschAlarmIntegration().setup({ name: 'Bosch Alarm Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'alarm_control_panel', service: 'status', target: {} });
  expect((status.data as IBoschAlarmSnapshot).online).toBeTrue();
  expect((await runtime.callService!({ domain: 'alarm_control_panel', service: 'arm_away', target: {} })).success).toBeFalse();
  await runtime.destroy();

  const executorRuntime = await new BoschAlarmIntegration().setup({
    name: 'Bosch Alarm 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();
