import { expect, tap } from '@git.zone/tstest/tapbundle';
import { CasperGlowClient, CasperGlowConfigFlow, CasperGlowIntegration, CasperGlowMapper, HomeAssistantCasperGlowIntegration, casperGlowProfile, createCasperGlowDiscoveryDescriptor, type ICasperGlowSnapshot } from '../../ts/integrations/casper_glow/index.js';

const rawData = {
  on: true,
  brightness: 128,
  battery: 74,
};

tap.test('matches manual Casper Glow candidates and exposes native profile metadata', async () => {
  const integration = new CasperGlowIntegration();
  const alias = new HomeAssistantCasperGlowIntegration();
  const descriptor = createCasperGlowDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'casper_glow-manual-match');
  const result = await matcher!.matches({ host: 'casper-glow.local', name: 'Casper Glow', metadata: { rawData } }, {});

  expect(alias.domain).toEqual('casper_glow');
  expect(integration.displayName).toEqual('Casper Glow');
  expect(casperGlowProfile.manufacturer).toEqual('Casper');
  expect(casperGlowProfile.model).toEqual('Glow light');
  expect(casperGlowProfile.metadata.requirements).toEqual(['pycasperglow==1.2.0']);
  expect(casperGlowProfile.metadata.dependencies).toEqual(['bluetooth_adapters']);
  expect(casperGlowProfile.metadata.qualityScale).toEqual('platinum');
  expect(casperGlowProfile.serviceDomains).toEqual(['light']);
  expect(casperGlowProfile.controlServices).toEqual(['turn_on', 'turn_off', 'toggle', 'set_level']);
  expect(result.matched).toBeTrue();
  expect(result.candidate?.integrationDomain).toEqual('casper_glow');

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

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

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

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(snapshot.rawData).toEqual(rawData);
  expect(devices[0].integrationDomain).toEqual('casper_glow');
  expect(devices[0].manufacturer).toEqual('Casper');
  expect(devices[0].model).toEqual('Glow light');
  expect(entities.some((entityArg) => entityArg.name === 'Battery' && entityArg.state === 74)).toBeTrue();
});

tap.test('exposes Casper Glow light services without fake live control', async () => {
  const runtime = await new CasperGlowIntegration().setup({ name: 'Casper Glow Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'light', service: 'status', target: {} });
  const snapshot = status.data as ICasperGlowSnapshot;

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

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

  const executorRuntime = await new CasperGlowIntegration().setup({
    name: 'Casper Glow Executor',
    rawData,
    commandExecutor: {
      execute: async (requestArg) => ({ success: true, data: { service: requestArg.service } }),
    },
  }, {});
  const executed = await executorRuntime.callService!({ domain: 'light', service: 'set_level', target: {}, data: { brightness: 96 } });
  expect(executed.success).toBeTrue();
  expect((executed.data as { service: string }).service).toEqual('set_level');
  await executorRuntime.destroy();
});

export default tap.start();
