import { expect, tap } from '@git.zone/tstest/tapbundle';
import { KodiMapper, type IKodiSnapshot } from '../../ts/integrations/kodi/index.js';

const snapshot: IKodiSnapshot = {
  deviceInfo: {
    uuid: 'kodi-uuid-123',
    name: 'Living Room Kodi',
    host: '192.168.1.70',
    port: 8080,
    manufacturer: 'Kodi',
    version: '21.0',
  },
  application: {
    name: 'Kodi',
    volume: 42,
    muted: false,
    version: { major: 21, minor: 0 },
  },
  players: [{ playerid: 1, type: 'video', playertype: 'internal' }],
  player: { playerid: 1, type: 'video', playertype: 'internal' },
  playerProperties: {
    speed: 1,
    time: { hours: 0, minutes: 3, seconds: 4 },
    totaltime: { hours: 1, minutes: 2, seconds: 3 },
    live: false,
  },
  item: {
    id: 77,
    type: 'movie',
    title: 'The Test Movie',
    file: 'smb://media/test.mkv',
    thumbnail: 'image://poster.jpg/',
    streamdetails: { video: [{ hdrtype: 'hdr10' }] },
  },
  online: true,
};

tap.test('maps Kodi JSON-RPC snapshots to media devices', async () => {
  const devices = KodiMapper.toDevices(snapshot);
  expect(devices[0].id).toEqual('kodi.device.kodi_uuid_123');
  expect(devices[0].protocol).toEqual('http');
  expect(devices[0].state.some((stateArg) => stateArg.featureId === 'volume' && stateArg.value === 42)).toBeTrue();
  expect(devices[0].state.some((stateArg) => stateArg.featureId === 'current_title' && stateArg.value === 'The Test Movie')).toBeTrue();
});

tap.test('maps Kodi JSON-RPC snapshots to media player entities', async () => {
  const entities = KodiMapper.toEntities(snapshot);
  expect(entities[0].id).toEqual('media_player.living_room_kodi');
  expect(entities[0].platform).toEqual('media_player');
  expect(entities[0].state).toEqual('playing');
  expect(entities[0].attributes?.volumeLevel).toEqual(0.42);
  expect(entities[0].attributes?.mediaContentType).toEqual('movie');
  expect(entities[0].attributes?.mediaDuration).toEqual(3723);
  expect(entities[0].attributes?.mediaPosition).toEqual(184);
  expect(entities[0].attributes?.dynamicRange).toEqual('hdr10');
});

export default tap.start();
