import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HegelClient, HegelConfigFlow, HegelIntegration, HegelMapper, HomeAssistantHegelIntegration, createHegelDiscoveryDescriptor, hegelProfile, type IHegelSnapshot, type THegelRawData } from '../../ts/integrations/hegel/index.js';

const rawData: THegelRawData = {
  name: 'Living Room Hegel',
  model: 'H390',
  state: {
    power: true,
    volume: 35,
    mute: false,
    input: 10,
  },
};

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

  expect(result.matched).toBeTrue();
  expect(result.candidate?.integrationDomain).toEqual('hegel');
  expect(result.candidate?.port).toEqual(50001);

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

  const done = await (await new HegelConfigFlow().start(result.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.host).toEqual('hegel.local');
  expect(done.config?.port).toEqual(50001);
  expect(done.config?.rawData).toEqual(rawData);
});

tap.test('maps Hegel raw snapshots to runtime devices and entities', async () => {
  const client = new HegelClient({ host: 'hegel.local', model: 'H390', rawData });
  const snapshot = await client.getSnapshot();
  const mappedSnapshot = HegelMapper.toSnapshotFromRaw({ host: 'hegel.local', model: 'H390' }, rawData);
  const devices = HegelMapper.toDevices(mappedSnapshot);
  const entities = HegelMapper.toEntities(mappedSnapshot);

  expect(snapshot.online).toBeTrue();
  expect(devices[0].integrationDomain).toEqual('hegel');
  expect(devices[0].manufacturer).toEqual('Hegel');
  expect(entities[0].id).toEqual('media_player.living_room_hegel_media_player');
  expect(entities[0].state).toEqual('on');
  expect(entities[0].attributes?.source).toEqual('Network');
  expect(entities[0].attributes?.volumeLevel).toEqual(0.35);
});

tap.test('exposes Hegel read-only runtime, HA alias, and unsupported control', async () => {
  const integration = new HegelIntegration();
  const alias = new HomeAssistantHegelIntegration();
  expect(alias instanceof HegelIntegration).toBeTrue();
  expect(alias.domain).toEqual('hegel');
  expect(integration.status).toEqual('read-only-runtime');
  expect(hegelProfile.metadata.configFlow).toEqual(true);
  expect(hegelProfile.metadata.qualityScale).toEqual('silver');
  expect(hegelProfile.metadata.requirements).toEqual(['hegel-ip-client==0.1.4']);

  const runtime = await integration.setup({ host: 'hegel.local', model: 'H390', rawData }, {});
  const status = await runtime.callService!({ domain: 'hegel', service: 'status', target: {} });
  const refresh = await runtime.callService!({ domain: 'hegel', service: 'refresh', target: {} });
  const snapshot = status.data as IHegelSnapshot;

  expect(status.success).toBeTrue();
  expect(refresh.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.entities())[0].platform).toEqual('media_player');

  const command = await runtime.callService!({ domain: 'media_player', service: 'turn_off', target: { entityId: 'media_player.living_room_hegel_media_player' } });
  expect(command.success).toBeFalse();
  expect(command.error!).toContain('requires an injected client.execute() or commandExecutor');
  await runtime.destroy();
});

export default tap.start();
