import { expect, tap } from '@git.zone/tstest/tapbundle';
import { RainbirdMapper, type IRainbirdSnapshot } from '../../ts/integrations/rainbird/index.js';

const snapshot: IRainbirdSnapshot = {
  controller: {
    id: 'aabbcc123456',
    name: 'Backyard Controller',
    manufacturer: 'Rain Bird',
    modelName: 'ESP-TM2',
    macAddress: 'aabbcc123456',
    rainSensorActive: false,
    rainDelayDays: 2,
    host: '192.168.1.40',
  },
  zones: [
    { id: 1, name: 'Front Lawn', active: true, defaultDurationMinutes: 10 },
    { id: 2, name: 'Back Beds', active: false, defaultDurationMinutes: 8 },
  ],
  programs: [{
    id: 0,
    name: 'PGM A',
    enabled: true,
    starts: ['06:00'],
    frequency: 'custom',
    zoneDurations: [{ zoneId: 1, durationMinutes: 10 }, { zoneId: 2, durationMinutes: 8 }],
  }],
  events: [],
  connected: true,
  updatedAt: '2026-01-01T00:00:00.000Z',
};

tap.test('maps Rain Bird zones and programs to canonical devices and entities', async () => {
  const devices = RainbirdMapper.toDevices(snapshot);
  const entities = RainbirdMapper.toEntities(snapshot);
  expect(devices.some((deviceArg) => deviceArg.id === 'rainbird.controller.aabbcc123456')).toBeTrue();
  expect(devices.some((deviceArg) => deviceArg.id === 'rainbird.zone.aabbcc123456.1')).toBeTrue();
  expect(devices.some((deviceArg) => deviceArg.id === 'rainbird.program.aabbcc123456.0')).toBeTrue();
  expect(entities.some((entityArg) => entityArg.id === 'switch.front_lawn' && entityArg.state === 'on')).toBeTrue();
  expect(entities.some((entityArg) => entityArg.id === 'sensor.pgm_a')).toBeTrue();
  expect(entities.some((entityArg) => entityArg.id === 'sensor.raindelay' && entityArg.state === 2)).toBeTrue();
});

tap.test('maps Rain Bird services to controller commands', async () => {
  const startCommand = RainbirdMapper.commandForService(snapshot, {
    domain: 'rainbird',
    service: 'start_zone',
    target: {},
    data: { zoneId: 2, duration: 7 },
  });
  expect(startCommand).toEqual({ type: 'start_zone', zoneId: 2, durationMinutes: 7, entityId: undefined, deviceId: undefined });

  const switchCommand = RainbirdMapper.commandForService(snapshot, {
    domain: 'switch',
    service: 'turn_off',
    target: { entityId: 'switch.front_lawn' },
  });
  expect(switchCommand).toEqual({ type: 'stop_zone', zoneId: 1, entityId: 'switch.front_lawn', deviceId: undefined });

  const rainDelayCommand = RainbirdMapper.commandForService(snapshot, {
    domain: 'rainbird',
    service: 'set_rain_delay',
    target: {},
    data: { duration: 4 },
  });
  expect(rainDelayCommand).toEqual({ type: 'set_rain_delay', days: 4, entityId: undefined, deviceId: undefined });
});

export default tap.start();
