import { createServer, type IncomingMessage, type ServerResponse } from 'node:http';
import { expect, tap } from '@git.zone/tstest/tapbundle';
import { IometerClient, IometerConfigFlow, IometerIntegration, IometerMapper, createIometerDiscoveryDescriptor, iometerProfile, type IIometerSnapshot, type TIometerRawData } from '../../ts/integrations/iometer/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 rawData: TIometerRawData = {
  device: {
    id: 'iometer-device-1',
    name: "IOmeter Device",
    manufacturer: "IOmeter",
    model: "IOmeter local integration",
    serialNumber: 'iometer-serial-1',
  },
  entities: [
    { id: 'status', name: 'Status', platform: "binary_sensor", state: true, attributes: { domain: "iometer" } },
  ],
  online: true,
  updatedAt: '2026-01-01T00:00:00.000Z',
  source: 'manual',
};

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

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

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

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

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

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

tap.test('reads IOmeter /v1/reading and /v1/status over local HTTP', async () => {
  const requests: Array<{ url?: string; userAgent?: string | string[] }> = [];
  const server = createServer((requestArg: IncomingMessage, responseArg: ServerResponse) => {
    const url = new URL(requestArg.url || '/', 'http://127.0.0.1');
    requests.push({ url: url.pathname, userAgent: requestArg.headers['user-agent'] });
    if (url.pathname === '/v1/reading') {
      json(responseArg, {
        meter: {
          number: 'METER123',
          reading: {
            time: '2026-01-01T00:00:00Z',
            registers: [
              { obis: '01-00:01.08.00*ff', value: 12345, unit: 'Wh' },
              { obis: '01-00:02.08.00*ff', value: 234, unit: 'Wh' },
              { obis: '01-00:10.07.00*ff', value: 321, unit: 'W' },
              { obis: '01-00:01.08.01*ff', value: 10000, unit: 'Wh' },
              { obis: '01-00:01.08.02*ff', value: 2345, unit: 'Wh' },
            ],
          },
        },
      });
      return;
    }
    if (url.pathname === '/v1/status') {
      json(responseArg, {
        meter: { number: 'METER123' },
        device: {
          id: 'bridge-abc',
          bridge: { rssi: -55, version: '1.2.3' },
          core: { connectionStatus: 'connected', rssi: -61, version: '2.3.4', powerStatus: 'wired', attachmentStatus: 'attached', pinStatus: 'entered', batteryLevel: 88 },
        },
      });
      return;
    }
    json(responseArg, { error: 'not_found' }, 404);
  });

  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 IometerClient({ url, timeoutMs: 1000 });
    const snapshot = await client.getSnapshot(true);
    const runtime = await new IometerIntegration().setup({ url, timeoutMs: 1000 }, {});
    const entities = await runtime.entities();

    expect(snapshot.online).toBeTrue();
    expect(snapshot.source).toEqual('http');
    expect(snapshot.device.serialNumber).toEqual('bridge-abc');
    expect(snapshot.entities.find((entityArg) => entityArg.id === 'power')?.state).toEqual(321);
    expect(snapshot.entities.find((entityArg) => entityArg.id === 'connection_status')?.state).toBeTrue();
    expect(entities.some((entityArg) => entityArg.state === 321 && entityArg.attributes?.deviceClass === 'power')).toBeTrue();
    expect(requests.every((requestArg) => requestArg.userAgent === 'PythonIOmeter/0.1')).toBeTrue();
    await runtime.destroy();
  } finally {
    await new Promise<void>((resolve, reject) => server.close((errorArg) => errorArg ? reject(errorArg) : resolve()));
  }
});

tap.test('exposes IOmeter runtime and unsupported control without executor', async () => {
  const integration = new IometerIntegration();
  expect(integration.status).toEqual("read-only-runtime");
  expect(iometerProfile.metadata.configFlow).toEqual(true);
  expect(iometerProfile.metadata.requirements).toEqual([
    "iometer==0.4.0",
  ]);

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

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

  const command = await runtime.callService!({ domain: "iometer", service: iometerProfile.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();
