import { expect, tap } from '@git.zone/tstest/tapbundle';
import { DelugeMapper, type IDelugeRawData } from '../../ts/integrations/deluge/index.js';

const rawData: Partial<IDelugeRawData> = {
  webUi: {
    connected: true,
    stats: {
      download_rate: 2048,
      upload_rate: 1024,
      download_protocol_rate: 512,
      upload_protocol_rate: 256,
      num_connections: 12,
    },
    torrents: {
      abc: { name: 'Ubuntu ISO', state: 'Downloading', paused: false, progress: 42, total_size: 1000, total_done: 420 },
      def: { name: 'Archive Seed', state: 'Seeding', paused: false, progress: 100, ratio: 2.5 },
      ghi: { name: 'Paused Torrent', state: 'Paused', paused: true, progress: 10 },
    },
  },
  fetchedAt: '2026-01-01T00:00:00.000Z',
};

tap.test('maps Deluge Web JSON-RPC data to snapshots, devices and entities', async () => {
  const snapshot = DelugeMapper.toSnapshot({ config: { name: 'Local Deluge', host: '192.168.1.5', password: 'deluge' }, rawData, online: true, source: 'manual' });
  const devices = DelugeMapper.toDevices(snapshot);
  const entities = DelugeMapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(snapshot.sensors.find((sensorArg) => sensorArg.key === 'current_status')?.value).toEqual('seeding_and_downloading');
  expect(snapshot.sensors.find((sensorArg) => sensorArg.key === 'download_speed')?.value).toEqual(2);
  expect(snapshot.sensors.find((sensorArg) => sensorArg.key === 'downloading_count')?.value).toEqual(1);
  expect(snapshot.sensors.find((sensorArg) => sensorArg.key === 'seeding_count')?.value).toEqual(1);
  expect(snapshot.switches[0].isOn).toBeTrue();
  expect(devices[0].features.some((featureArg) => featureArg.id === 'enabled')).toBeTrue();
  expect(entities.find((entityArg) => entityArg.id === 'sensor.local_deluge_download_speed')?.state).toEqual(2);
  expect(entities.find((entityArg) => entityArg.id === 'switch.local_deluge_enabled')?.state).toEqual('on');
});

tap.test('maps Deluge services to native command requests', async () => {
  const snapshot = DelugeMapper.toSnapshot({ config: { name: 'Local Deluge' }, rawData, online: true, source: 'manual' });
  const pause = DelugeMapper.commandForService(snapshot, { domain: 'switch', service: 'turn_off', target: { entityId: 'switch.local_deluge_enabled' } });
  const add = DelugeMapper.commandForService(snapshot, { domain: 'deluge', service: 'add_magnet', target: {}, data: { magnet: 'magnet:?xt=urn:btih:123', options: { download_location: '/data' } } });
  const remove = DelugeMapper.commandForService(snapshot, { domain: 'deluge', service: 'remove_torrent', target: {}, data: { torrent_id: 'abc', remove_data: true } });

  expect(pause?.action).toEqual('pause');
  expect(pause?.torrentIds).toEqual([]);
  expect(add?.action).toEqual('add_torrent');
  expect(add?.magnet).toEqual('magnet:?xt=urn:btih:123');
  expect(add?.options?.download_location).toEqual('/data');
  expect(remove?.action).toEqual('remove_torrent');
  expect(remove?.torrentIds).toEqual(['abc']);
  expect(remove?.removeData).toBeTrue();
});

export default tap.start();
