import { expect, tap } from '@git.zone/tstest/tapbundle';
import { AirosMapper, type IAirosRawStatus } from '../../ts/integrations/airos/index.js';

const rawStatus: IAirosRawStatus = {
  host: {
    hostname: 'Roof Bridge',
    uptime: 12345,
    fwversion: 'v8.7.19',
    devmodel: 'LiteBeam 5AC',
    netrole: 'bridge',
    cpuload: 4.2,
    totalram: 128000,
    freeram: 64000,
  },
  services: {
    dhcpc: true,
    dhcpd: false,
    dhcp6d_stateful: false,
    pppoe: false,
  },
  portfw: false,
  wireless: {
    mode: 'ap-ptp',
    essid: 'Backhaul',
    frequency: 5805,
    distance: 1200,
    antenna_gain: 23,
    polling: { dl_capacity: 210000, ul_capacity: 190000 },
    throughput: { tx: 12000, rx: 9000 },
  },
  interfaces: [
    { ifname: 'ath0', hwaddr: '11:22:33:44:55:66', enabled: true },
    { ifname: 'br0', hwaddr: 'AA:BB:CC:DD:EE:FF', enabled: true },
  ],
};

tap.test('maps airOS status snapshots to Home Assistant entities', async () => {
  const snapshot = AirosMapper.toSnapshot({
    host: '192.168.1.20',
    name: 'Roof Bridge',
    rawStatus,
    firmwareStatus: { update: true, version: 'v8.7.20', changelog: 'https://example.invalid/changelog.txt' },
  });
  const entities = AirosMapper.toEntities(snapshot);

  expect(snapshot.connected).toBeTrue();
  expect(snapshot.device.macAddress).toEqual('aa:bb:cc:dd:ee:ff');
  expect(snapshot.device.firmwareMajor).toEqual(8);
  expect(snapshot.wireless.role).toEqual('access_point');
  expect(snapshot.wireless.mode).toEqual('point_to_point');
  expect(entities.find((entityArg) => entityArg.id === 'sensor.roof_bridge_wireless_frequency')?.state).toEqual(5805);
  expect(entities.find((entityArg) => entityArg.id === 'binary_sensor.roof_bridge_dhcp_client')?.state).toBeTrue();
  expect(entities.find((entityArg) => entityArg.id === 'button.roof_bridge_reboot')?.available).toBeTrue();
  expect(entities.find((entityArg) => entityArg.id === 'update.roof_bridge_firmware_update')?.state).toEqual('on');
});

tap.test('maps airOS snapshots to device features and commands', async () => {
  const snapshot = AirosMapper.toSnapshot({ host: '192.168.1.20', name: 'Roof Bridge', rawStatus, firmwareStatus: { update: true, version: 'v8.7.20' } });
  const devices = AirosMapper.toDevices(snapshot);
  const device = devices[0];

  expect(device.id).toEqual('airos.device.aa_bb_cc_dd_ee_ff');
  expect(device.protocol).toEqual('http');
  expect(device.online).toBeTrue();
  expect(device.features.some((featureArg) => featureArg.id === 'firmware_update' && featureArg.writable)).toBeTrue();
  expect(device.state.find((stateArg) => stateArg.featureId === 'download_capacity')?.value).toEqual(210000);

  const reboot = AirosMapper.commandForService(snapshot, { domain: 'button', service: 'press', target: { entityId: 'button.roof_bridge_reboot' }, data: {} });
  expect(reboot?.action).toEqual('reboot');
  expect(reboot?.method).toEqual('reboot.cgi');

  const install = AirosMapper.commandForService(snapshot, { domain: 'update', service: 'install', target: { entityId: 'update.roof_bridge_firmware_update' }, data: {} });
  expect(install?.action).toEqual('install_update');
  expect(install?.method).toEqual('fwflash.cgi');
});

export default tap.start();
