import { expect, tap } from '@git.zone/tstest/tapbundle';
import { ChannelsMapper, type IChannelsSnapshot } from '../../ts/integrations/channels/index.js';

const snapshot: IChannelsSnapshot = {
  deviceInfo: {
    id: 'channels-living-room',
    name: 'Living Room Channels',
    host: '192.168.1.51',
    port: 57000,
    manufacturer: 'Fancy Bits',
    model: 'Channels',
  },
  status: {
    status: 'playing',
    muted: false,
    channel: {
      channel_number: '35.1',
      channel_name: 'Fox-HD',
      channel_image_url: 'http://images.example/channel.png',
    },
    now_playing: {
      title: 'The Mick',
      episode_title: 'The Accident',
      season_number: 2,
      episode_number: 16,
      image_url: 'http://images.example/show.png',
      summary: 'Episode summary',
    },
  },
  favoriteChannels: [
    { number: '12.1', name: 'NBC HD' },
    { number: '35.1', name: 'Fox-HD' },
  ],
  online: true,
  updatedAt: '2026-01-01T00:00:00.000Z',
};

tap.test('maps Channels snapshots to media devices', async () => {
  const devices = ChannelsMapper.toDevices(snapshot);
  expect(devices[0].id).toEqual('channels.device.channels_living_room');
  expect(devices[0].protocol).toEqual('http');
  expect(devices[0].state.some((stateArg) => stateArg.featureId === 'playback' && stateArg.value === 'playing')).toBeTrue();
  expect(devices[0].state.some((stateArg) => stateArg.featureId === 'channel' && stateArg.value === '35.1')).toBeTrue();
  expect(devices[0].state.some((stateArg) => stateArg.featureId === 'current_title' && stateArg.value === 'The Mick')).toBeTrue();
});

tap.test('maps Channels snapshots to media player entities', async () => {
  const entities = ChannelsMapper.toEntities(snapshot);
  expect(entities[0].id).toEqual('media_player.living_room_channels');
  expect(entities[0].platform).toEqual('media_player');
  expect(entities[0].state).toEqual('playing');
  expect(entities[0].attributes?.mediaTitle).toEqual('The Mick');
  expect(entities[0].attributes?.mediaEpisodeTitle).toEqual('The Accident');
  expect(entities[0].attributes?.mediaImageUrl).toEqual('http://images.example/show.png');
  expect(entities[0].attributes?.sourceList).toEqual(['NBC HD', 'Fox-HD']);
  expect(entities[0].attributes?.channelNumber).toEqual('35.1');
});

export default tap.start();
