import { expect, tap } from '@git.zone/tstest/tapbundle';
import { FullyKioskMapper, type IFullyKioskSnapshot } from '../../ts/integrations/fully_kiosk/index.js';

const snapshot: IFullyKioskSnapshot = {
  online: true,
  updatedAt: '2026-01-01T00:00:00.000Z',
  source: 'snapshot',
  host: '192.168.1.50',
  port: 2323,
  protocol: 'http',
  ssl: false,
  uniqueId: 'device-123',
  macAddress: 'aa:bb:cc:dd:ee:ff',
  name: 'Wall Tablet',
  deviceInfo: {
    deviceID: 'device-123',
    deviceName: 'Wall Tablet',
    deviceManufacturer: 'Samsung',
    deviceModel: 'SM-T500',
    appVersionName: '1.60',
    Mac: 'aa:bb:cc:dd:ee:ff',
    ip4: '192.168.1.50',
    batteryLevel: 87,
    batteryTemperature: 31.4,
    currentPage: 'https://example.test/dashboard',
    screenOn: true,
    isInScreensaver: false,
    kioskLocked: true,
    plugged: true,
    soundUrlPlaying: 'https://example.test/sound.mp3',
    internalStorageFreeSpace: 123456789,
  },
  settings: {
    motionDetection: true,
    screenBrightness: '120',
    timeToScreensaverV2: '60',
  },
};

tap.test('maps Fully Kiosk snapshot to devices and entities', async () => {
  const devices = FullyKioskMapper.toDevices(snapshot);
  const entities = FullyKioskMapper.toEntities(snapshot);

  expect(devices[0].id).toEqual('fully_kiosk.device.device_123');
  expect(devices[0].online).toBeTrue();
  expect(devices[0].metadata?.appVersionName).toEqual('1.60');
  expect(entities.find((entityArg) => entityArg.id === 'sensor.wall_tablet_batterylevel')?.state).toEqual(87);
  expect(entities.find((entityArg) => entityArg.id === 'switch.wall_tablet_motion_detection')?.state).toEqual('on');
  expect(entities.find((entityArg) => entityArg.id === 'number.wall_tablet_screenbrightness')?.state).toEqual(120);
  expect(entities.find((entityArg) => entityArg.id === 'camera.wall_tablet_screenshot')?.attributes?.imageKind).toEqual('screenshot');
  expect(entities.find((entityArg) => entityArg.id === 'media_player.wall_tablet_mediaplayer')?.state).toEqual('playing');
});

tap.test('models Fully Kiosk control commands from services and entities', async () => {
  const switchCommand = FullyKioskMapper.commandForService(snapshot, {
    domain: 'switch',
    service: 'turn_off',
    target: { entityId: 'switch.wall_tablet_motion_detection' },
  });
  const loadUrlCommand = FullyKioskMapper.commandForService(snapshot, {
    domain: 'fully_kiosk',
    service: 'load_url',
    target: {},
    data: { url: 'https://example.test/next' },
  });
  const setConfigCommand = FullyKioskMapper.commandForService(snapshot, {
    domain: 'fully_kiosk',
    service: 'set_config',
    target: {},
    data: { key: 'motionDetection', value: 'false' },
  });
  const mediaCommand = FullyKioskMapper.commandForService(snapshot, {
    domain: 'media_player',
    service: 'play_media',
    target: { entityId: 'media_player.wall_tablet_mediaplayer' },
    data: { media_content_id: 'https://example.test/movie.mp4', media_content_type: 'video/mp4' },
  });

  expect(Boolean(switchCommand && !('error' in switchCommand))).toBeTrue();
  if (switchCommand && !('error' in switchCommand)) {
    expect(switchCommand.type).toEqual('set_boolean_setting');
    expect(switchCommand.settingKey).toEqual('motionDetection');
    expect(switchCommand.value).toBeFalse();
  }
  expect(Boolean(loadUrlCommand && !('error' in loadUrlCommand))).toBeTrue();
  if (loadUrlCommand && !('error' in loadUrlCommand)) {
    expect(loadUrlCommand.cmd).toEqual('loadUrl');
    expect(loadUrlCommand.params?.url).toEqual('https://example.test/next');
  }
  expect(Boolean(setConfigCommand && !('error' in setConfigCommand))).toBeTrue();
  if (setConfigCommand && !('error' in setConfigCommand)) {
    expect(setConfigCommand.type).toEqual('set_boolean_setting');
    expect(setConfigCommand.value).toBeFalse();
  }
  expect(Boolean(mediaCommand && !('error' in mediaCommand))).toBeTrue();
  if (mediaCommand && !('error' in mediaCommand)) {
    expect(mediaCommand.cmd).toEqual('playVideo');
  }
});

export default tap.start();
