import { expect, tap } from '@git.zone/tstest/tapbundle';
import { SynologyDsmMapper, type ISynologyDsmSnapshot } from '../../ts/integrations/synology_dsm/index.js';

const snapshot: ISynologyDsmSnapshot = {
  connected: true,
  updatedAt: '2026-01-01T00:00:00.000Z',
  system: {
    serial: 'SYN123',
    name: 'DiskStation',
    hostname: 'diskstation',
    host: '192.168.1.20',
    port: 5001,
    ssl: true,
    model: 'DS920+',
    versionString: 'DSM 7.2.2-72806',
    temperature: 42,
    uptimeSeconds: 3600,
    macs: ['AA:BB:CC:DD:EE:FF'],
  },
  utilization: {
    cpuUserLoad: 12,
    cpuSystemLoad: 5,
    cpuTotalLoad: 17,
    cpu5MinLoad: 23,
    memoryRealUsage: 64,
    memorySize: 8_589_934_592,
    networkUp: 1024,
    networkDown: 2048,
  },
  storage: {
    volumes: [
      { id: 'volume_1', name: 'Volume 1', status: 'normal', sizeTotal: 4_000, sizeUsed: 2_000, percentageUsed: 50, diskTempAvg: 38, diskTempMax: 41, deviceType: 'shr' },
    ],
    disks: [
      { id: 'disk_1', name: 'Drive 1', vendor: 'Seagate', model: 'IronWolf', status: 'normal', smartStatus: 'normal', temperature: 36, exceedBadSectorThreshold: false, belowRemainLifeThreshold: false },
    ],
  },
  network: {
    hostname: 'diskstation',
    macs: ['AA:BB:CC:DD:EE:FF'],
    uploadRate: 1024,
    downloadRate: 2048,
  },
  cameras: [
    { id: '1', name: 'Front Door', model: 'BC500', enabled: true, recording: true, motionDetectionEnabled: true, rtsp: 'rtsp://nas/camera/1' },
  ],
  update: {
    installedVersion: 'DSM 7.2.2-72806',
    latestVersion: 'DSM 7.3-73000',
    updateAvailable: true,
    releaseUrl: 'http://update.synology.com/autoupdate/whatsnew.php?model=DS920%2B&update_version=73000',
  },
  switches: [
    { key: 'home_mode', name: 'Home mode', enabled: true, type: 'home_mode' },
  ],
  security: {
    status: 'safe',
    statusByCheck: { malware: 'safe' },
  },
  actions: [],
};

tap.test('maps Synology DSM system, storage, network, camera, switch, and update snapshot data', async () => {
  const normalized = SynologyDsmMapper.toSnapshot({ snapshot });
  const devices = SynologyDsmMapper.toDevices(normalized);
  const entities = SynologyDsmMapper.toEntities(normalized);

  expect(devices.some((deviceArg) => deviceArg.id === 'synology_dsm.nas.syn123')).toBeTrue();
  expect(devices.some((deviceArg) => deviceArg.id === 'synology_dsm.volume.syn123.volume_1')).toBeTrue();
  expect(devices.some((deviceArg) => deviceArg.id === 'synology_dsm.disk.syn123.disk_1')).toBeTrue();
  expect(devices.some((deviceArg) => deviceArg.id === 'synology_dsm.camera.syn123.1')).toBeTrue();
  expect(entities.find((entityArg) => entityArg.attributes?.nativeKey === 'cpu_user_load')?.state).toEqual(12);
  expect(entities.find((entityArg) => entityArg.attributes?.nativeKey === 'cpu_5min_load')?.state).toEqual(0.23);
  expect(entities.find((entityArg) => entityArg.attributes?.nativeKey === 'volume_percentage_used')?.state).toEqual(50);
  expect(entities.find((entityArg) => entityArg.attributes?.nativeKey === 'disk_temp')?.state).toEqual(36);
  expect(entities.find((entityArg) => entityArg.attributes?.nativeKey === 'network_down')?.state).toEqual(2048);
  expect(entities.find((entityArg) => entityArg.id === 'binary_sensor.front_door_motion_detection')?.state).toEqual('on');
  expect(entities.find((entityArg) => entityArg.id === 'switch.diskstation_surveillance_station_home_mode')?.state).toEqual('on');
  expect(entities.find((entityArg) => entityArg.id === 'update.diskstation_dsm_update')?.attributes?.latestVersion).toEqual('DSM 7.3-73000');
});

tap.test('models represented Synology DSM commands without executing them', async () => {
  const normalized = SynologyDsmMapper.toSnapshot({ snapshot });
  const rebootCommand = SynologyDsmMapper.commandForService(normalized, {
    domain: 'synology_dsm',
    service: 'reboot',
    target: {},
  });
  const homeModeCommand = SynologyDsmMapper.commandForService(normalized, {
    domain: 'switch',
    service: 'turn_off',
    target: { entityId: 'switch.diskstation_surveillance_station_home_mode' },
  });
  const shutdownCommand = SynologyDsmMapper.commandForService(normalized, {
    domain: 'button',
    service: 'press',
    target: { entityId: 'button.diskstation_shutdown' },
  });
  const cameraCommand = SynologyDsmMapper.commandForService(normalized, {
    domain: 'camera',
    service: 'disable_motion_detection',
    target: { deviceId: 'synology_dsm.camera.syn123.1' },
  });

  expect(rebootCommand?.type).toEqual('system.action');
  expect(rebootCommand?.action).toEqual('reboot');
  expect(homeModeCommand?.type).toEqual('switch.set');
  expect(homeModeCommand?.payload?.enabled).toBeFalse();
  expect(shutdownCommand?.action).toEqual('shutdown');
  expect(cameraCommand?.type).toEqual('camera.action');
  expect(cameraCommand?.cameraId).toEqual('1');
});

export default tap.start();
