import { expect, tap } from '@git.zone/tstest/tapbundle';
import { JellyfinMapper, type IJellyfinSnapshot } from '../../ts/integrations/jellyfin/index.js';

const snapshot: IJellyfinSnapshot = {
  server: {
    Id: 'server-1',
    Name: 'Home Jellyfin',
    Version: '10.10.7',
  },
  online: true,
  updatedAt: '2026-05-05T12:00:00.000Z',
  sessions: [
    {
      Id: 'session-1',
      UserName: 'phil',
      Client: 'Jellyfin Web',
      DeviceName: 'Living Room Browser',
      DeviceId: 'device-1',
      ApplicationVersion: '10.10.7',
      IsActive: true,
      SupportsRemoteControl: true,
      Capabilities: {
        SupportsMediaControl: true,
        SupportsPersistentIdentifier: true,
        SupportedCommands: ['Pause', 'Unpause', 'Stop', 'SetVolume'],
      },
      LastPlaybackCheckIn: '2026-05-05T12:01:00.000Z',
      PlayState: {
        IsPaused: false,
        IsMuted: false,
        PositionTicks: 1800000000,
        VolumeLevel: 55,
      },
      NowPlayingItem: {
        Id: 'movie-1',
        Name: 'Example Movie',
        Type: 'Movie',
        RunTimeTicks: 72000000000,
      },
    },
  ],
};

tap.test('maps active Jellyfin sessions to media devices', async () => {
  const devices = JellyfinMapper.toDevices(snapshot);
  expect(devices.some((deviceArg) => deviceArg.id === 'jellyfin.server.server_1')).toBeTrue();
  const sessionDevice = devices.find((deviceArg) => deviceArg.id === 'jellyfin.session.device_1');
  expect(sessionDevice?.name).toEqual('Living Room Browser');
  expect(sessionDevice?.features.some((featureArg) => featureArg.id === 'remote_command')).toBeTrue();
  expect(sessionDevice?.state.some((stateArg) => stateArg.featureId === 'current_title' && stateArg.value === 'Example Movie')).toBeTrue();
});

tap.test('maps active Jellyfin sessions to media player entities', async () => {
  const entities = JellyfinMapper.toEntities(snapshot);
  const player = entities.find((entityArg) => entityArg.platform === 'media_player');
  expect(player?.id).toEqual('media_player.living_room_browser');
  expect(player?.state).toEqual('playing');
  expect(player?.attributes?.volumeLevel).toEqual(0.55);
  expect(player?.attributes?.mediaContentType).toEqual('movie');
  expect(player?.attributes?.mediaDuration).toEqual(7200);
  expect(player?.attributes?.mediaPosition).toEqual(180);
});

export default tap.start();
