import { expect, tap } from '@git.zone/tstest/tapbundle';
import { Control4Client, Control4ConfigFlow, Control4Integration, Control4Mapper, HomeAssistantControl4Integration, control4Profile, createControl4DiscoveryDescriptor, type IControl4Snapshot } from '../../ts/integrations/control4/index.js';

const rawData = {
  lights: 8,
  media_zones: 2,
  online: true,
};

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

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

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

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

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

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

tap.test('exposes Control4 profile and runtime services without fake live control', async () => {
  expect(new HomeAssistantControl4Integration().domain).toEqual('control4');
  expect(control4Profile.metadata.requirements).toEqual(['pyControl4==1.5.0']);
  expect(control4Profile.serviceDomains).toEqual(['light', 'switch', 'media_player']);

  const runtime = await new Control4Integration().setup({ name: 'Control4 Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'media_player', service: 'status', target: {} });
  const snapshot = status.data as IControl4Snapshot;

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

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

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

export default tap.start();
