import { expect, tap } from '@git.zone/tstest/tapbundle';
import { BondMapper, bondActions, bondDeviceTypes } from '../../ts/integrations/bond/index.js';

const snapshot = BondMapper.toSnapshot({
  config: { host: '192.168.1.45', accessToken: 'token' },
  source: 'http',
  online: true,
  rawData: {
    version: {
      bondid: 'ZZ12345',
      make: 'Olibra',
      target: 'zermatt',
      fw_ver: '3.25.1',
    },
    bridge: {
      name: 'Patio Bond',
      location: 'Patio',
    },
    devices: [
      {
        deviceId: 'fan-device',
        attrs: {
          name: 'Living Room Fan',
          type: bondDeviceTypes.ceilingFan,
          actions: [bondActions.turnOn, bondActions.turnOff, bondActions.setSpeed, bondActions.setDirection, bondActions.turnLightOn, bondActions.turnLightOff, bondActions.setBrightness, bondActions.toggleLight, bondActions.stop, bondActions.preset],
        },
        props: {
          max_speed: 3,
          trust_state: true,
        },
        state: {
          power: 1,
          speed: 2,
          direction: 1,
          light: 1,
          brightness: 50,
        },
      },
      {
        deviceId: 'shade-device',
        attrs: {
          name: 'Office Shade',
          type: bondDeviceTypes.motorizedShades,
          actions: [bondActions.open, bondActions.close, bondActions.hold, bondActions.setPosition],
        },
        state: {
          open: 1,
          position: 25,
        },
      },
    ],
  },
});

tap.test('maps Bond snapshots to hub/device definitions and entities', async () => {
  const devices = BondMapper.toDevices(snapshot);
  const entities = BondMapper.toEntities(snapshot);

  expect(devices[0].id).toEqual('bond.hub.zz12345');
  expect(devices.some((deviceArg) => deviceArg.id === 'bond.device.zz12345_fan_device')).toBeTrue();
  expect(entities.find((entityArg) => entityArg.id === 'fan.living_room_fan')?.state).toEqual('on');
  expect(entities.find((entityArg) => entityArg.id === 'fan.living_room_fan')?.attributes?.percentage).toEqual(50);
  expect(entities.find((entityArg) => entityArg.id === 'light.living_room_fan')?.attributes?.brightness).toEqual(128);
  expect(entities.find((entityArg) => entityArg.id === 'cover.office_shade')?.attributes?.currentPosition).toEqual(75);
  expect(entities.some((entityArg) => entityArg.id === 'button.living_room_fan_button_preset')).toBeTrue();
});

tap.test('maps Home Assistant-style services to Bond local API commands', async () => {
  const fanCommand = BondMapper.commandForService(snapshot, {
    domain: 'fan',
    service: 'set_percentage',
    target: { entityId: 'fan.living_room_fan' },
    data: { percentage: 100 },
  });
  const coverCommand = BondMapper.commandForService(snapshot, {
    domain: 'cover',
    service: 'set_cover_position',
    target: { entityId: 'cover.office_shade' },
    data: { position: 80 },
  });

  expect(fanCommand?.action).toEqual(bondActions.setSpeed);
  expect(fanCommand?.argument).toEqual(3);
  expect(coverCommand?.action).toEqual(bondActions.setPosition);
  expect(coverCommand?.argument).toEqual(20);
});

export default tap.start();
