import { expect, tap } from '@git.zone/tstest/tapbundle';
import { TplinkMapper, type ITplinkSnapshot } from '../../ts/integrations/tplink/index.js';

const snapshot: ITplinkSnapshot = {
  connected: true,
  devices: [
    {
      id: 'bulb-1',
      alias: 'Living Lamp',
      model: 'KL130',
      type: 'bulb',
      macAddress: '1C:3B:F3:00:11:22',
      state: {
        state: true,
        brightness: 80,
        color_temperature: 3000,
        rgb: { r: 255, g: 100, b: 10 },
        current_consumption: 7.5,
        rssi: -53,
      },
    },
    {
      id: 'strip-1',
      alias: 'Office Strip',
      model: 'HS300',
      type: 'strip',
      state: { state: true },
      children: [
        {
          id: 'strip-1-outlet-1',
          alias: 'Printer',
          model: 'HS300 Outlet',
          type: 'plug',
          state: {
            state: false,
            current_consumption: 2.25,
          },
        },
      ],
    },
    {
      id: 'sensor-1',
      alias: 'Back Door',
      model: 'T110',
      type: 'sensor',
      state: {
        is_open: true,
        battery_level: 92,
      },
    },
  ],
  entities: [],
  events: [],
};

tap.test('maps TP-Link plugs, strips, bulbs, and sensors', async () => {
  const devices = TplinkMapper.toDevices(snapshot);
  const entities = TplinkMapper.toEntities(snapshot);

  expect(devices.some((deviceArg) => deviceArg.id === 'tplink.device.1c_3b_f3_00_11_22')).toBeTrue();
  expect(devices.some((deviceArg) => deviceArg.id === 'tplink.device.strip_1_outlet_1')).toBeTrue();
  expect(entities.find((entityArg) => entityArg.id === 'light.living_lamp')?.state).toEqual('on');
  expect(entities.find((entityArg) => entityArg.id === 'switch.printer')?.state).toEqual('off');
  expect(entities.find((entityArg) => entityArg.id === 'sensor.living_lamp_current_consumption')?.state).toEqual(7.5);
  expect(entities.find((entityArg) => entityArg.id === 'binary_sensor.back_door_is_open')?.state).toEqual('on');
  expect(entities.find((entityArg) => entityArg.id === 'sensor.back_door_battery_level')?.state).toEqual(92);
});

tap.test('maps canonical services to TP-Link feature commands', async () => {
  const turnOnCommand = TplinkMapper.commandForService(snapshot, {
    domain: 'light',
    service: 'turn_on',
    target: { entityId: 'light.living_lamp' },
    data: { brightness_pct: 50, color_temp_kelvin: 2700, rgb_color: [10, 20, 30] },
  });
  const switchCommand = TplinkMapper.commandForService(snapshot, {
    domain: 'switch',
    service: 'turn_off',
    target: { entityId: 'switch.printer' },
  });
  const numberCommand = TplinkMapper.commandForService(snapshot, {
    domain: 'number',
    service: 'set_value',
    target: { deviceId: 'tplink.device.1c_3b_f3_00_11_22' },
    data: { featureId: 'brightness', value: 30 },
  });

  expect(turnOnCommand?.payload.brightness).toEqual(50);
  expect(turnOnCommand?.payload.color_temperature).toEqual(2700);
  expect(turnOnCommand?.payload.rgb).toEqual({ r: 10, g: 20, b: 30 });
  expect(switchCommand?.featureId).toEqual('state');
  expect(switchCommand?.value).toEqual(false);
  expect(numberCommand?.featureId).toEqual('brightness');
  expect(numberCommand?.value).toEqual(30);
});

export default tap.start();
