import { expect, tap } from '@git.zone/tstest/tapbundle';
import { BryantEvolutionClient, BryantEvolutionConfigFlow, BryantEvolutionIntegration, BryantEvolutionMapper, HomeAssistantBryantEvolutionIntegration, bryantEvolutionProfile, createBryantEvolutionDiscoveryDescriptor, type IBryantEvolutionSnapshot } from '../../ts/integrations/bryant_evolution/index.js';

const rawData = {
  temperature: 21.3,
  target_temperature: 22,
  mode: 'heat',
};

tap.test('defines the Bryant Evolution simple-local profile and alias', async () => {
  const integration = new HomeAssistantBryantEvolutionIntegration();

  expect(integration).toBeInstanceOf(BryantEvolutionIntegration);
  expect(integration.status).toEqual('control-runtime');
  expect(bryantEvolutionProfile.metadata.requirements).toEqual(['evolutionhttp==0.0.18']);
  expect(bryantEvolutionProfile.metadata.sample).toEqual(rawData);
  expect(bryantEvolutionProfile.serviceDomains).toEqual(['climate']);
  expect(bryantEvolutionProfile.controlServices).toEqual(['set_temperature', 'set_hvac_mode', 'turn_on', 'turn_off']);
});

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

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

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

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

tap.test('maps Bryant Evolution samples and gates live climate control', async () => {
  const client = new BryantEvolutionClient({ name: 'Bryant Evolution Test', rawData });
  const snapshot = await client.getSnapshot();
  const devices = BryantEvolutionMapper.toDevices(snapshot);
  const entities = BryantEvolutionMapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('bryant_evolution');
  expect(entities.some((entityArg) => entityArg.id === 'sensor.bryant_evolution_test_temperature')).toBeTrue();

  const runtime = await new BryantEvolutionIntegration().setup({ name: 'Bryant Evolution Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'climate', service: 'status', target: {} });
  expect((status.data as IBryantEvolutionSnapshot).online).toBeTrue();
  expect((await runtime.callService!({ domain: 'climate', service: 'set_temperature', target: {} })).success).toBeFalse();
  await runtime.destroy();

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

export default tap.start();
