import * as net from 'node:net';
import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HomeAssistantMysensorsIntegration, MysensorsClient, MysensorsConfigFlow, MysensorsIntegration, MysensorsMapper, createMysensorsDiscoveryDescriptor, mysensorsProfile, type IMysensorsSnapshot, type TMysensorsRawData } from '../../ts/integrations/mysensors/index.js';

const rawData: TMysensorsRawData = {
  device: {
    id: 'mysensors-device-1',
    name: "MySensors Device",
    manufacturer: "MySensors",
    model: "MySensors local integration",
    serialNumber: 'mysensors-serial-1',
  },
  entities: [
    { id: 'status', name: 'Status', platform: "binary_sensor", state: true, attributes: { domain: "mysensors" } },
  ],
  online: true,
  updatedAt: '2026-01-01T00:00:00.000Z',
  source: 'manual',
};

tap.test('matches manual MySensors candidates and creates config flow output', async () => {
  const descriptor = createMysensorsDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'mysensors-manual-match');
  const result = await matcher!.matches({ source: 'manual', id: 'mysensors-device-1', name: "MySensors Device", metadata: { rawData } }, {});

  expect(result.matched).toBeTrue();
  expect(result.candidate?.integrationDomain).toEqual("mysensors");

  const validation = await descriptor.getValidators()[0].validate(result.candidate!, {});
  expect(validation.matched).toBeTrue();

  const done = await (await new MysensorsConfigFlow().start(result.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.uniqueId).toEqual('mysensors-device-1');
  expect(done.config?.rawData).toEqual(rawData);
});

tap.test('maps MySensors raw snapshots to runtime devices and entities', async () => {
  const client = new MysensorsClient({ name: "MySensors Runtime", rawData });
  const snapshot = await client.getSnapshot();
  const mappedSnapshot = MysensorsMapper.toSnapshotFromRaw({ name: "MySensors Runtime" }, rawData);
  const devices = MysensorsMapper.toDevices(mappedSnapshot);
  const entities = MysensorsMapper.toEntities(mappedSnapshot);

  expect(snapshot.online).toBeTrue();
  expect(mappedSnapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual("mysensors");
  expect(devices[0].manufacturer).toEqual("MySensors");
  expect(entities.some((entityArg) => entityArg.integrationDomain === "mysensors" && entityArg.platform === "binary_sensor")).toBeTrue();
});

tap.test('exposes MySensors runtime, HA alias, and unsupported control without executor', async () => {
  const integration = new MysensorsIntegration();
  const alias = new HomeAssistantMysensorsIntegration();
  expect(alias instanceof MysensorsIntegration).toBeTrue();
  expect(alias.domain).toEqual("mysensors");
  expect(integration.status).toEqual("control-runtime");
  expect(mysensorsProfile.metadata.configFlow).toEqual(true);
  expect(mysensorsProfile.metadata.requirements).toEqual([
    "pymysensors==0.26.0",
  ]);

  const runtime = await integration.setup({ name: "MySensors Runtime", rawData }, {});
  const statusResult = await runtime.callService!({ domain: "mysensors", service: 'status', target: {} });
  const refresh = await runtime.callService!({ domain: "mysensors", service: 'refresh', target: {} });
  const snapshot = statusResult.data as IMysensorsSnapshot;

  expect(statusResult.success).toBeTrue();
  expect(refresh.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual("MySensors Device");

  const command = await runtime.callService!({ domain: "mysensors", service: mysensorsProfile.controlServices?.[0] || 'turn_on', target: {} });
  expect(command.success).toBeFalse();
  expect(command.error!).toContain('requires config.host/config.url');
  await runtime.destroy();
});

tap.test('collects and controls MySensors TCP gateway serial API lines', async () => {
  const received: string[] = [];
  const server = net.createServer((socket) => {
    socket.write('12;6;0;0;3;Relay\n12;6;1;0;2;1\n');
    socket.on('data', (chunkArg) => received.push(chunkArg.toString('utf8')));
  });

  await listen(server);
  try {
    const address = server.address();
    const port = typeof address === 'object' && address ? address.port : 0;
    const client = new MysensorsClient({ host: '127.0.0.1', port, collectMs: 50, timeoutMs: 1000 });
    const snapshot = await client.getSnapshot(true);
    const command = await client.execute({ domain: 'switch', service: 'turn_off', target: {}, data: { nodeId: 12, childId: 6 } });
    await sleep(20);

    expect(snapshot.online).toBeTrue();
    expect(snapshot.source).toEqual('tcp');
    expect(snapshot.entities.some((entityArg) => entityArg.platform === 'switch' && entityArg.state === true && entityArg.attributes?.nodeId === 12)).toBeTrue();
    expect(command.success).toBeTrue();
    expect(received.join('').includes('0;255;3;0;2;')).toBeTrue();
    expect(received.join('').includes('12;6;1;0;2;0')).toBeTrue();
  } finally {
    await close(server);
  }
});

const listen = (serverArg: net.Server): Promise<void> => new Promise((resolve) => serverArg.listen(0, '127.0.0.1', () => resolve()));
const close = (serverArg: net.Server): Promise<void> => new Promise((resolve, reject) => serverArg.close((errorArg) => errorArg ? reject(errorArg) : resolve()));
const sleep = (msArg: number): Promise<void> => new Promise((resolve) => setTimeout(resolve, msArg));

export default tap.start();
