import { expect, tap } from '@git.zone/tstest/tapbundle';
import { AcerProjectorClient, AcerProjectorConfigFlow, AcerProjectorIntegration, AcerProjectorMapper, createAcerProjectorDiscoveryDescriptor, type IAcerProjectorSnapshot } from '../../ts/integrations/acer_projector/index.js';

const rawData = {
  "power": true,
  "input": "hdmi1",
  "lamp_hours": 1200
};

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

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

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

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

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

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

tap.test('exposes Acer Projector runtime services without fake live control', async () => {
  const runtime = await new AcerProjectorIntegration().setup({ name: 'Acer Projector Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'acer_projector', service: 'status', target: {} });
  const snapshot = status.data as IAcerProjectorSnapshot;

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

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

  const executorRuntime = await new AcerProjectorIntegration().setup({
    name: 'Acer Projector Executor',
    rawData,
    commandExecutor: {
      execute: async (requestArg) => ({ success: true, data: { service: requestArg.service } }),
    },
  }, {});
  const executed = await executorRuntime.callService!({ domain: 'acer_projector', 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();
