import { expect, tap } from '@git.zone/tstest/tapbundle';
import { BalboaClient, BalboaConfigFlow, BalboaIntegration, BalboaMapper, HomeAssistantBalboaIntegration, createBalboaDiscoveryDescriptor, type IBalboaSnapshot } from '../../ts/integrations/balboa/index.js';

const rawData = {
  "water_temperature": 37.5,
  "target_temperature": 38,
  "pump": true
};

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

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

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

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

tap.test('maps Balboa Spa Client raw snapshots to runtime devices and entities', async () => {
  const client = new BalboaClient({ name: 'Balboa Spa Client Test', rawData });
  const snapshot = await client.getSnapshot();
  const devices = BalboaMapper.toDevices(snapshot);
  const entities = BalboaMapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('balboa');
  expect(entities.length > 0).toBeTrue();
});

tap.test('exposes Balboa Spa Client runtime services without fake live control', async () => {
  const integration = new BalboaIntegration();
  expect(new HomeAssistantBalboaIntegration().domain).toEqual('balboa');
  expect(integration.status).toEqual('control-runtime');

  const runtime = await integration.setup({ name: 'Balboa Spa Client Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'balboa', service: 'status', target: {} });
  const snapshot = status.data as IBalboaSnapshot;

  expect(status.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('Balboa Spa Client Runtime');

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

  const executorRuntime = await new BalboaIntegration().setup({
    name: 'Balboa Spa Client Executor',
    rawData,
    commandExecutor: {
      execute: async (requestArg) => ({ success: true, data: { service: requestArg.service } }),
    },
  }, {});
  const executed = await executorRuntime.callService!({ domain: 'switch', service: 'turn_on', target: {} });
  expect(executed.success).toBeTrue();
  expect((executed.data as { service: string }).service).toEqual('turn_on');
  await executorRuntime.destroy();
});

export default tap.start();
