import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HomeAssistantHpIloIntegration, HpIloClient, HpIloConfigFlow, HpIloIntegration, HpIloMapper, createHpIloDiscoveryDescriptor, hpIloProfile, type IHpIloSnapshot, type THpIloRawData } from '../../ts/integrations/hp_ilo/index.js';

const rawData: THpIloRawData = {
  device: {
    id: 'hp-ilo-server-1',
    name: 'HP iLO Server',
    manufacturer: 'HP',
    model: 'Integrated Lights-Out',
    host: 'ilo.local',
    port: 443,
  },
  entities: [
    { id: 'server_power_status', name: 'Server Power Status', platform: 'sensor', state: 'ON' },
    { id: 'server_health', name: 'Server Health', platform: 'sensor', state: 'OK' },
    { id: 'server_uid_status', name: 'Server UID Status', platform: 'sensor', state: 'OFF' },
    { id: 'server_power_on_time', name: 'Server Power On Time', platform: 'sensor', state: 123456, unit: 's' },
  ],
};

tap.test('matches manual HP iLO candidates and creates config flow output', async () => {
  const descriptor = createHpIloDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'hp_ilo-manual-match');
  const result = await matcher!.matches({ source: 'manual', id: 'hp-ilo-server-1', name: 'HP iLO Server', host: 'ilo.local', port: 443, metadata: { rawData } }, {});

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

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

  const done = await (await new HpIloConfigFlow().start(result.candidate!, {})).submit!({ username: 'admin', password: 'secret' });
  expect(done.kind).toEqual('done');
  expect(done.config?.host).toEqual('ilo.local');
  expect(done.config?.port).toEqual(443);
  expect(done.config?.rawData).toEqual(rawData);
});

tap.test('maps HP iLO raw snapshots to runtime devices and entities', async () => {
  const client = new HpIloClient({ name: 'HP iLO Runtime', rawData });
  const snapshot = await client.getSnapshot();
  const mappedSnapshot = HpIloMapper.toSnapshotFromRaw({ name: 'HP iLO Runtime' }, rawData);
  const devices = HpIloMapper.toDevices(mappedSnapshot);
  const entities = HpIloMapper.toEntities(mappedSnapshot);

  expect(snapshot.online).toBeTrue();
  expect(mappedSnapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('hp_ilo');
  expect(devices[0].manufacturer).toEqual('HP');
  expect(entities.some((entityArg) => entityArg.id === 'sensor.hp_ilo_server_server_power_status')).toBeTrue();
  expect(entities.some((entityArg) => entityArg.id === 'sensor.hp_ilo_server_server_health')).toBeTrue();
});

tap.test('exposes HP iLO read-only runtime, HA alias, and unsupported control', async () => {
  const integration = new HpIloIntegration();
  const alias = new HomeAssistantHpIloIntegration();
  expect(alias instanceof HpIloIntegration).toBeTrue();
  expect(alias.domain).toEqual('hp_ilo');
  expect(integration.status).toEqual('read-only-runtime');
  expect(hpIloProfile.metadata.configFlow).toEqual(false);
  expect(hpIloProfile.metadata.qualityScale).toEqual('legacy');
  expect(hpIloProfile.metadata.requirements).toEqual(['python-hpilo==4.4.3']);

  const runtime = await integration.setup({ name: 'HP iLO Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'hp_ilo', service: 'status', target: {} });
  const refresh = await runtime.callService!({ domain: 'hp_ilo', service: 'refresh', target: {} });
  const snapshot = status.data as IHpIloSnapshot;

  expect(status.success).toBeTrue();
  expect(refresh.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('HP iLO Server');

  const command = await runtime.callService!({ domain: 'hp_ilo', service: 'turn_on', target: { deviceId: 'hp_ilo.device.hp_ilo_server' } });
  expect(command.success).toBeFalse();
  expect(command.error!).toContain('requires an injected client.execute() or commandExecutor');
  await runtime.destroy();
});

export default tap.start();
