import { expect, tap } from '@git.zone/tstest/tapbundle';
import { DunehdMapper, type IDunehdSnapshot } from '../../ts/integrations/dunehd/index.js';

const snapshot: IDunehdSnapshot = {
  deviceInfo: {
    id: 'dune-123',
    host: '192.168.1.70',
    name: 'Living Room Dune',
    manufacturer: 'Dune',
    model: 'Dune HD Pro 4K',
  },
  state: {
    player_state: 'playing',
    playback_speed: '256',
    playback_volume: '45',
    playback_mute: '0',
    playback_url: 'smb://media.example/movies/Movie.mkv',
    playback_position: '120',
  },
  online: true,
  updatedAt: '2026-05-07T00:00:00.000Z',
};

tap.test('maps Dune HD snapshots to media devices and entities', async () => {
  const devices = DunehdMapper.toDevices(snapshot);
  const entities = DunehdMapper.toEntities(snapshot);

  expect(devices[0].id).toEqual('dunehd.device.dune_123');
  expect(devices[0].state.some((stateArg) => stateArg.featureId === 'playback' && stateArg.value === 'playing')).toBeTrue();
  expect(devices[0].state.some((stateArg) => stateArg.featureId === 'volume' && stateArg.value === 45)).toBeTrue();
  expect(entities[0].id).toEqual('media_player.living_room_dune');
  expect(entities[0].platform).toEqual('media_player');
  expect(entities[0].state).toEqual('playing');
  expect(entities[0].attributes?.mediaTitle).toEqual('Movie.mkv');
  expect(entities[0].attributes?.volumeLevel).toEqual(0.45);
});

tap.test('preserves Home Assistant Dune HD state precedence', async () => {
  expect(DunehdMapper.mediaState({ player_state: 'navigator', playback_speed: '0' })).toEqual('on');
  expect(DunehdMapper.mediaState({ player_state: 'photo_viewer', playback_speed: '0' })).toEqual('paused');
  expect(DunehdMapper.mediaTitle({ player_state: 'bluray_playback' })).toEqual('Blu-Ray');
  expect(DunehdMapper.mediaTitle({ player_state: 'photo_viewer' })).toEqual('Photo Viewer');
});

export default tap.start();
