import { createServer, type IncomingMessage, type ServerResponse } from 'node:http';
import { expect, tap } from '@git.zone/tstest/tapbundle';
import { IncomfortClient, IncomfortConfigFlow, IncomfortIntegration, IncomfortMapper, createIncomfortDiscoveryDescriptor, incomfortProfile, type IIncomfortSnapshot, type TIncomfortRawData } from '../../ts/integrations/incomfort/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 heaterPayload = {
  displ_code: 126,
  IO: 0x02,
  ch_temp_msb: 12,
  ch_temp_lsb: 48,
  tap_temp_msb: 11,
  tap_temp_lsb: 27,
  ch_pressure_msb: 0,
  ch_pressure_lsb: 123,
  room_temp_1_msb: 8,
  room_temp_1_lsb: 64,
  room_temp_set_1_msb: 7,
  room_temp_set_1_lsb: 158,
  room_set_ovr_1_msb: 7,
  room_set_ovr_1_lsb: 208,
  room_temp_2_msb: 127,
  room_temp_2_lsb: 255,
  rf_message_rssi: 38,
  rfstatus_cntr: 0,
  nodenr: 200,
};

const startIncomfortServer = async (): Promise<{ url: string; setpointCalls: string[]; close(): Promise<void> }> => {
  const setpointCalls: string[] = [];
  const server = createServer((requestArg: IncomingMessage, responseArg: ServerResponse) => {
    const url = new URL(requestArg.url || '/', 'http://127.0.0.1');

    if (url.pathname === '/heaterlist.json') {
      json(responseArg, { heaterlist: ['175t23072', '000W00000', null] });
      return;
    }
    if (url.pathname === '/data.json' && url.searchParams.get('heater') === '0') {
      if (url.searchParams.has('setpoint')) {
        setpointCalls.push(url.search);
      }
      json(responseArg, heaterPayload);
      return;
    }

    json(responseArg, {}, 404);
  });

  await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
  const address = server.address();
  const port = typeof address === 'object' && address ? address.port : 0;
  return {
    url: `http://127.0.0.1:${port}`,
    setpointCalls,
    close: async () => new Promise<void>((resolve, reject) => server.close((errorArg) => errorArg ? reject(errorArg) : resolve())),
  };
};

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

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

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

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

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

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

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

tap.test('reads InComfort gateway snapshots and writes thermostat setpoints over local HTTP', async () => {
  const server = await startIncomfortServer();
  try {
    const endpoint = new URL(server.url);
    const client = new IncomfortClient({ host: endpoint.hostname, port: Number(endpoint.port), timeoutMs: 1000 });
    const snapshot = await client.getSnapshot(true);
    const entities = IncomfortMapper.toEntities(snapshot);
    const climate = entities.find((entityArg) => entityArg.platform === 'climate');

    expect(snapshot.online).toBeTrue();
    expect(snapshot.source).toEqual('http');
    expect(snapshot.device.manufacturer).toEqual('Intergas');
    expect(entities.find((entityArg) => entityArg.id === 'sensor.intergas_gateway_heater_0_cv_pressure')?.state).toEqual(1.23);
    expect(climate?.attributes?.heaterIndex).toEqual(0);
    expect(climate?.attributes?.roomNumber).toEqual(1);

    const runtime = await new IncomfortIntegration().setup({ host: endpoint.hostname, port: Number(endpoint.port), timeoutMs: 1000 }, {});
    const result = await runtime.callService?.({ domain: 'climate', service: 'set_temperature', target: { entityId: climate?.id }, data: { temperature: 19.5 } });

    expect(result?.success).toBeTrue();
    expect(server.setpointCalls[0]).toContain('heater=0');
    expect(server.setpointCalls[0]).toContain('thermostat=0');
    expect(server.setpointCalls[0]).toContain('setpoint=145');
    await runtime.destroy();
  } finally {
    await server.close();
  }
});

tap.test('exposes Intergas gateway runtime and unsupported control without executor', async () => {
  const integration = new IncomfortIntegration();
  expect(integration.status).toEqual("control-runtime");
  expect(incomfortProfile.metadata.configFlow).toEqual(true);
  expect(incomfortProfile.metadata.requirements).toEqual([
    "incomfort-client==0.7.0",
  ]);

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

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

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

export default tap.start();
