import { createHash } from 'node:crypto';
import { createServer, type IncomingMessage, type ServerResponse } from 'node:http';
import { expect, tap } from '@git.zone/tstest/tapbundle';
import { IntellifireClient, IntellifireConfigFlow, IntellifireIntegration, IntellifireMapper, createIntellifireDiscoveryDescriptor, intellifireProfile, type IIntellifireSnapshot, type TIntellifireRawData } from '../../ts/integrations/intellifire/index.js';

const json = (responseArg: ServerResponse, valueArg: unknown, statusArg = 200): void => {
  responseArg.statusCode = statusArg;
  responseArg.setHeader('content-type', 'application/json');
  responseArg.end(JSON.stringify(valueArg));
};

const readBody = async (requestArg: IncomingMessage): Promise<string> => {
  const chunks: Buffer[] = [];
  for await (const chunk of requestArg) {
    chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
  }
  return Buffer.concat(chunks).toString('utf8');
};

const intellifireResponse = (apiKeyArg: string, challengeArg: string, commandArg: string, valueArg: number): string => {
  const apiBytes = Buffer.from(apiKeyArg, 'hex');
  const challengeBytes = Buffer.from(challengeArg, 'hex');
  const payloadBytes = Buffer.from(`post:command=${commandArg}&value=${valueArg}`, 'utf8');
  const inner = createHash('sha256').update(Buffer.concat([apiBytes, challengeBytes, payloadBytes])).digest();
  return createHash('sha256').update(Buffer.concat([apiBytes, inner])).digest('hex');
};

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

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

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

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

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

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

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

tap.test('reads IntelliFire /poll over local HTTP and signs local /post controls', async () => {
  const apiKey = '00112233445566778899aabbccddeeff';
  const challenge = 'abcdef0123456789';
  const bodies: string[] = [];
  const server = createServer((requestArg: IncomingMessage, responseArg: ServerResponse) => {
    void (async () => {
      const url = new URL(requestArg.url || '/', 'http://127.0.0.1');
      if (url.pathname === '/poll') {
        json(responseArg, {
          serial: 'IFT123456',
          name: 'Living Room Fireplace',
          power: 1,
          pilot: 1,
          timer: 0,
          thermostat: 1,
          temperature: 22,
          setpoint: 2100,
          height: 3,
          fanspeed: 2,
          light: 2,
          feature_fan: 1,
          feature_light: 1,
          feature_thermostat: 1,
          ipv4_address: '127.0.0.1',
          errors: [6],
          firmware_version: '0x01000000',
        });
        return;
      }
      if (url.pathname === '/get_challenge') {
        responseArg.statusCode = 200;
        responseArg.setHeader('content-type', 'text/plain');
        responseArg.end(challenge);
        return;
      }
      if (url.pathname === '/post' && requestArg.method === 'POST') {
        const body = await readBody(requestArg);
        bodies.push(body);
        const params = new URLSearchParams(body);
        expect(params.get('command')).toEqual('power');
        expect(params.get('value')).toEqual('0');
        expect(params.get('user')).toEqual('user-123');
        expect(params.get('response')).toEqual(intellifireResponse(apiKey, challenge, 'power', 0));
        json(responseArg, { ok: true });
        return;
      }
      json(responseArg, { error: 'not_found' }, 404);
    })().catch((errorArg) => {
      responseArg.statusCode = 500;
      responseArg.end(String(errorArg));
    });
  });

  await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
  try {
    const address = server.address();
    const port = typeof address === 'object' && address ? address.port : 0;
    const url = `http://127.0.0.1:${port}`;
    const client = new IntellifireClient({ url, userId: 'user-123', apiKey, timeoutMs: 1000 });
    const snapshot = await client.getSnapshot(true);
    const command = await client.execute({ domain: 'switch', service: 'turn_off', target: { entityId: 'switch.living_room_fireplace_flame' } });
    const runtime = await new IntellifireIntegration().setup({ url, timeoutMs: 1000 }, {});
    const entities = await runtime.entities();

    expect(snapshot.online).toBeTrue();
    expect(snapshot.source).toEqual('http');
    expect(snapshot.device.serialNumber).toEqual('IFT123456');
    expect(entities.some((entityArg) => entityArg.attributes?.key === 'fanspeed' && entityArg.platform === 'fan')).toBeTrue();
    expect(command.success).toBeTrue();
    expect(bodies.length).toEqual(1);
    await runtime.destroy();
  } finally {
    await new Promise<void>((resolve, reject) => server.close((errorArg) => errorArg ? reject(errorArg) : resolve()));
  }
});

tap.test('exposes IntelliFire runtime and unsupported control without executor', async () => {
  const integration = new IntellifireIntegration();
  expect(integration.status).toEqual("control-runtime");
  expect(intellifireProfile.metadata.configFlow).toEqual(true);
  expect(intellifireProfile.metadata.requirements).toEqual([
    "intellifire4py==4.4.0",
  ]);

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

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

  const command = await runtime.callService!({ domain: "switch", service: 'turn_on', target: { entityId: 'switch.intellifire_runtime_flame' } });
  expect(command.success).toBeFalse();
  expect(command.error!).toContain('requires config.host or config.url');
  await runtime.destroy();
});

export default tap.start();
