import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HomeWizardMapper, type IHomeWizardRawData } from '../../ts/integrations/homewizard/index.js';

const socketRawData: Partial<IHomeWizardRawData> = {
  device: {
    product_type: 'HWE-SKT',
    product_name: 'Energy Socket',
    serial: 'AABBCCDDEEFF',
    firmware_version: '4.12',
    api_version: 'v1',
  },
  measurement: {
    wifi_ssid: 'Local Wi-Fi',
    wifi_strength: 88,
    total_power_import_kwh: 30.511,
    total_power_export_kwh: 2.5,
    active_power_w: 543.312,
    active_voltage_v: 231.539,
    active_current_a: 2.346,
    active_power_factor: 0.81688,
  },
  system: {
    cloud_enabled: true,
  },
  state: {
    power_on: true,
    switch_lock: false,
    brightness: 128,
  },
};

tap.test('maps HomeWizard v1 snapshots to devices and entities', async () => {
  const snapshot = HomeWizardMapper.toSnapshot({ config: { host: '192.168.1.50' }, rawData: socketRawData, online: true, source: 'manual' });
  const devices = HomeWizardMapper.toDevices(snapshot);
  const entities = HomeWizardMapper.toEntities(snapshot);

  expect(snapshot.measurement.energy_import_kwh).toEqual(30.511);
  expect(snapshot.system?.wifi_strength_pct).toEqual(88);
  expect(snapshot.capabilities.powerSwitch).toBeTrue();
  expect(devices[0].id).toEqual('homewizard.aabbccddeeff');
  expect(entities.find((entityArg) => entityArg.attributes?.key === 'active_power_w')?.state).toEqual(543.312);
  expect(entities.find((entityArg) => entityArg.attributes?.key === 'active_power_factor')?.state).toEqual(81.688);
  expect(entities.find((entityArg) => entityArg.attributes?.key === 'power_on')?.state).toEqual('on');
  expect(entities.find((entityArg) => entityArg.attributes?.key === 'status_light_brightness')?.state).toEqual(50);
});

tap.test('maps Home Assistant-style services to HomeWizard commands', async () => {
  const snapshot = HomeWizardMapper.toSnapshot({ config: { host: '192.168.1.50' }, rawData: socketRawData, online: true, source: 'manual' });
  const entities = HomeWizardMapper.toEntities(snapshot);
  const powerSwitch = entities.find((entityArg) => entityArg.attributes?.key === 'power_on')!;
  const cloudSwitch = entities.find((entityArg) => entityArg.attributes?.key === 'cloud_connection')!;
  const brightness = entities.find((entityArg) => entityArg.attributes?.key === 'status_light_brightness')!;

  const powerCommand = HomeWizardMapper.commandForService(snapshot, { domain: 'switch', service: 'turn_off', target: { entityId: powerSwitch.id } });
  const cloudCommand = HomeWizardMapper.commandForService(snapshot, { domain: 'switch', service: 'turn_off', target: { entityId: cloudSwitch.id } });
  const brightnessCommand = HomeWizardMapper.commandForService(snapshot, { domain: 'number', service: 'set_value', target: { entityId: brightness.id }, data: { value: 25 } });

  expect(powerCommand?.action).toEqual('set_state');
  expect(powerCommand?.powerOn).toBeFalse();
  expect(cloudCommand?.action).toEqual('set_system');
  expect(cloudCommand?.cloudEnabled).toBeFalse();
  expect(brightnessCommand?.statusLedBrightnessPct).toEqual(25);
});

tap.test('maps v2 battery group data and select commands', async () => {
  const snapshot = HomeWizardMapper.toSnapshot({
    config: { host: '192.168.1.52', apiVersion: 'v2', token: 'TOKEN' },
    online: true,
    source: 'manual',
    rawData: {
      device: { product_type: 'HWE-P1', product_name: 'P1 Meter', serial: '112233445566', firmware_version: '6.04', api_version: '2.2.0' },
      measurement: { power_w: -100, energy_import_kwh: 10 },
      system: { status_led_brightness_pct: 75, cloud_enabled: true },
      batteries: { mode: 'zero', permissions: ['charge_allowed', 'discharge_allowed'], battery_count: 2, power_w: -404 },
    },
  });
  const batterySelect = HomeWizardMapper.toEntities(snapshot).find((entityArg) => entityArg.attributes?.key === 'battery_group_mode')!;
  const command = HomeWizardMapper.commandForService(snapshot, { domain: 'select', service: 'select_option', target: { entityId: batterySelect.id }, data: { option: 'zero_charge_only' } });

  expect(batterySelect.state).toEqual('zero');
  expect((batterySelect.attributes?.options as string[]).includes('zero_charge_only')).toBeTrue();
  expect(command?.batteryMode).toEqual('zero_charge_only');
});

export default tap.start();
