import { expect, tap } from '@git.zone/tstest/tapbundle';
import { MikrotikMapper, type IMikrotikSnapshot } from '../../ts/integrations/mikrotik/index.js';

const snapshot: IMikrotikSnapshot = {
  connected: true,
  updatedAt: '2026-01-01T00:00:00.000Z',
  router: {
    host: '192.168.88.1',
    name: 'Core Router',
    model: 'hAP ax3',
    serialNumber: 'MK123456',
    firmware: '7.15.3',
    actions: ['reboot'],
  },
  resources: {
    cpuLoad: 12,
    freeMemory: 512 * 1048576,
    totalMemory: 1024 * 1048576,
    uptime: '1h2m3s',
    version: '7.15.3',
    boardName: 'hAP ax3',
  },
  devices: [
    {
      mac: '11:22:33:44:55:66',
      name: 'Kitchen Phone',
      ipAddress: '192.168.88.20',
      interface: 'bridge',
      ssid: 'Home WiFi',
      connected: true,
      signalStrength: -55,
      signalToNoise: 42,
      actions: ['arp_ping'],
    },
  ],
  interfaces: [
    {
      id: '*1',
      name: 'ether1',
      label: 'WAN',
      type: 'ether',
      running: true,
      disabled: false,
      rxBytes: 2_000_000_000,
      txBytes: 1_000_000_000,
      rxBitsPerSecond: 2_000_000,
      txBitsPerSecond: 1_000_000,
    },
  ],
  sensors: {},
};

tap.test('maps Mikrotik router resources, clients, interfaces, and traffic sensors', async () => {
  const normalized = MikrotikMapper.toSnapshot({ snapshot });
  const devices = MikrotikMapper.toDevices(normalized);
  const entities = MikrotikMapper.toEntities(normalized);

  expect(devices.some((deviceArg) => deviceArg.id === 'mikrotik.router.mk123456')).toBeTrue();
  expect(devices.some((deviceArg) => deviceArg.id === 'mikrotik.client.11_22_33_44_55_66')).toBeTrue();
  expect(entities.find((entityArg) => entityArg.id === 'sensor.core_router_cpu_load')?.state).toEqual(12);
  expect(entities.find((entityArg) => entityArg.id === 'sensor.core_router_memory_free')?.state).toEqual(512);
  expect(entities.find((entityArg) => entityArg.id === 'sensor.core_router_uptime')?.state).toEqual(3723);
  expect(entities.find((entityArg) => entityArg.id === 'sensor.core_router_wan_download')?.state).toEqual(2);
  expect(entities.find((entityArg) => entityArg.id === 'sensor.core_router_wan_download_speed')?.state).toEqual(2);
  expect(entities.find((entityArg) => entityArg.id === 'binary_sensor.kitchen_phone_connected')?.attributes?.nativePlatform).toEqual('device_tracker');
  expect(entities.find((entityArg) => entityArg.id === 'switch.core_router_wan')?.state).toEqual('on');
});

tap.test('models only represented Mikrotik RouterOS/API commands safely', async () => {
  const normalized = MikrotikMapper.toSnapshot({ snapshot });
  const rebootCommand = MikrotikMapper.commandForService(normalized, {
    domain: 'mikrotik',
    service: 'reboot',
    target: {},
  });
  const interfaceCommand = MikrotikMapper.commandForService(normalized, {
    domain: 'switch',
    service: 'turn_off',
    target: { entityId: 'switch.core_router_wan' },
  });
  const arpPingCommand = MikrotikMapper.commandForService(normalized, {
    domain: 'mikrotik',
    service: 'arp_ping',
    target: {},
    data: { mac: '11-22-33-44-55-66', count: 1 },
  });
  const unsupportedDisconnect = MikrotikMapper.commandForService(normalized, {
    domain: 'mikrotik',
    service: 'disconnect_client',
    target: {},
    data: { mac: '11:22:33:44:55:66' },
  });

  expect(rebootCommand?.path).toEqual('/system/reboot');
  expect(interfaceCommand?.path).toEqual('/interface/set');
  expect(interfaceCommand?.params.disabled).toEqual('yes');
  expect(arpPingCommand?.path).toEqual('/ping');
  expect(arpPingCommand?.params.address).toEqual('192.168.88.20');
  expect(unsupportedDisconnect).toBeUndefined();
});

export default tap.start();
