import { expect, tap } from '@git.zone/tstest/tapbundle';
import { ComelitClient, ComelitConfigFlow, ComelitIntegration, ComelitMapper, HomeAssistantComelitIntegration, comelitProfile, createComelitDiscoveryDescriptor, type IComelitSnapshot } from '../../ts/integrations/comelit/index.js';

const rawData = {
  "lights": 4,
  "covers": 2,
  "alarm": false
};

tap.test('exposes Comelit SimpleHome profile and alias class', async () => {
  const integration = new HomeAssistantComelitIntegration();

  expect(integration.domain).toEqual('comelit');
  expect(integration.status).toEqual('control-runtime');
  expect(comelitProfile.displayName).toEqual('Comelit SimpleHome');
  expect(comelitProfile.metadata.qualityScale).toEqual('platinum');
  expect((comelitProfile.metadata.requirements as string[])[0]).toEqual('aiocomelit==2.0.3');
});

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

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

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

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

tap.test('maps Comelit SimpleHome snapshots and gates live control', async () => {
  const client = new ComelitClient({ name: 'Comelit Runtime', rawData });
  const snapshot = await client.getSnapshot();
  const devices = ComelitMapper.toDevices(snapshot);
  const entities = ComelitMapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('comelit');
  expect(entities.length).toEqual(3);

  const runtime = await new ComelitIntegration().setup({ name: 'Comelit Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'comelit', service: 'status', target: {} });
  const statusSnapshot = status.data as IComelitSnapshot;

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

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

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

export default tap.start();
