import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HomeAssistantSynologyDsmIntegration, type ISynologyDsmCommand, type ISynologyDsmConfig } from '../../ts/integrations/synology_dsm/index.js';

const config: ISynologyDsmConfig = {
  host: '192.168.1.20',
  snapshot: {
    connected: true,
    system: {
      serial: 'SYN123',
      name: 'DiskStation',
      host: '192.168.1.20',
      model: 'DS920+',
      versionString: 'DSM 7.2.2-72806',
    },
    utilization: {},
    storage: { volumes: [], disks: [] },
    network: {},
    cameras: [],
    switches: [],
    actions: [],
  },
};

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

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

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

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

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

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

export default tap.start();
