import { createServer, type IncomingMessage, type ServerResponse } from 'node:http';
import { expect, tap } from '@git.zone/tstest/tapbundle';
import { LektricoClient, LektricoConfigFlow, LektricoIntegration, LektricoMapper, createLektricoDiscoveryDescriptor, lektricoProfile, type ILektricoSnapshot, type TLektricoRawData } from '../../ts/integrations/lektrico/index.js';

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

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

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

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

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

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

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

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

tap.test('reads Lektrico charger snapshots over local HTTP JSON-RPC and writes mapped commands', async () => {
  const requests: Array<{ method?: string; url?: string; body?: Record<string, unknown> }> = [];
  const server = createServer((requestArg: IncomingMessage, responseArg: ServerResponse) => {
    void (async () => {
      const url = new URL(requestArg.url || '/', 'http://127.0.0.1');
      let body: Record<string, unknown> | undefined;
      if (requestArg.method === 'POST') {
        body = JSON.parse(await readBody(requestArg)) as Record<string, unknown>;
      }
      requests.push({ method: requestArg.method, url: url.pathname, body });

      if (requestArg.method === 'GET' && url.pathname === '/rpc/Device_id.Get') {
        json(responseArg, { device_id: '3p22k_500006' });
        return;
      }
      if (requestArg.method === 'GET' && url.pathname === '/rpc/charger_config.get') {
        json(responseArg, { serial_number: 500006, board_revision: 'E' });
        return;
      }
      if (requestArg.method === 'GET' && url.pathname === '/rpc/charger_info.get') {
        json(responseArg, {
          currents: [1, 2, 3],
          voltages: [230, 231, 232],
          fw_version: '1.45',
          extended_charger_state: 'C',
          session_energy: 1200,
          charging_time: 60,
          instant_power: 4200,
          temperature: 39.8,
          total_charged_energy: 18,
          has_active_errors: false,
          state_machine_e_activated: false,
          user_current: 32,
          current_limit_reason: 3,
        });
        return;
      }
      if (requestArg.method === 'GET' && url.pathname === '/rpc/app_config.get') {
        json(responseArg, { headless: false, install_current: 32, led_max_brightness: 100 });
        return;
      }
      if (requestArg.method === 'GET' && url.pathname === '/rpc/dynamic_current.get') {
        json(responseArg, { dynamic_current: 16, relay_mode: 3 });
        return;
      }
      if (requestArg.method === 'POST' && url.pathname === '/rpc') {
        json(responseArg, { id: body?.id, src: '3p22k_500006', dst: 'HASS', result: 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 LektricoClient({ url, timeoutMs: 1000 });
    const snapshot = await client.getSnapshot(true);
    const runtime = await new LektricoIntegration().setup({ url, timeoutMs: 1000 }, {});
    const entities = await runtime.entities();
    const dynamicLimit = entities.find((entityArg) => entityArg.attributes?.key === 'dynamic_limit')!;
    const authentication = entities.find((entityArg) => entityArg.attributes?.key === 'authentication')!;
    const forceSinglePhase = entities.find((entityArg) => entityArg.attributes?.key === 'force_single_phase')!;
    const chargeStart = entities.find((entityArg) => entityArg.attributes?.key === 'charge_start')!;

    const dynamicResult = await runtime.callService!({ domain: 'number', service: 'set_value', target: { entityId: dynamicLimit.id }, data: { value: 20 } });
    const authResult = await runtime.callService!({ domain: 'switch', service: 'turn_off', target: { entityId: authentication.id } });
    const forceResult = await runtime.callService!({ domain: 'switch', service: 'turn_on', target: { entityId: forceSinglePhase.id } });
    const chargeResult = await runtime.callService!({ domain: 'button', service: 'press', target: { entityId: chargeStart.id } });

    expect(snapshot.online).toBeTrue();
    expect(snapshot.source).toEqual('http');
    expect(snapshot.device.serialNumber).toEqual('500006');
    expect(snapshot.entities.find((entityArg) => entityArg.id === 'state')?.state).toEqual('charging');
    expect(snapshot.entities.find((entityArg) => entityArg.id === 'power')?.state).toEqual(4200);
    expect(snapshot.entities.find((entityArg) => entityArg.id === 'dynamic_limit')?.state).toEqual(16);
    expect(dynamicResult.success).toBeTrue();
    expect(authResult.success).toBeTrue();
    expect(forceResult.success).toBeTrue();
    expect(chargeResult.success).toBeTrue();
    expect(requests.some((requestArg) => requestArg.method === 'GET' && requestArg.url === '/rpc/Device_id.Get')).toBeTrue();
    expect(requests.some((requestArg) => requestArg.body?.method === 'dynamic_current.set' && (requestArg.body.params as Record<string, unknown>).dynamic_current === 20)).toBeTrue();
    expect(requests.some((requestArg) => requestArg.body?.method === 'app_config.set' && (requestArg.body.params as Record<string, unknown>).config_key === 'headless' && (requestArg.body.params as Record<string, unknown>).config_value === true)).toBeTrue();
    expect(requests.some((requestArg) => requestArg.body?.method === 'dynamic_current.Set' && (requestArg.body.params as Record<string, unknown>).relay_mode === 1)).toBeTrue();
    expect(requests.some((requestArg) => requestArg.body?.method === 'charge.start')).toBeTrue();
    await runtime.destroy();
  } finally {
    await new Promise<void>((resolve, reject) => server.close((errorArg) => errorArg ? reject(errorArg) : resolve()));
  }
});

tap.test('exposes Lektrico Charging Station runtime and unsupported control without executor', async () => {
  const integration = new LektricoIntegration();
  expect(integration.status).toEqual("control-runtime");
  expect(lektricoProfile.metadata.configFlow).toEqual(true);
  expect(lektricoProfile.metadata.requirements).toEqual([
    "lektricowifi==0.1",
  ]);
  expect((lektricoProfile.metadata.localApi as { status: string }).status).toContain('Native local HTTP JSON-RPC is implemented');

  const runtime = await integration.setup({ name: "Lektrico Charging Station Runtime", rawData }, {});
  const statusResult = await runtime.callService!({ domain: "lektrico", service: 'status', target: {} });
  const refresh = await runtime.callService!({ domain: "lektrico", service: 'refresh', target: {} });
  const snapshot = statusResult.data as ILektricoSnapshot;

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

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