import { expect, tap } from '@git.zone/tstest/tapbundle';
import { EheimdigitalClient, EheimdigitalConfigFlow, EheimdigitalIntegration, EheimdigitalMapper, HomeAssistantEheimdigitalIntegration, createEheimdigitalDiscoveryDescriptor, eheimdigitalProfile, type IEheimdigitalSnapshot } from '../../ts/integrations/eheimdigital/index.js';

const rawData = {
  device: {
    id: 'eheim-hub-aa-bb-cc',
    name: 'EHEIM Aquarium',
    manufacturer: 'EHEIM',
    model: 'EHEIM Digital Hub',
    serialNumber: 'AA:BB:CC:DD:EE:FF',
    host: 'eheimdigital.local',
  },
  entities: [
    { id: 'filter_active', name: 'Filter Active', platform: 'switch', state: true, writable: true },
    { id: 'current_speed', name: 'Current Speed', platform: 'sensor', state: 42, unit: 'Hz', deviceClass: 'frequency' },
    { id: 'manual_speed', name: 'Manual Speed', platform: 'select', state: '50', writable: true },
    { id: 'system_led', name: 'System LED', platform: 'number', state: 75, unit: '%', writable: true },
    { id: 'heater', name: 'Heater', platform: 'climate', state: { currentTemperature: 24.7, targetTemperature: 25, hvacMode: 'auto' }, writable: true },
  ],
};

tap.test('matches manual EHEIM Digital candidates and creates config flow output', async () => {
  const descriptor = createEheimdigitalDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'eheimdigital-manual-match');
  const result = await matcher!.matches({ host: 'eheimdigital.local', name: 'EHEIM Aquarium', metadata: { rawData } }, {});

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

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

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

tap.test('maps EHEIM Digital raw snapshots to devices and mixed entities', async () => {
  const client = new EheimdigitalClient({ name: 'EHEIM Runtime', rawData });
  const snapshot = await client.getSnapshot();
  const devices = EheimdigitalMapper.toDevices(snapshot);
  const entities = EheimdigitalMapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('eheimdigital');
  expect(devices[0].manufacturer).toEqual('EHEIM');
  expect(entities.length).toEqual(5);
  expect(entities.some((entityArg) => entityArg.platform === 'switch')).toBeTrue();
  expect(entities.some((entityArg) => entityArg.platform === 'climate')).toBeTrue();
});

tap.test('exposes EHEIM Digital runtime services, HA alias, and unsupported control without executor', async () => {
  expect(new HomeAssistantEheimdigitalIntegration().domain).toEqual('eheimdigital');
  expect(new HomeAssistantEheimdigitalIntegration().status).toEqual('control-runtime');
  expect(eheimdigitalProfile.metadata.qualityScale).toEqual('platinum');
  expect(eheimdigitalProfile.metadata.requirements).toEqual(['eheimdigital==1.6.0']);
  expect(eheimdigitalProfile.metadata.configFlow).toBeTrue();

  const runtime = await new EheimdigitalIntegration().setup({ name: 'EHEIM Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'eheimdigital', service: 'status', target: {} });
  const snapshot = status.data as IEheimdigitalSnapshot;
  const snapshotService = await runtime.callService!({ domain: 'eheimdigital', service: 'snapshot', target: {} });

  expect(status.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect(snapshotService.success).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('EHEIM Aquarium');

  const controlCommand = await runtime.callService!({ domain: 'switch', service: 'turn_on', target: {} });
  expect(controlCommand.success).toBeFalse();
  await runtime.destroy();
});

export default tap.start();
