import { expect, tap } from '@git.zone/tstest/tapbundle';
import { VelbusMapper, type IVelbusSnapshot } from '../../ts/integrations/velbus/index.js';

const snapshot: IVelbusSnapshot = {
  gateway: {
    id: 'velbus-gateway',
    name: 'Velbus Gateway',
    connection: 'tcp',
    host: '192.168.1.60',
    port: 27015,
    tls: true,
  },
  connected: true,
  updatedAt: '2026-01-01T00:00:00.000Z',
  modules: [{
    address: 1,
    name: 'Cabinet Module',
    type: 0x26,
    typeName: 'VMB4RYLD-20',
    swVersion: '1.0',
    serialNumber: 'MOD001',
    channels: [
      { id: 'relay-1', channelNumber: 1, kind: 'relay', name: 'Kitchen Relay', state: true },
      { id: 'dimmer-1', channelNumber: 2, kind: 'dimmer', name: 'Living Dimmer', state: true, brightness: 75 },
      { id: 'input-1', channelNumber: 3, kind: 'button', name: 'Door Contact', state: 'closed', deviceClass: 'door' },
      { id: 'blind-1', channelNumber: 4, kind: 'blind', name: 'Kitchen Blind', state: 'open', position: 60 },
      { id: 'temp-1', channelNumber: 5, kind: 'temperature', name: 'Hall Temperature', currentTemperature: 21.5, unit: 'C' },
      { id: 'thermostat-1', channelNumber: 6, kind: 'climate', name: 'Hall Thermostat', currentTemperature: 21.5, targetTemperature: 22, hvacMode: 'heat', presetMode: 'home', unit: 'C' },
    ],
  }],
};

tap.test('maps Velbus modules and channels to canonical devices and entities', async () => {
  const devices = VelbusMapper.toDevices(snapshot);
  const entities = VelbusMapper.toEntities(snapshot);
  expect(devices.some((deviceArg) => deviceArg.id === 'velbus.gateway.velbus_gateway')).toBeTrue();
  expect(devices.some((deviceArg) => deviceArg.id === 'velbus.module.mod001')).toBeTrue();
  expect(entities.some((entityArg) => entityArg.platform === 'light' && entityArg.id === 'light.living_dimmer' && entityArg.attributes?.brightness === 75)).toBeTrue();
  expect(entities.some((entityArg) => entityArg.platform === 'switch' && entityArg.id === 'switch.kitchen_relay' && entityArg.state === 'on')).toBeTrue();
  expect(entities.some((entityArg) => entityArg.platform === 'binary_sensor' && entityArg.id === 'binary_sensor.door_contact' && entityArg.state === 'on')).toBeTrue();
  expect(entities.some((entityArg) => entityArg.platform === 'sensor' && entityArg.id === 'sensor.hall_temperature' && entityArg.state === 21.5)).toBeTrue();
  expect(entities.some((entityArg) => entityArg.platform === 'cover' && entityArg.id === 'cover.kitchen_blind' && entityArg.attributes?.currentPosition === 60)).toBeTrue();
  expect(entities.some((entityArg) => entityArg.platform === 'climate' && entityArg.id === 'climate.hall_thermostat' && entityArg.attributes?.targetTemperature === 22)).toBeTrue();
});

tap.test('maps supported Velbus services to commands', async () => {
  const lightCommand = VelbusMapper.commandForService(snapshot, {
    domain: 'light',
    service: 'turn_on',
    target: { entityId: 'light.living_dimmer' },
    data: { brightness: 128 },
  });
  expect(lightCommand).toEqual({ type: 'turn_on', moduleAddress: 1, channelId: 'dimmer-1', channelNumber: 2, platform: 'light', entityId: 'light.living_dimmer', value: 50 });

  const switchCommand = VelbusMapper.commandForService(snapshot, {
    domain: 'switch',
    service: 'turn_off',
    target: { entityId: 'switch.kitchen_relay' },
  });
  expect(switchCommand).toEqual({ type: 'turn_off', moduleAddress: 1, channelId: 'relay-1', channelNumber: 1, platform: 'switch', entityId: 'switch.kitchen_relay' });

  const velbusTurnOffCommand = VelbusMapper.commandForService(snapshot, {
    domain: 'velbus',
    service: 'turn_off',
    target: { entityId: 'light.living_dimmer' },
  });
  expect(velbusTurnOffCommand?.type).toEqual('turn_off');

  const setValueCommand = VelbusMapper.commandForService(snapshot, {
    domain: 'velbus',
    service: 'set_value',
    target: { entityId: 'cover.kitchen_blind' },
    data: { value: 30 },
  });
  expect(setValueCommand).toEqual({ type: 'set_value', moduleAddress: 1, channelId: 'blind-1', channelNumber: 4, platform: 'cover', entityId: 'cover.kitchen_blind', value: 30 });

  const openCommand = VelbusMapper.commandForService(snapshot, {
    domain: 'cover',
    service: 'open_cover',
    target: { entityId: 'cover.kitchen_blind' },
  });
  expect(openCommand?.type).toEqual('open');

  const closeCommand = VelbusMapper.commandForService(snapshot, {
    domain: 'cover',
    service: 'close_cover',
    target: { entityId: 'cover.kitchen_blind' },
  });
  expect(closeCommand?.type).toEqual('close');
});

export default tap.start();
