import { expect, tap } from '@git.zone/tstest/tapbundle';
import { ArcamFmjMapper, type IArcamFmjSnapshot } from '../../ts/integrations/arcam_fmj/index.js';

const snapshot: IArcamFmjSnapshot = {
  deviceInfo: {
    host: '192.168.1.60',
    port: 50000,
    name: 'Living Room Arcam',
    manufacturer: 'Arcam',
    model: 'AVR20',
    revision: '1.2.3',
    uniqueId: 'arcam-abc123',
    apiModel: 'APIHDA_SERIES',
  },
  zones: [{
    zone: 1,
    name: 'Main Zone',
    power: true,
    volume: 50,
    muted: false,
    source: 'BD',
    sourceList: ['BD', 'SAT', 'FM', 'DAB', 'NET'],
    soundMode: 'DOLBY_SURROUND',
    media: {
      title: 'Blu-ray',
      contentType: 'video',
    },
    available: true,
  }, {
    zone: 2,
    name: 'Patio',
    power: false,
    volumeLevel: 0.25,
    muted: true,
    source: 'FM',
    media: {
      title: 'FM - Radio One',
      channel: 'Radio One',
      contentType: 'music',
      contentId: 'preset:4',
    },
    available: true,
  }],
  online: true,
  source: 'snapshot',
  lastUpdated: '2026-01-01T00:00:00.000Z',
};

tap.test('maps Arcam FMJ receiver zones to canonical devices', async () => {
  const devices = ArcamFmjMapper.toDevices(snapshot);
  expect(devices.length).toEqual(2);
  expect(devices[0].id).toEqual('arcam_fmj.receiver.arcam_abc123');
  expect(devices[0].manufacturer).toEqual('Arcam');
  expect(devices[0].state.some((stateArg) => stateArg.featureId === 'source' && stateArg.value === 'BD')).toBeTrue();
  expect(devices[1].metadata?.viaDeviceId).toEqual('arcam_fmj.receiver.arcam_abc123');
  expect(devices[1].state.some((stateArg) => stateArg.featureId === 'power' && stateArg.value === 'off')).toBeTrue();
});

tap.test('maps Arcam FMJ zones to media player entities', async () => {
  const entities = ArcamFmjMapper.toEntities(snapshot);
  const main = entities.find((entityArg) => entityArg.id === 'media_player.living_room_arcam');
  const zone2 = entities.find((entityArg) => entityArg.id === 'media_player.living_room_arcam_zone_2');

  expect(main?.platform).toEqual('media_player');
  expect(main?.state).toEqual('on');
  expect(main?.attributes?.volumeLevel).toEqual(50 / 99);
  expect(main?.attributes?.source).toEqual('BD');
  expect(main?.attributes?.soundMode).toEqual('DOLBY_SURROUND');
  expect(zone2?.state).toEqual('off');
  expect(zone2?.attributes?.mediaChannel).toEqual('Radio One');
  expect(zone2?.attributes?.mediaContentId).toEqual('preset:4');
});

export default tap.start();
