import { expect, tap } from '@git.zone/tstest/tapbundle';
import { GoodweMapper, type IGoodweRawData } from '../../ts/integrations/goodwe/index.js';

const rawData: Partial<IGoodweRawData> = {
  deviceInfo: {
    host: '192.168.1.50',
    port: 8899,
    name: 'Garage Inverter',
    model: 'GW10K-ET',
    modelFamily: 'ET',
    serialNumber: 'ETU123456789',
    firmware: '1010',
    armFirmware: '2020',
    ratedPower: 10000,
  },
  runtimeData: {
    ppv: 4200,
    house_consumption: 1800,
    active_power: 2100,
    battery_soc: 67,
    e_day: 12.4,
    e_total: 5567.8,
  },
  settingsData: {
    grid_export_limit: 5000,
    battery_discharge_depth: 80,
  },
  sensors: [
    { id: 'ppv', name: 'PV Power', unit: 'W', kind: 'PV' },
    { id: 'battery_soc', name: 'Battery SoC', unit: '%', kind: 'BAT' },
    { id: 'e_total', name: 'Total Energy', unit: 'kWh' },
  ],
  settings: [
    { id: 'grid_export_limit', name: 'Grid Export Limit', unit: 'W' },
    { id: 'battery_discharge_depth', name: 'Depth of Discharge', unit: '%' },
    { id: 'time', name: 'Time' },
  ],
  operationMode: 0,
  supportedOperationModes: [0, 1, 2, 3, 4, 98, 99],
  fetchedAt: '2026-01-01T00:00:00.000Z',
};

tap.test('maps GoodWe raw runtime data to devices and entities', async () => {
  const snapshot = GoodweMapper.toSnapshot({ config: { commandExecutor: { execute: async () => ({ ok: true }) } }, rawData, online: true, source: 'manual' });
  const devices = GoodweMapper.toDevices(snapshot);
  const entities = GoodweMapper.toEntities(snapshot);

  expect(snapshot.inverter.serialNumber).toEqual('ETU123456789');
  expect(snapshot.controls.operationMode).toEqual('general');
  expect(snapshot.capabilities.localControl).toBeTrue();
  expect(devices[0].id).toEqual('goodwe.inverter.etu123456789');
  expect(devices[0].features.some((featureArg) => featureArg.id === 'ppv')).toBeTrue();
  expect(entities.find((entityArg) => entityArg.id === 'sensor.garage_inverter_pv_power')?.state).toEqual(4200);
  expect(entities.find((entityArg) => entityArg.id === 'sensor.garage_inverter_battery_soc')?.attributes?.deviceClass).toEqual('battery');
  expect(entities.find((entityArg) => entityArg.id === 'select.garage_inverter_operation_mode')?.attributes?.options).toEqual(['general', 'off_grid', 'backup', 'eco', 'peak_shaving', 'eco_charge', 'eco_discharge']);
  expect(entities.some((entityArg) => entityArg.id === 'button.garage_inverter_synchronize_clock')).toBeTrue();
});

tap.test('maps Home Assistant-style services to GoodWe command requests', async () => {
  const snapshot = GoodweMapper.toSnapshot({ config: {}, rawData, online: true, source: 'manual' });
  const modeCommand = GoodweMapper.commandForService(snapshot, {
    domain: 'select',
    service: 'select_option',
    target: { entityId: 'select.garage_inverter_operation_mode' },
    data: { option: 'eco_charge' },
  });
  const limitCommand = GoodweMapper.commandForService(snapshot, {
    domain: 'number',
    service: 'set_value',
    target: { entityId: 'number.garage_inverter_grid_export_limit' },
    data: { value: 3500 },
  });

  expect(modeCommand?.action).toEqual('set_operation_mode');
  expect(modeCommand?.operationMode).toEqual('eco_charge');
  expect(limitCommand?.action).toEqual('set_grid_export_limit');
  expect(limitCommand?.gridExportLimit).toEqual(3500);
});

export default tap.start();
