import { expect, tap } from '@git.zone/tstest/tapbundle';
import { BsblanClient, BsblanConfigFlow, BsblanIntegration, BsblanMapper, HomeAssistantBsblanIntegration, bsblanProfile, createBsblanDiscoveryDescriptor, type IBsblanSnapshot } from '../../ts/integrations/bsblan/index.js';

const rawData = {
  boiler_temperature: 58,
  target_temperature: 21,
  burner: true,
};

tap.test('defines the BSB-LAN simple-local profile and alias', async () => {
  const integration = new HomeAssistantBsblanIntegration();

  expect(integration).toBeInstanceOf(BsblanIntegration);
  expect(integration.status).toEqual('control-runtime');
  expect(bsblanProfile.metadata.requirements).toEqual(['python-bsblan==5.2.0']);
  expect(bsblanProfile.metadata.sample).toEqual(rawData);
  expect(bsblanProfile.serviceDomains).toEqual(['climate', 'switch']);
  expect(bsblanProfile.controlServices).toEqual(['set_temperature', 'turn_on', 'turn_off', 'toggle']);
});

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

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

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

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

tap.test('maps BSB-LAN samples and gates live heating control', async () => {
  const client = new BsblanClient({ name: 'BSB-LAN Test', rawData });
  const snapshot = await client.getSnapshot();
  const devices = BsblanMapper.toDevices(snapshot);
  const entities = BsblanMapper.toEntities(snapshot);

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

  const runtime = await new BsblanIntegration().setup({ name: 'BSB-LAN Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'climate', service: 'status', target: {} });
  expect((status.data as IBsblanSnapshot).online).toBeTrue();
  expect((await runtime.callService!({ domain: 'switch', service: 'toggle', target: {} })).success).toBeFalse();
  await runtime.destroy();

  const executorRuntime = await new BsblanIntegration().setup({
    name: 'BSB-LAN Executor',
    rawData,
    commandExecutor: {
      execute: async (requestArg) => ({ success: true, data: { service: requestArg.service } }),
    },
  }, {});
  const executed = await executorRuntime.callService!({ domain: 'climate', service: 'set_temperature', target: {} });
  expect(executed.success).toBeTrue();
  expect((executed.data as { service: string }).service).toEqual('set_temperature');
  await executorRuntime.destroy();
});

export default tap.start();
