import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HomeAssistantSynologySrmIntegration, type ISynologySrmCommand, type ISynologySrmConfig } from '../../ts/integrations/synology_srm/index.js';

const config: ISynologySrmConfig = {
  host: '192.168.1.1',
  snapshot: {
    connected: true,
    router: {
      name: 'RT6600ax',
      host: '192.168.1.1',
      macAddress: 'AA:BB:CC:DD:EE:FF',
      actions: ['reboot'],
    },
    system: {},
    network: {},
    interfaces: [],
    clients: [],
    traffic: {},
    sensors: {},
    switches: [],
    actions: [],
  },
};

tap.test('does not fake Synology SRM command success without injected executor', async () => {
  const runtime = await new HomeAssistantSynologySrmIntegration().setup(config, {});
  const result = await runtime.callService!({ domain: 'synology_srm', service: 'reboot', target: {} });

  expect(result.success).toBeFalse();
  expect(result.error || '').toContain('not faked');
  await runtime.destroy();
});

tap.test('executes explicit Synology SRM commands through injected executor', async () => {
  let command: ISynologySrmCommand | undefined;
  const runtime = await new HomeAssistantSynologySrmIntegration().setup({
    ...config,
    commandExecutor: async (commandArg) => {
      command = commandArg;
      return { success: true, data: { accepted: true } };
    },
  }, {});
  const result = await runtime.callService!({ domain: 'synology_srm', service: 'reboot', target: {} });

  expect(result.success).toBeTrue();
  expect(command?.type).toEqual('router.action');
  expect(command?.action).toEqual('reboot');
  await runtime.destroy();
});

tap.test('reports offline refresh without snapshot, provider, or native client', async () => {
  const runtime = await new HomeAssistantSynologySrmIntegration().setup({ host: '192.168.1.1' }, {});
  const result = await runtime.callService!({ domain: 'synology_srm', service: 'refresh', target: {} });

  expect(result.success).toBeFalse();
  expect(result.error || '').toContain('nativeClient');
  await runtime.destroy();
});

export default tap.start();
