import { expect, tap } from '@git.zone/tstest/tapbundle';
import { DirectvIntegration, DirectvMapper, HomeAssistantDirectvIntegration, type IDirectvSnapshot } from '../../ts/integrations/directv/index.js';

const snapshot: IDirectvSnapshot = {
  online: true,
  source: 'snapshot',
  updatedAt: '2026-05-07T00:00:00.000Z',
  device: {
    brand: 'DirecTV',
    receiverId: 'RID001',
    version: '0x1234',
    host: '192.168.1.55',
    port: 8080,
    name: 'DirecTV Receiver',
  },
  locations: [
    { client: false, name: 'Living Room', address: '0' },
    { client: true, name: 'Bedroom', address: '1' },
  ],
  states: [
    {
      address: '0',
      available: true,
      authorized: true,
      standby: false,
      status: 'active',
      paused: false,
      positionUpdatedAt: '2026-05-07T00:00:00.000Z',
      program: {
        channel: '202',
        channelName: 'CNN',
        ondemand: false,
        recorded: false,
        recording: true,
        viewed: false,
        programId: 42,
        programType: 'tvshow',
        duration: 1800,
        title: 'News Hour',
        episodeTitle: 'Morning',
        partial: false,
        payperview: false,
        position: 120,
        purchased: false,
        rating: 'TV-G',
      },
    },
    {
      address: '1',
      available: true,
      authorized: true,
      standby: true,
      status: 'standby',
    },
  ],
};

tap.test('maps DirecTV snapshots to receiver and client devices', async () => {
  const devices = DirectvMapper.toDevices(snapshot);
  expect(devices.length).toEqual(2);
  expect(devices[0].id).toEqual('directv.device.rid001');
  expect(devices[0].online).toBeTrue();
  expect(devices[0].state.find((stateArg) => stateArg.featureId === 'channel')?.value).toEqual('202');
  expect(devices[1].id).toEqual('directv.device.1');
  expect(devices[1].state.find((stateArg) => stateArg.featureId === 'power')?.value).toEqual('off');
});

tap.test('maps DirecTV locations to media player entities', async () => {
  const entities = DirectvMapper.toEntities(snapshot);
  const livingRoom = entities.find((entityArg) => entityArg.uniqueId === 'directv_rid001_media_player');
  const bedroom = entities.find((entityArg) => entityArg.uniqueId === 'directv_1_media_player');

  expect(livingRoom?.state).toEqual('playing');
  expect(livingRoom?.attributes?.mediaTitle).toEqual('News Hour');
  expect(livingRoom?.attributes?.mediaChannel).toEqual('CNN (202)');
  expect(livingRoom?.attributes?.mediaCurrentlyRecording).toBeTrue();
  expect(bedroom?.state).toEqual('off');
});

tap.test('preserves the generated Home Assistant export as a native alias', async () => {
  const integration = new HomeAssistantDirectvIntegration();
  expect(integration).toBeInstanceOf(DirectvIntegration);
  expect(integration.domain).toEqual('directv');
  expect(integration.status).toEqual('control-runtime');
});

export default tap.start();
