import { expect, tap } from '@git.zone/tstest/tapbundle';
import { MpdMapper, type IMpdSnapshot } from '../../ts/integrations/mpd/index.js';

const snapshot: IMpdSnapshot = {
  server: {
    id: 'mpd-living-room',
    host: '192.168.1.50',
    port: 6600,
    name: 'Living Room MPD',
    protocolVersion: '0.24.0',
  },
  status: {
    volume: '42',
    repeat: '1',
    single: '0',
    random: '1',
    playlist: '12',
    playlistlength: '7',
    state: 'play',
    songid: '23',
    elapsed: '31.2',
    duration: '180.5',
    audio: '44100:16:2',
    bitrate: '320',
    lastloadedplaylist: 'Favorites',
  },
  currentSong: {
    file: 'artist/album/example.flac',
    artist: ['Example Artist', 'Guest Artist'],
    album: 'Example Album',
    title: 'Example Track',
    time: '180',
    id: '23',
  },
  outputs: [{
    outputid: 0,
    outputname: 'Main ALSA',
    plugin: 'alsa',
    outputenabled: true,
    attributes: { dop: '0' },
  }, {
    outputid: 1,
    outputname: 'Headphones',
    plugin: 'pulse',
    outputenabled: false,
  }],
  commands: ['status', 'currentsong', 'play', 'pause', 'setvol', 'outputs'],
  playlists: [{ playlist: 'Favorites' }, { playlist: 'Radio' }],
  stats: { songs: '2000', uptime: '3600' },
  online: true,
  updatedAt: '2026-01-01T00:00:00.000Z',
  source: 'manual',
};

tap.test('maps MPD server and outputs to canonical devices', async () => {
  const devices = MpdMapper.toDevices(snapshot);
  expect(devices.some((deviceArg) => deviceArg.id === 'mpd.server.mpd_living_room')).toBeTrue();
  expect(devices.some((deviceArg) => deviceArg.id === 'mpd.output.mpd_living_room.0')).toBeTrue();
});

tap.test('maps MPD status current song and outputs to entities', async () => {
  const entities = MpdMapper.toEntities(snapshot);
  const player = entities.find((entityArg) => entityArg.id === 'media_player.living_room_mpd');
  const status = entities.find((entityArg) => entityArg.id === 'sensor.living_room_mpd_mpd_status');
  const output = entities.find((entityArg) => entityArg.id === 'switch.living_room_mpd_main_alsa_mpd_output');

  expect(player?.platform).toEqual('media_player');
  expect(player?.state).toEqual('playing');
  expect(player?.attributes?.volumeLevel).toEqual(0.42);
  expect(player?.attributes?.mediaTitle).toEqual('Example Track');
  expect(player?.attributes?.mediaArtist).toEqual('Example Artist, Guest Artist');
  expect(player?.attributes?.sourceList).toEqual(['Favorites', 'Radio']);
  expect(status?.state).toEqual('play');
  expect(output?.platform).toEqual('switch');
  expect(output?.state).toEqual('on');
  expect(output?.attributes?.mpdOutputId).toEqual(0);
});

export default tap.start();
