import * as crypto from 'node:crypto';
import * as net from 'node:net';
import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HomeAssistantMycroftIntegration, MycroftClient, MycroftConfigFlow, MycroftIntegration, MycroftMapper, createMycroftDiscoveryDescriptor, mycroftProfile, type IMycroftSnapshot, type TMycroftRawData } from '../../ts/integrations/mycroft/index.js';

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

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

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

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

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

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

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

tap.test('exposes Mycroft runtime, HA alias, and unsupported control without executor', async () => {
  const integration = new MycroftIntegration();
  const alias = new HomeAssistantMycroftIntegration();
  expect(alias instanceof MycroftIntegration).toBeTrue();
  expect(alias.domain).toEqual("mycroft");
  expect(integration.status).toEqual("control-runtime");
  expect(mycroftProfile.metadata.configFlow).toEqual(false);
  expect(mycroftProfile.metadata.requirements).toEqual([
    "mycroftapi==2.0",
  ]);

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

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

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

tap.test('checks and sends Mycroft speak messages through local websocket endpoint', async () => {
  const messages: string[] = [];
  const server = net.createServer((socket) => {
    let handshaken = false;
    socket.on('data', (chunkArg) => {
      if (!handshaken) {
        const text = chunkArg.toString('utf8');
        const key = /sec-websocket-key:\s*(.+)/i.exec(text)?.[1]?.trim() || '';
        const accept = crypto.createHash('sha1').update(`${key}258EAFA5-E914-47DA-95CA-C5AB0DC85B11`).digest('base64');
        socket.write([
          'HTTP/1.1 101 Switching Protocols',
          'Upgrade: websocket',
          'Connection: Upgrade',
          `Sec-WebSocket-Accept: ${accept}`,
          '\r\n',
        ].join('\r\n'));
        handshaken = true;
        return;
      }
      const decoded = decodeWebSocketTextFrame(Buffer.isBuffer(chunkArg) ? chunkArg : Buffer.from(chunkArg));
      if (decoded) {
        messages.push(decoded);
      }
    });
  });

  await listen(server);
  try {
    const address = server.address();
    const port = typeof address === 'object' && address ? address.port : 0;
    const client = new MycroftClient({ host: '127.0.0.1', port, timeoutMs: 1000 });
    const snapshot = await client.getSnapshot(true);
    const command = await client.execute({ domain: 'notify', service: 'send_message', target: {}, data: { message: 'Hello Mycroft' } });
    await sleep(20);

    expect(snapshot.online).toBeTrue();
    expect(snapshot.source).toEqual('tcp');
    expect(command.success).toBeTrue();
    expect(messages.length).toEqual(1);
    expect(JSON.parse(messages[0]).type).toEqual('speak');
    expect(JSON.parse(messages[0]).data.utterance).toEqual('Hello Mycroft');
  } 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));

function decodeWebSocketTextFrame(frameArg: Buffer): string | undefined {
  const opcode = frameArg[0] & 0x0f;
  if (opcode !== 1) {
    return undefined;
  }
  let offset = 2;
  let length = frameArg[1] & 0x7f;
  if (length === 126) {
    length = frameArg.readUInt16BE(offset);
    offset += 2;
  }
  const mask = frameArg.subarray(offset, offset + 4);
  offset += 4;
  const payload = frameArg.subarray(offset, offset + length);
  return Buffer.from(payload.map((byteArg, indexArg) => byteArg ^ mask[indexArg % 4])).toString('utf8');
}

export default tap.start();
