import { expect, tap } from '@git.zone/tstest/tapbundle';
import * as plugins from '../../ts/plugins.js';
import { HomeAssistantLcnIntegration, LcnClient, LcnConfigFlow, LcnIntegration, LcnMapper, createLcnDiscoveryDescriptor, lcnProfile, type ILcnSnapshot, type TLcnRawData } from '../../ts/integrations/lcn/index.js';

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

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

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

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

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

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

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

tap.test('exposes LCN runtime, HA alias, and unsupported control without executor', async () => {
  const integration = new LcnIntegration();
  const alias = new HomeAssistantLcnIntegration();
  expect(alias instanceof LcnIntegration).toBeTrue();
  expect(alias.domain).toEqual("lcn");
  expect(integration.status).toEqual("control-runtime");
  expect(lcnProfile.metadata.configFlow).toEqual(true);
  expect(lcnProfile.metadata.requirements).toEqual([
    "pypck==0.9.11",
    "lcn-frontend==0.2.7",
  ]);

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

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

  const command = await runtime.callService!({ domain: "lcn", service: lcnProfile.controlServices?.[0] || 'turn_on', target: {} });
  expect(command.success).toBeFalse();
  expect(command.error!).toContain('requires an injected client.execute() or commandExecutor');
  await runtime.destroy();
});

tap.test('reads native LCN PCHK status over TCP', async () => {
  const server = await startPchkTestServer();
  try {
    const client = new LcnClient({ host: '127.0.0.1', port: server.port, username: 'lcn', password: 'secret', timeoutMs: 1000, name: 'LCN PCHK Test' });
    const snapshot = await client.getSnapshot(true);

    expect(snapshot.online).toBeTrue();
    expect(snapshot.source).toEqual('tcp');
    expect(snapshot.device.model).toEqual('LCN-PCHK');
    expect(snapshot.entities.some((entityArg) => entityArg.id === 'pchk_connection' && entityArg.state === true)).toBeTrue();
    expect(server.received.includes('lcn')).toBeTrue();
    expect(server.received.includes('secret')).toBeTrue();
    expect(server.received.includes('!CHD')).toBeTrue();
    expect(server.received.includes('!OM1P')).toBeTrue();
  } finally {
    await server.close();
  }
});

tap.test('sends native addressed LCN PCK command over TCP', async () => {
  const server = await startPchkTestServer();
  try {
    const client = new LcnClient({ host: '127.0.0.1', port: server.port, username: 'lcn', password: 'secret', timeoutMs: 1000, postCommandWaitMs: 1 });
    const result = await client.execute({ domain: 'lcn', service: 'pck', target: {}, data: { pck: 'A1DI050000', segmentId: 0, moduleId: 10 } });

    await waitForReceived(server, '>M000010.A1DI050000');
    expect(result.success).toBeTrue();
    expect(server.received.includes('>M000010.A1DI050000')).toBeTrue();
  } finally {
    await server.close();
  }
});

tap.test('uses native LCN client through integration runtime for host configs', async () => {
  const server = await startPchkTestServer();
  const runtime = await new LcnIntegration().setup({ host: '127.0.0.1', port: server.port, username: 'lcn', password: 'secret', timeoutMs: 1000 }, {});
  try {
    const status = await runtime.callService!({ domain: 'lcn', service: 'status', target: {} });
    const snapshot = status.data as ILcnSnapshot;

    expect(status.success).toBeTrue();
    expect(snapshot.online).toBeTrue();
    expect(snapshot.source).toEqual('tcp');
  } finally {
    await runtime.destroy();
    await server.close();
  }
});

interface IPchkTestServer {
  port: number;
  received: string[];
  close(): Promise<void>;
}

async function startPchkTestServer(): Promise<IPchkTestServer> {
  const received: string[] = [];
  const sockets = new Set<plugins.net.Socket>();
  const server = plugins.net.createServer((socket) => {
    sockets.add(socket);
    socket.once('close', () => sockets.delete(socket));
    socket.write('Username:\n');
    let buffer = '';
    socket.on('data', (chunkArg) => {
      buffer += chunkArg.toString('utf8');
      const lines = buffer.split('\n');
      buffer = lines.pop() || '';
      for (const rawLine of lines) {
        const line = rawLine.replace(/\r$/, '');
        if (!line) {
          continue;
        }
        received.push(line);
        if (line === 'lcn') {
          socket.write('Password:\n');
        } else if (line === 'secret') {
          socket.write('OK\n$io:#LCN:connected\n');
        } else if (line === '!CHD') {
          socket.write('(dec-mode)\n');
        }
      }
    });
  });

  await new Promise<void>((resolve, reject) => {
    server.once('error', reject);
    server.listen(0, '127.0.0.1', () => {
      server.off('error', reject);
      resolve();
    });
  });

  const address = server.address();
  if (!address || typeof address === 'string') {
    throw new Error('LCN test server did not bind to a TCP port.');
  }

  return {
    port: address.port,
    received,
    close: async () => {
      for (const socket of sockets) {
        socket.destroy();
      }
      await new Promise<void>((resolve) => server.close(() => resolve()));
    },
  };
}

async function waitForReceived(serverArg: IPchkTestServer, lineArg: string): Promise<void> {
  for (let index = 0; index < 20; index++) {
    if (serverArg.received.includes(lineArg)) {
      return;
    }
    await new Promise((resolve) => setTimeout(resolve, 10));
  }
}

export default tap.start();
