import { expect, tap } from '@git.zone/tstest/tapbundle';
import { GoogleWifiClient, GoogleWifiConfigFlow, GoogleWifiIntegration, GoogleWifiMapper, HomeAssistantGoogleWifiIntegration, createGoogleWifiDiscoveryDescriptor, googleWifiProfile, type IGoogleWifiSnapshot, type TGoogleWifiRawData } from '../../ts/integrations/google_wifi/index.js';

const rawData: TGoogleWifiRawData = {
  software: {
    softwareVersion: '14150.43.80',
    updateNewVersion: '0.0.0.0',
  },
  system: {
    uptime: 86400,
  },
  wan: {
    localIpAddress: '100.64.1.2',
    online: true,
  },
};

tap.test('matches manual Google Wifi candidates and creates config flow output', async () => {
  const descriptor = createGoogleWifiDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'google_wifi-manual-match');
  const result = await matcher!.matches({ source: 'manual', id: 'google-wifi', name: 'Google Wifi', metadata: { rawData } }, {});

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

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

  const done = await (await new GoogleWifiConfigFlow().start(result.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.name).toEqual('Google Wifi');
  expect(done.config?.rawData).toEqual(rawData);
});

tap.test('maps Google Wifi raw snapshots to runtime devices and entities', async () => {
  const client = new GoogleWifiClient({ name: 'Google Wifi', rawData });
  const snapshot = await client.getSnapshot();
  const mappedSnapshot = GoogleWifiMapper.toSnapshotFromRaw({ name: 'Google Wifi' }, rawData);
  const devices = GoogleWifiMapper.toDevices(mappedSnapshot);
  const entities = GoogleWifiMapper.toEntities(mappedSnapshot);

  expect(snapshot.online).toBeTrue();
  expect(devices[0].integrationDomain).toEqual('google_wifi');
  expect(entities.some((entityArg) => entityArg.id === 'sensor.google_wifi_current_version')).toBeTrue();
  expect(entities.some((entityArg) => entityArg.id === 'sensor.google_wifi_new_version' && entityArg.state === 'Latest')).toBeTrue();
  expect(entities.some((entityArg) => entityArg.id === 'sensor.google_wifi_uptime' && entityArg.state === 1)).toBeTrue();
  expect(entities.some((entityArg) => entityArg.id === 'sensor.google_wifi_status' && entityArg.state === 'Online')).toBeTrue();
});

tap.test('exposes Google Wifi read-only runtime, HA alias, and unsupported control', async () => {
  const integration = new GoogleWifiIntegration();
  const alias = new HomeAssistantGoogleWifiIntegration();
  expect(alias instanceof GoogleWifiIntegration).toBeTrue();
  expect(alias.domain).toEqual('google_wifi');
  expect(integration.status).toEqual('read-only-runtime');
  expect(googleWifiProfile.metadata.configFlow).toEqual(false);
  expect(googleWifiProfile.metadata.qualityScale).toEqual('legacy');
  expect(googleWifiProfile.metadata.requirements).toEqual([]);

  const runtime = await integration.setup({ name: 'Google Wifi', rawData }, {});
  const status = await runtime.callService!({ domain: 'google_wifi', service: 'status', target: {} });
  const refresh = await runtime.callService!({ domain: 'google_wifi', service: 'refresh', target: {} });
  const snapshot = status.data as IGoogleWifiSnapshot;

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

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

export default tap.start();
