import { expect, tap } from '@git.zone/tstest/tapbundle';
import { CoolmasterClient, CoolmasterConfigFlow, CoolmasterIntegration, CoolmasterMapper, HomeAssistantCoolmasterIntegration, coolmasterProfile, createCoolmasterDiscoveryDescriptor, type ICoolmasterSnapshot } from '../../ts/integrations/coolmaster/index.js';

const rawData = {
  zones: 6,
  temperature: 22.6,
  mode: 'cool',
};

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

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

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

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

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

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

tap.test('exposes CoolMasterNet profile and runtime services without fake live control', async () => {
  expect(new HomeAssistantCoolmasterIntegration().domain).toEqual('coolmaster');
  expect(coolmasterProfile.metadata.requirements).toEqual(['pycoolmasternet-async==0.2.4']);
  expect(coolmasterProfile.serviceDomains).toEqual(['climate', 'fan']);

  const runtime = await new CoolmasterIntegration().setup({ name: 'CoolMasterNet Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'climate', service: 'status', target: {} });
  const snapshot = status.data as ICoolmasterSnapshot;

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

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

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