import { expect, tap } from '@git.zone/tstest/tapbundle';
import { DevoloHomeNetworkMapper, type IDevoloConfig } from '../../ts/integrations/devolo_home_network/index.js';

const config: IDevoloConfig = {
  host: '192.168.1.20',
  name: 'Office Adapter',
  product: 'Magic 2 WiFi 6',
  serialNumber: 'S12345',
  macAddress: 'AA-AA-AA-AA-AA-AA',
  firmwareVersion: '5.10.0',
  features: ['led', 'restart', 'update', 'wifi1'],
  plcDevices: [
    {
      macAddress: 'AA:AA:AA:AA:AA:AA',
      userDeviceName: 'Office Adapter',
      topology: 'LOCAL',
      attachedToRouter: true,
    },
    {
      macAddress: 'BB:BB:BB:BB:BB:BB',
      userDeviceName: 'Garage Adapter',
      topology: 'REMOTE',
      attachedToRouter: true,
    },
  ],
  plcLinks: [
    {
      macAddressFrom: 'AA:AA:AA:AA:AA:AA',
      macAddressTo: 'BB:BB:BB:BB:BB:BB',
      rxRate: 410,
      txRate: 395,
    },
  ],
  wifiStations: [
    {
      macAddress: 'CC:CC:CC:CC:CC:CC',
      hostname: 'Kitchen Tablet',
      ipAddress: '192.168.1.44',
      band: 1,
      vapType: 0,
      rssi: -55,
    },
  ],
  neighboringWifiNetworks: [
    { ssid: 'Neighbor', bssid: 'DD:DD:DD:DD:DD:DD', band: 1, channel: 44, rssi: -70 },
  ],
  switches: {
    leds: true,
    guestWifi: { enabled: false, ssid: 'Guest', key: 'secret' },
  },
  firmware: {
    installedVersion: '5.10.0',
    latestVersion: '5.11.0',
  },
};

tap.test('maps devolo PLC/Wi-Fi snapshot to devices and HA-style entities', async () => {
  const snapshot = DevoloHomeNetworkMapper.toSnapshot(config);
  const devices = DevoloHomeNetworkMapper.toDevices(snapshot);
  const entities = DevoloHomeNetworkMapper.toEntities(snapshot);

  expect(snapshot.connected).toBeTrue();
  expect(snapshot.sensors.connected_plc_devices).toEqual(1);
  expect(snapshot.sensors.connected_wifi_clients).toEqual(1);
  expect(devices.some((deviceArg) => deviceArg.id === 'devolo_home_network.device.s12345')).toBeTrue();
  expect(devices.some((deviceArg) => deviceArg.id === 'devolo_home_network.plc.bb_bb_bb_bb_bb_bb')).toBeTrue();
  expect(devices.some((deviceArg) => deviceArg.id === 'devolo_home_network.station.cc_cc_cc_cc_cc_cc')).toBeTrue();
  expect(entities.find((entityArg) => entityArg.id === 'sensor.office_adapter_connected_wifi_clients')?.state).toEqual(1);
  expect(entities.find((entityArg) => entityArg.id === 'sensor.office_adapter_plc_downlink_phy_rate_garage_adapter')?.state).toEqual(410);
  expect(entities.find((entityArg) => entityArg.id === 'sensor.office_adapter_plc_uplink_phy_rate_garage_adapter')?.state).toEqual(395);
  expect(entities.find((entityArg) => entityArg.id === 'switch.office_adapter_enable_leds')?.state).toEqual('on');
  expect(entities.find((entityArg) => entityArg.id === 'switch.office_adapter_enable_guest_wi_fi')?.state).toEqual('off');
  expect(entities.find((entityArg) => entityArg.id === 'update.office_adapter_regular_firmware')?.attributes?.latestVersion).toEqual('5.11.0');
  expect(entities.find((entityArg) => entityArg.id === 'sensor.kitchen_tablet_wi_fi_band')?.state).toEqual('5');
});

tap.test('maps only represented devolo service actions to safe commands', async () => {
  const snapshot = DevoloHomeNetworkMapper.toSnapshot(config);
  const ledCommand = DevoloHomeNetworkMapper.commandForService(snapshot, {
    domain: 'switch',
    service: 'turn_off',
    target: { entityId: 'switch.office_adapter_enable_leds' },
  });
  const restartCommand = DevoloHomeNetworkMapper.commandForService(snapshot, {
    domain: 'button',
    service: 'press',
    target: { entityId: 'button.office_adapter_restart_device' },
  });
  const updateCommand = DevoloHomeNetworkMapper.commandForService(snapshot, {
    domain: 'update',
    service: 'install',
    target: { entityId: 'update.office_adapter_regular_firmware' },
  });
  const unsupported = DevoloHomeNetworkMapper.commandForService(DevoloHomeNetworkMapper.toSnapshot({ ...config, features: ['wifi1'], actions: [] }), {
    domain: 'button',
    service: 'press',
    target: { entityId: 'button.office_adapter_restart_device' },
  });

  expect(ledCommand?.type).toEqual('device.set_leds');
  expect(ledCommand?.enabled).toEqual(false);
  expect(restartCommand?.type).toEqual('device.restart');
  expect(updateCommand?.type).toEqual('firmware.install');
  expect(unsupported).toBeUndefined();
});

export default tap.start();
