import { expect, tap } from '@git.zone/tstest/tapbundle';
import { BlinksticklightClient, BlinksticklightConfigFlow, BlinksticklightIntegration, BlinksticklightMapper, HomeAssistantBlinksticklightIntegration, blinksticklightProfile, blinksticklightSampleData, createBlinksticklightDiscoveryDescriptor, type IBlinksticklightSnapshot } from '../../ts/integrations/blinksticklight/index.js';

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

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

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

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

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

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

tap.test('exposes BlinkStick control runtime and Home Assistant alias', async () => {
  const integration = new BlinksticklightIntegration();
  const alias = new HomeAssistantBlinksticklightIntegration();
  expect(integration.status).toEqual('control-runtime');
  expect(alias.domain).toEqual('blinksticklight');
  expect(blinksticklightProfile.serviceDomains).toEqual(['light']);
  expect(blinksticklightProfile.metadata.sampleData).toEqual(blinksticklightSampleData);

  const runtime = await integration.setup({ name: 'BlinkStick Runtime', rawData: blinksticklightSampleData }, {});
  const status = await runtime.callService!({ domain: 'light', service: 'status', target: {} });
  const snapshot = status.data as IBlinksticklightSnapshot;

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

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

  const executorRuntime = await integration.setup({
    name: 'BlinkStick Executor',
    rawData: blinksticklightSampleData,
    commandExecutor: {
      execute: async (requestArg) => ({ success: true, data: { domain: requestArg.domain, service: requestArg.service } }),
    },
  }, {});
  const executed = await executorRuntime.callService!({ domain: 'light', service: 'turn_on', target: {} });
  expect(executed.success).toBeTrue();
  expect((executed.data as { domain: string; service: string }).domain).toEqual('light');
  expect((executed.data as { domain: string; service: string }).service).toEqual('turn_on');
  await executorRuntime.destroy();
});

export default tap.start();
