import { expect, tap } from '@git.zone/tstest/tapbundle';
import { DeconzMapper, type IDeconzSnapshot } from '../../ts/integrations/deconz/index.js';

const snapshot: IDeconzSnapshot = {
  config: {
    bridgeid: '00212EFFFF00C5FB',
    name: 'RaspBee GW',
    devicename: 'ConBee II',
    modelid: 'deCONZ',
    swversion: '2.26.3',
    rfconnected: true,
    websocketport: 8088,
  },
  lights: {
    '1': {
      name: 'Kitchen Ceiling',
      manufacturername: 'IKEA',
      modelid: 'TRADFRI bulb E27',
      type: 'Extended color light',
      uniqueid: '00:0b:57:ff:fe:9a:46:ab-01',
      state: {
        on: true,
        bri: 204,
        ct: 370,
        reachable: true,
      },
    },
    '2': {
      name: 'Counter Plug',
      manufacturername: 'dresden elektronik',
      modelid: 'Smart plug',
      type: 'Smart plug',
      uniqueid: '00:0b:57:ff:fe:9a:46:ac-01',
      state: {
        on: false,
        reachable: true,
      },
    },
    '3': {
      name: 'Living Blind',
      manufacturername: 'ubisys',
      modelid: 'J1',
      type: 'Window covering device',
      uniqueid: '00:0b:57:ff:fe:9a:46:ad-01',
      state: {
        open: true,
        lift: 25,
        reachable: true,
      },
    },
  },
  groups: {
    '4': {
      name: 'Living Room',
      lights: ['1', '3'],
      state: {
        all_on: false,
        any_on: true,
      },
      action: {
        on: true,
        bri: 128,
      },
    },
  },
  sensors: {
    '5': {
      name: 'Hall Temperature',
      manufacturername: 'Xiaomi',
      modelid: 'lumi.weather',
      type: 'ZHATemperature',
      uniqueid: '00:15:8d:00:01:aa:bb:cc-01-0402',
      config: {
        battery: 88,
        on: true,
        reachable: true,
      },
      state: {
        temperature: 2150,
        lastupdated: '2026-05-05T08:00:00',
      },
    },
    '6': {
      name: 'Hall Motion',
      manufacturername: 'Philips',
      modelid: 'SML001',
      type: 'ZHAPresence',
      uniqueid: '00:17:88:01:02:03:04:05-02-0406',
      config: {
        lowbattery: false,
        on: true,
        reachable: true,
      },
      state: {
        presence: true,
        lastupdated: '2026-05-05T08:01:00',
      },
    },
    '7': {
      name: 'Radiator',
      manufacturername: 'Eurotronic',
      modelid: 'SPZB0001',
      type: 'ZHAThermostat',
      uniqueid: '00:15:8d:00:01:aa:bb:dd-01-0201',
      config: {
        heatsetpoint: 2200,
        locked: false,
        mode: 'heat',
        on: true,
        reachable: true,
      },
      state: {
        temperature: 2010,
        lastupdated: '2026-05-05T08:02:00',
      },
    },
  },
};

tap.test('maps deCONZ lights, groups, sensors, covers, and climate entities', async () => {
  const devices = DeconzMapper.toDevices(snapshot);
  const entities = DeconzMapper.toEntities(snapshot);

  expect(devices.some((deviceArg) => deviceArg.id === 'deconz.gateway.00212effff00c5fb')).toBeTrue();
  expect(devices.some((deviceArg) => deviceArg.features.some((featureArg) => featureArg.capability === 'cover'))).toBeTrue();

  expect(entities.find((entityArg) => entityArg.id === 'light.kitchen_ceiling')?.state).toEqual('on');
  expect(entities.find((entityArg) => entityArg.id === 'switch.counter_plug')?.state).toEqual('off');
  expect(entities.find((entityArg) => entityArg.id === 'cover.living_blind')?.attributes?.position).toEqual(75);
  expect(entities.find((entityArg) => entityArg.id === 'light.living_room')?.attributes?.isDeconzGroup).toEqual(true);
  expect(entities.find((entityArg) => entityArg.id === 'sensor.hall_temperature')?.state).toEqual(21.5);
  expect(entities.find((entityArg) => entityArg.id === 'binary_sensor.hall_motion')?.state).toEqual('on');
  expect(entities.find((entityArg) => entityArg.id === 'climate.radiator')?.attributes?.targetTemperature).toEqual(22);
});

export default tap.start();
