import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HikvisioncamClient, HikvisioncamConfigFlow, HikvisioncamIntegration, HikvisioncamMapper, HomeAssistantHikvisioncamIntegration, createHikvisioncamDiscoveryDescriptor, hikvisioncamProfile, type IHikvisioncamSnapshot, type THikvisioncamRawData } from '../../ts/integrations/hikvisioncam/index.js';

const rawData: THikvisioncamRawData = {
  id: 'front-door-camera',
  name: 'Front Door Camera',
  model: 'DS-2CD2042WD-I',
  motionDetectionEnabled: true,
};

tap.test('matches manual Hikvision candidates and creates config flow output', async () => {
  const descriptor = createHikvisioncamDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'hikvisioncam-manual-match');
  const result = await matcher!.matches({ source: 'manual', host: 'hikvision.local', name: 'Front Door Camera', metadata: { rawData } }, {});

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

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

  const done = await (await new HikvisioncamConfigFlow().start(result.candidate!, {})).submit!({ username: 'admin', password: '12345' });
  expect(done.kind).toEqual('done');
  expect(done.config?.host).toEqual('hikvision.local');
  expect(done.config?.port).toEqual(80);
  expect(done.config?.rawData).toEqual(rawData);
});

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

  expect(snapshot.online).toBeTrue();
  expect(devices[0].integrationDomain).toEqual('hikvisioncam');
  expect(devices[0].manufacturer).toEqual('Hikvision');
  expect(entities[0].id).toEqual('switch.front_door_camera_motion_detection');
  expect(entities[0].state).toEqual(true);
});

tap.test('exposes Hikvision read-only runtime, HA alias, and unsupported control', async () => {
  const integration = new HikvisioncamIntegration();
  const alias = new HomeAssistantHikvisioncamIntegration();
  expect(alias instanceof HikvisioncamIntegration).toBeTrue();
  expect(alias.domain).toEqual('hikvisioncam');
  expect(integration.status).toEqual('read-only-runtime');
  expect(hikvisioncamProfile.metadata.configFlow).toEqual(false);
  expect(hikvisioncamProfile.metadata.qualityScale).toEqual('legacy');
  expect(hikvisioncamProfile.metadata.requirements).toEqual(['hikvision==0.4']);

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

  expect(status.success).toBeTrue();
  expect(refresh.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('Front Door Camera');

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

export default tap.start();
