import { expect, tap } from '@git.zone/tstest/tapbundle';
import { SynologyDsmConfigFlow, createSynologyDsmDiscoveryDescriptor, type ISynologyDsmSnapshot } from '../../ts/integrations/synology_dsm/index.js';

const snapshot: ISynologyDsmSnapshot = {
  connected: true,
  system: {
    serial: 'SYN123',
    name: 'DiskStation',
    host: '192.168.1.20',
    port: 5001,
    ssl: true,
    model: 'DS920+',
  },
  utilization: {},
  storage: { volumes: [], disks: [] },
  network: {},
  cameras: [],
  switches: [],
  actions: [],
};

tap.test('matches and validates manual Synology DSM entries', async () => {
  const descriptor = createSynologyDsmDiscoveryDescriptor();
  const matcher = descriptor.getMatchers()[0];
  const result = await matcher.matches({ host: '192.168.1.20', name: 'NAS' }, {});

  expect(result.matched).toBeTrue();
  expect(result.candidate?.integrationDomain).toEqual('synology_dsm');
  expect(result.candidate?.port).toEqual(5001);

  const validation = await descriptor.getValidators()[0].validate(result.candidate!, {});
  expect(validation.matched).toBeTrue();
  expect(validation.normalizedDeviceId).toEqual('192.168.1.20:5001');
});

tap.test('matches Synology HTTP, SSDP, and mDNS local candidates', async () => {
  const descriptor = createSynologyDsmDiscoveryDescriptor();
  const httpResult = await descriptor.getMatchers()[1].matches({ url: 'https://diskstation.local:5001/webapi/query.cgi' }, {});
  const ssdpResult = await descriptor.getMatchers()[2].matches({
    location: 'http://192.168.1.20:5000/description.xml',
    manufacturer: 'Synology',
    upnp: {
      friendlyName: 'DiskStation (DS920+)',
      modelName: 'DS920+',
      manufacturer: 'Synology',
      serialNumber: 'AABBCCDDEEFF',
    },
  }, {});
  const mdnsResult = await descriptor.getMatchers()[3].matches({
    type: '_http._tcp.local.',
    name: 'DiskStation._http._tcp.local.',
    host: 'diskstation.local',
    properties: {
      vendor: 'synology',
      mac_address: 'AA:BB:CC:DD:EE:FF',
    },
  }, {});

  expect(httpResult.matched).toBeTrue();
  expect(httpResult.candidate?.host).toEqual('diskstation.local');
  expect(ssdpResult.matched).toBeTrue();
  expect(ssdpResult.normalizedDeviceId).toEqual('aa:bb:cc:dd:ee:ff');
  expect(mdnsResult.matched).toBeTrue();
  expect(mdnsResult.normalizedDeviceId).toEqual('aa:bb:cc:dd:ee:ff');
});

tap.test('creates config flow output from discovery and snapshot JSON', async () => {
  const step = await new SynologyDsmConfigFlow().start({ source: 'manual', host: '192.168.1.20', id: 'SYN123', name: 'DiskStation' }, {});
  const done = await step.submit!({ username: 'admin', password: 'secret', snapshotJson: JSON.stringify(snapshot), snapshotQuality: '2' });

  expect(done.kind).toEqual('done');
  expect(done.config?.host).toEqual('192.168.1.20');
  expect(done.config?.port).toEqual(5001);
  expect(done.config?.snapshot?.system.serial).toEqual('SYN123');
  expect(done.config?.snapshotQuality).toEqual(2);
});

tap.test('rejects candidates without Synology DSM hints or usable data', async () => {
  const descriptor = createSynologyDsmDiscoveryDescriptor();
  const httpResult = await descriptor.getMatchers()[1].matches({ url: 'http://example.local/status' }, {});
  expect(httpResult.matched).toBeFalse();

  const validation = await descriptor.getValidators()[0].validate({ source: 'manual', name: 'Synology DSM' }, {});
  expect(validation.matched).toBeFalse();
});

export default tap.start();
