import { expect, tap } from '@git.zone/tstest/tapbundle';
import { EufyClient, EufyConfigFlow, EufyIntegration, EufyMapper, HomeAssistantEufyIntegration, createEufyDiscoveryDescriptor, eufyProfile, type IEufySnapshot } from '../../ts/integrations/eufy/index.js';

const rawData = {
  device: {
    id: 'eufy-home-1',
    name: 'Eufy Home Devices',
    manufacturer: 'Eufy',
    model: 'EufyHome',
    host: '192.0.2.24',
  },
  entities: [
    { id: 'color_bulb', name: 'Color Bulb', platform: 'light', state: true, writable: true, attributes: { type: 'T1013', brightness: 182, colorTempKelvin: 3200, hsColor: [32, 72] } },
    { id: 'smart_plug', name: 'Smart Plug', platform: 'switch', state: false, writable: true, attributes: { type: 'T1201' } },
  ],
};

tap.test('matches manual EufyHome candidates and creates config flow output', async () => {
  const descriptor = createEufyDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'eufy-manual-match');
  const result = await matcher!.matches({ host: '192.0.2.24', name: 'EufyHome Color Bulb', metadata: { rawData, type: 'T1013', accessToken: 'token' } }, {});

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

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

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

tap.test('maps EufyHome raw snapshots to runtime devices and entities', async () => {
  const client = new EufyClient({ name: 'Eufy Runtime', rawData });
  const snapshot = await client.getSnapshot();
  const devices = EufyMapper.toDevices(snapshot);
  const entities = EufyMapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('eufy');
  expect(devices[0].manufacturer).toEqual('Eufy');
  expect(entities.some((entityArg) => entityArg.platform === 'light')).toBeTrue();
  expect(entities.some((entityArg) => entityArg.platform === 'switch')).toBeTrue();
});

tap.test('exposes EufyHome runtime services, HA alias, and unsupported control without executor', async () => {
  const alias = new HomeAssistantEufyIntegration();
  expect(alias instanceof EufyIntegration).toBeTrue();
  expect(alias.domain).toEqual('eufy');
  expect(eufyProfile.metadata.qualityScale).toEqual('legacy');
  expect(eufyProfile.metadata.requirements).toEqual(['lakeside==0.13']);
  expect(eufyProfile.metadata.configFlow).toEqual(false);

  const runtime = await new EufyIntegration().setup({ name: 'Eufy Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'eufy', service: 'status', target: {} });
  const snapshot = status.data as IEufySnapshot;

  expect(status.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.callService!({ domain: 'eufy', service: 'refresh', target: {} })).success).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('Eufy Home Devices');

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

export default tap.start();
