import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HdfuryClient, HdfuryConfigFlow, HdfuryIntegration, HdfuryMapper, HomeAssistantHdfuryIntegration, createHdfuryDiscoveryDescriptor, hdfuryProfile, type IHdfurySnapshot, type THdfuryRawData } from '../../ts/integrations/hdfury/index.js';

const rawData: THdfuryRawData = {
  board: {
    hostname: 'vrroom-lab',
    serial: 'HDF123456',
    version: 'FW: 1.23',
    pcbv: 'A1',
  },
  info: {
    TX0: '4K60',
    AUDOUT: 'eARC',
    portseltx0: '1',
    opmode: '2',
  },
  config: {
    macaddr: '00:11:22:33:44:55',
    cec: '1',
    oled: '0',
    oledfade: '15',
  },
};

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

  expect(result.matched).toBeTrue();
  expect(result.candidate?.integrationDomain).toEqual('hdfury');
  expect(result.candidate?.port).toEqual(80);

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

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

tap.test('maps HDFury raw snapshots to runtime devices and entities', async () => {
  const client = new HdfuryClient({ host: 'vrroom.local', rawData });
  const snapshot = await client.getSnapshot();
  const mappedSnapshot = HdfuryMapper.toSnapshotFromRaw({ host: 'vrroom.local' }, rawData);
  const devices = HdfuryMapper.toDevices(mappedSnapshot);
  const entities = HdfuryMapper.toEntities(mappedSnapshot);

  expect(snapshot.online).toBeTrue();
  expect(mappedSnapshot.device.serialNumber).toEqual('HDF123456');
  expect(devices[0].integrationDomain).toEqual('hdfury');
  expect(devices[0].manufacturer).toEqual('HDFury');
  expect(entities.some((entityArg) => entityArg.id === 'sensor.vrroom_lab_tx0')).toBeTrue();
  expect(entities.some((entityArg) => entityArg.id === 'switch.vrroom_lab_cec')).toBeTrue();
  expect(entities.some((entityArg) => entityArg.id === 'number.vrroom_lab_oledfade')).toBeTrue();
  expect(entities.some((entityArg) => entityArg.id === 'select.vrroom_lab_opmode')).toBeTrue();
});

tap.test('exposes HDFury read-only runtime, HA alias, and unsupported control', async () => {
  const integration = new HdfuryIntegration();
  const alias = new HomeAssistantHdfuryIntegration();
  expect(alias instanceof HdfuryIntegration).toBeTrue();
  expect(alias.domain).toEqual('hdfury');
  expect(integration.status).toEqual('read-only-runtime');
  expect(hdfuryProfile.metadata.configFlow).toEqual(true);
  expect(hdfuryProfile.metadata.qualityScale).toEqual('platinum');
  expect(hdfuryProfile.metadata.requirements).toEqual(['hdfury==1.6.0']);

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

  expect(status.success).toBeTrue();
  expect(refresh.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('vrroom-lab');

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

export default tap.start();
