import { createServer, type IncomingMessage } from 'node:http';
import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HomeAssistantNzbgetIntegration, NzbgetClient, NzbgetConfigFlow, NzbgetIntegration, NzbgetMapper, createNzbgetDiscoveryDescriptor, nzbgetProfile, type INzbgetSnapshot, type TNzbgetRawData } from '../../ts/integrations/nzbget/index.js';

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

tap.test('reads and controls NZBGet over the native XML-RPC endpoint', async () => {
  const requests: Array<{ method?: string; authorization?: string; body: string }> = [];
  const server = createServer((requestArg, responseArg) => {
    void (async () => {
      const body = await readBody(requestArg);
      requests.push({ method: requestArg.method, authorization: requestArg.headers.authorization, body });
      const method = body.match(/<methodName>([^<]+)<\/methodName>/)?.[1] || '';
      responseArg.setHeader('content-type', 'text/xml');
      if (requestArg.headers.authorization !== `Basic ${Buffer.from('nzb:secret').toString('base64')}`) {
        responseArg.statusCode = 401;
        responseArg.end('auth required');
        return;
      }
      if (method === 'version') {
        responseArg.end(xmlRpcResponse('21.1'));
        return;
      }
      if (method === 'status') {
        responseArg.end(xmlRpcResponse({ DownloadPaused: false, DownloadRate: 2048, RemainingSizeMB: 512, FreeDiskSpaceMB: 1024, UpTimeSec: 10, DownloadLimit: 1000 }));
        return;
      }
      if (method === 'history') {
        responseArg.end(xmlRpcResponse([{ Name: 'done.nzb', Category: 'movies', Status: 'SUCCESS' }]));
        return;
      }
      if (method === 'pausedownload' || method === 'rate') {
        responseArg.end(xmlRpcResponse(true));
        return;
      }
      responseArg.statusCode = 500;
      responseArg.end(xmlRpcResponse({ faultCode: 1, faultString: `unexpected ${method}` }));
    })().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 client = new NzbgetClient({ host: '127.0.0.1', port, username: 'nzb', password: 'secret', timeoutMs: 1000 });
    const snapshot = await client.getSnapshot();
    const pause = await client.execute({ domain: 'nzbget', service: 'pause', target: {} });
    const speed = await client.execute({ domain: 'nzbget', service: 'set_speed', target: {}, data: { speed: 4096 } });

    expect(snapshot.online).toBeTrue();
    expect(snapshot.source).toEqual('http');
    expect(snapshot.device.model).toEqual('NZBGet 21.1');
    expect(snapshot.entities.find((entityArg) => entityArg.id === 'download_rate')?.state).toEqual(2048);
    expect(snapshot.entities.find((entityArg) => entityArg.id === 'download')?.state).toBeTrue();
    expect(pause.success).toBeTrue();
    expect(speed.success).toBeTrue();
    expect(requests.some((requestArg) => requestArg.body.includes('<methodName>history</methodName>'))).toBeTrue();
    expect(requests.some((requestArg) => requestArg.body.includes('<methodName>rate</methodName>') && requestArg.body.includes('<int>4096</int>'))).toBeTrue();
  } finally {
    await new Promise<void>((resolve, reject) => server.close((errorArg) => errorArg ? reject(errorArg) : resolve()));
  }
});

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

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

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

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

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

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

tap.test('exposes NZBGet runtime, HA alias, and unsupported control without executor', async () => {
  const integration = new NzbgetIntegration();
  const alias = new HomeAssistantNzbgetIntegration();
  expect(alias instanceof NzbgetIntegration).toBeTrue();
  expect(alias.domain).toEqual("nzbget");
  expect(integration.status).toEqual("control-runtime");
  expect(nzbgetProfile.metadata.configFlow).toEqual(true);
  expect(nzbgetProfile.metadata.requirements).toEqual([
    "pynzbgetapi==0.2.0",
  ]);

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

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

  const command = await runtime.callService!({ domain: "nzbget", service: nzbgetProfile.controlServices?.[0] || 'turn_on', target: {} });
  expect(command.success).toBeFalse();
  expect(command.error!).toContain('Static snapshots/manual data are read-only');
  await runtime.destroy();
});

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 xmlRpcResponse = (valueArg: unknown): string => `<?xml version="1.0"?><methodResponse><params><param>${xmlRpcValue(valueArg)}</param></params></methodResponse>`;

const xmlRpcValue = (valueArg: unknown): string => {
  if (typeof valueArg === 'boolean') {
    return `<value><boolean>${valueArg ? '1' : '0'}</boolean></value>`;
  }
  if (typeof valueArg === 'number') {
    return `<value><int>${valueArg}</int></value>`;
  }
  if (typeof valueArg === 'string') {
    return `<value><string>${valueArg}</string></value>`;
  }
  if (Array.isArray(valueArg)) {
    return `<value><array><data>${valueArg.map(xmlRpcValue).join('')}</data></array></value>`;
  }
  if (valueArg && typeof valueArg === 'object') {
    const members = Object.entries(valueArg as Record<string, unknown>).map(([keyArg, memberValueArg]) => `<member><name>${keyArg}</name>${xmlRpcValue(memberValueArg)}</member>`).join('');
    return `<value><struct>${members}</struct></value>`;
  }
  return '<value><nil/></value>';
};

export default tap.start();
