import { expect, tap } from '@git.zone/tstest/tapbundle';
import { ElgatoMapper, type IElgatoRawData } from '../../ts/integrations/elgato/index.js';

const rawData: Partial<IElgatoRawData> = {
  info: {
    productName: 'Elgato Key Light Mini',
    serialNumber: 'ELG123',
    displayName: 'Desk Key Light',
    firmwareVersion: '1.0.3',
    firmwareBuildNumber: 42,
    hardwareBoardType: 7,
    macAddress: 'AABBCCDDEEFF',
  },
  settings: {
    colorChangeDurationMs: 100,
    powerOnBehavior: 1,
    powerOnBrightness: 40,
    switchOffDurationMs: 300,
    switchOnDurationMs: 300,
    battery: {
      bypass: 1,
      energySaving: { enable: 0, minimumBatteryLevel: 20, disableWifi: 0, adjustBrightness: { enable: 1, brightness: 25 } },
    },
  },
  state: { on: 1, brightness: 50, temperature: 200 },
  battery: { level: 88, status: 2, powerSource: 1, currentBatteryVoltage: 4100, inputChargeVoltage: 5000, inputChargeCurrent: 1000 },
};

tap.test('maps Elgato raw API data to snapshots, devices, entities, and battery settings', async () => {
  const snapshot = ElgatoMapper.toSnapshot({ config: { host: 'key-light.local' }, rawData, online: true, source: 'manual' });
  const devices = ElgatoMapper.toDevices(snapshot);
  const entities = ElgatoMapper.toEntities(snapshot);

  expect(snapshot.device.serialNumber).toEqual('ELG123');
  expect(snapshot.device.macAddress).toEqual('aa:bb:cc:dd:ee:ff');
  expect(snapshot.light.on).toBeTrue();
  expect(snapshot.light.brightness255).toEqual(128);
  expect(snapshot.light.colorTemperatureKelvin).toEqual(5000);
  expect(snapshot.light.supportedColorModes).toEqual(['color_temp']);
  expect(snapshot.battery?.level).toEqual(88);
  expect(snapshot.battery?.chargePower).toEqual(5);
  expect(snapshot.battery?.bypass).toBeTrue();
  expect(snapshot.battery?.energySavingEnabled).toBeFalse();
  expect(devices[0].id).toEqual('elgato.light.elg123');
  expect(devices[0].metadata?.serialNumber).toEqual('ELG123');
  expect(entities.find((entityArg) => entityArg.attributes?.key === 'light')?.state).toEqual('on');
  expect(entities.find((entityArg) => entityArg.attributes?.key === 'battery')?.state).toEqual(88);
  expect(entities.find((entityArg) => entityArg.attributes?.key === 'bypass')?.state).toEqual('on');
  expect(entities.find((entityArg) => entityArg.attributes?.key === 'identify')?.available).toBeTrue();
});

tap.test('maps Home Assistant style services to Elgato command shapes', async () => {
  const snapshot = ElgatoMapper.toSnapshot({ config: { host: 'key-light.local' }, rawData, online: true, source: 'manual' });
  const turnOn = ElgatoMapper.commandForService(snapshot, { domain: 'light', service: 'turn_on', target: { entityId: 'light.desk_key_light' }, data: { brightness: 128, color_temp_kelvin: 5000 } });
  const turnOff = ElgatoMapper.commandForService(snapshot, { domain: 'light', service: 'turn_off', target: { entityId: 'light.desk_key_light' }, data: {} });
  const identify = ElgatoMapper.commandForService(snapshot, { domain: 'button', service: 'press', target: { entityId: 'button.desk_key_light_identify' } });
  const bypassOff = ElgatoMapper.commandForService(snapshot, { domain: 'switch', service: 'turn_off', target: { entityId: 'switch.desk_key_light_bypass' } });

  expect(turnOn?.action).toEqual('set_light');
  expect(turnOn?.on).toBeTrue();
  expect(turnOn?.brightness).toEqual(50);
  expect(turnOn?.colorTemperatureKelvin).toEqual(5000);
  expect(turnOff?.on).toBeFalse();
  expect(identify?.action).toEqual('identify');
  expect(bypassOff?.action).toEqual('battery_bypass');
  expect(bypassOff?.enabled).toBeFalse();
});

export default tap.start();
