import { expect, tap } from '@git.zone/tstest/tapbundle';
import { DeakoClient, DeakoConfigFlow, DeakoIntegration, DeakoMapper, HomeAssistantDeakoIntegration, createDeakoDiscoveryDescriptor, deakoProfile, type IDeakoSnapshot } from '../../ts/integrations/deako/index.js';

const rawData = {
  device: {
    id: 'deako-kitchen',
    name: 'Kitchen Deako Dimmer',
    manufacturer: 'Deako',
    model: 'dimmer',
  },
  entities: [
    {
      id: 'kitchen_dimmer',
      name: 'Kitchen Dimmer',
      platform: 'light',
      state: true,
      attributes: {
        brightness: 64,
      },
    },
  ],
};

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

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

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

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

tap.test('maps Deako raw snapshots to runtime devices and entities', async () => {
  const client = new DeakoClient({ rawData });
  const snapshot = await client.getSnapshot();
  const devices = DeakoMapper.toDevices(snapshot);
  const entities = DeakoMapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('deako');
  expect(devices[0].manufacturer).toEqual('Deako');
  expect(entities[0].platform).toEqual('light');
});

tap.test('exposes Deako delegated control runtime without fake live control', async () => {
  expect(new HomeAssistantDeakoIntegration().domain).toEqual('deako');
  expect(deakoProfile.status).toEqual('control-runtime');
  expect(deakoProfile.metadata.configFlow).toBeTrue();
  expect(deakoProfile.metadata.requirements).toEqual(['pydeako==0.6.0']);
  expect(deakoProfile.metadata.dependencies).toEqual(['zeroconf']);
  expect(Object.prototype.hasOwnProperty.call(deakoProfile.metadata, 'qualityScale')).toBeTrue();
  expect(deakoProfile.metadata.qualityScale).toBeUndefined();

  const runtime = await new DeakoIntegration().setup({ rawData }, {});
  const status = await runtime.callService!({ domain: 'light', service: 'status', target: {} });
  const snapshot = status.data as IDeakoSnapshot;

  expect(status.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('Kitchen Deako Dimmer');

  const controlCommand = await runtime.callService!({ domain: 'light', service: 'turn_on', target: {} });
  expect(controlCommand.success).toBeFalse();
  expect(Boolean(controlCommand.error?.includes('requires an injected'))).toBeTrue();
  await runtime.destroy();
});

export default tap.start();
