import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HuaweiLteMapper, type IHuaweiLteRawData } from '../../ts/integrations/huawei_lte/index.js';

const rawData: Partial<IHuaweiLteRawData> = {
  deviceInformation: {
    DeviceName: 'B535-232',
    SerialNumber: 'HW123456',
    HardwareVersion: 'WL1B535M',
    SoftwareVersion: '12.0.5.1',
    MacAddress1: 'AA:BB:CC:DD:EE:FF',
  },
  deviceSignal: {
    rsrp: '-91dBm',
    rsrq: '-12dB',
    sinr: '18dB',
    nrrsrp: '-83dBm',
  },
  dialupMobileDataswitch: {
    dataswitch: '1',
  },
  monitoringStatus: {
    ConnectionStatus: '901',
    WifiStatus: '1',
    CurrentWifiUser: '3',
    BatteryPercent: '78',
  },
  monitoringTrafficStatistics: {
    CurrentDownload: '1024',
    CurrentUpload: '2048',
    CurrentDownloadRate: '128',
    CurrentUploadRate: '64',
    TotalDownload: '4096',
    TotalUpload: '8192',
  },
  monitoringMonthStatistics: {
    CurrentMonthDownload: '1000',
    CurrentMonthUpload: '2000',
  },
  monitoringCheckNotifications: {
    UnreadMessage: '2',
    SmsStorageFull: '0',
  },
  netCurrentPlmn: {
    FullName: 'Example Mobile',
    Numeric: '00101',
    State: '0',
  },
  netNetMode: {
    NetworkMode: '03',
  },
  smsSmsCount: {
    LocalUnread: '2',
    LocalInbox: '10',
    SimMax: '50',
  },
  lanHostInfo: {
    Hosts: {
      Host: [
        { MacAddress: '11:22:33:44:55:66', HostName: 'phone', IpAddress: '192.168.8.10', Active: '1', InterfaceType: 'Wireless' },
        { MacAddress: '22:33:44:55:66:77', HostName: 'nas', IpAddress: '192.168.8.20', Active: '0', InterfaceType: 'Ethernet' },
      ],
    },
  },
  wlanWifiFeatureSwitch: {
    wifi24g_switch_enable: '1',
    wifi5g_enabled: '0',
  },
  wlanWifiGuestNetworkSwitch: {
    WifiEnable: '1',
    WifiSsid: 'Guest LTE',
  },
};

tap.test('maps Huawei LTE raw API data to snapshot, devices, and entities', async () => {
  const snapshot = HuaweiLteMapper.toSnapshot({ config: { url: 'http://192.168.8.1/' }, rawData, online: true, source: 'manual' });
  const devices = HuaweiLteMapper.toDevices(snapshot);
  const entities = HuaweiLteMapper.toEntities(snapshot);

  expect(snapshot.device.serialNumber).toEqual('HW123456');
  expect(snapshot.device.macAddresses[0]).toEqual('aa:bb:cc:dd:ee:ff');
  expect(snapshot.connection.mobileConnected).toBeTrue();
  expect(snapshot.connection.mobileDataEnabled).toBeTrue();
  expect(snapshot.connection.wifi5ghzEnabled).toBeFalse();
  expect(snapshot.signal.rsrp).toEqual(-91);
  expect(snapshot.signal.nrrsrp).toEqual(-83);
  expect(snapshot.traffic.currentDownloadRate).toEqual(128);
  expect(snapshot.sms.unread).toEqual(2);
  expect(snapshot.network.operatorName).toEqual('Example Mobile');
  expect(snapshot.hosts.length).toEqual(2);
  expect(snapshot.hosts[0].active).toBeTrue();
  expect(snapshot.capabilities.mobileDataSwitch).toBeTrue();
  expect(snapshot.capabilities.sendSms).toBeTrue();
  expect(devices[0].id).toEqual('huawei_lte.router.hw123456');
  expect(devices[0].metadata?.softwareVersion).toEqual('12.0.5.1');
  expect(entities.find((entityArg) => entityArg.attributes?.key === 'mobile_data')?.state).toEqual('on');
  expect(entities.find((entityArg) => entityArg.attributes?.key === 'wifi_guest_network')?.state).toEqual('on');
  expect(entities.find((entityArg) => entityArg.attributes?.key === 'preferred_network_mode')?.state).toEqual('03');
  expect(entities.find((entityArg) => entityArg.attributes?.item === 'rsrp')?.state).toEqual(-91);
});

tap.test('maps Home Assistant style services to real Huawei LTE command shapes', async () => {
  const snapshot = HuaweiLteMapper.toSnapshot({ config: { url: 'http://192.168.8.1/' }, rawData, online: true, source: 'manual' });
  const mobileDataOff = HuaweiLteMapper.commandForService(snapshot, { domain: 'switch', service: 'turn_off', target: { entityId: 'switch.b535_mobile_data' }, data: {} });
  const guestOn = HuaweiLteMapper.commandForService(snapshot, { domain: 'switch', service: 'turn_on', target: { entityId: 'switch.b535_wifi_guest_network' }, data: {} });
  const sms = HuaweiLteMapper.commandForService(snapshot, { domain: 'huawei_lte', service: 'send_sms', target: {}, data: { phone_numbers: ['+123'], message: 'hello' } });
  const networkMode = HuaweiLteMapper.commandForService(snapshot, { domain: 'select', service: 'select_option', target: { entityId: 'select.b535_preferred_network_mode' }, data: { option: '00' } });

  expect(mobileDataOff?.action).toEqual('set_mobile_dataswitch');
  expect(mobileDataOff?.enabled).toBeFalse();
  expect(guestOn?.action).toEqual('set_wifi_guest_network');
  expect(guestOn?.enabled).toBeTrue();
  expect(sms?.action).toEqual('send_sms');
  expect(sms?.phoneNumbers?.[0]).toEqual('+123');
  expect(networkMode?.action).toEqual('set_net_mode');
  expect(networkMode?.networkMode).toEqual('00');
});

export default tap.start();
