import { createServer, type IncomingMessage, type ServerResponse } from 'node:http';
import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HomeAssistantInfluxdbIntegration, InfluxdbClient, InfluxdbConfigFlow, InfluxdbIntegration, InfluxdbMapper, createInfluxdbDiscoveryDescriptor, influxdbProfile, type IInfluxdbSnapshot, type TInfluxdbRawData } from '../../ts/integrations/influxdb/index.js';

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

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

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

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

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

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

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

tap.test('exposes InfluxDB runtime, HA alias, and unsupported control without executor', async () => {
  const integration = new InfluxdbIntegration();
  const alias = new HomeAssistantInfluxdbIntegration();
  expect(alias instanceof InfluxdbIntegration).toBeTrue();
  expect(alias.domain).toEqual("influxdb");
  expect(integration.status).toEqual("read-only-runtime");
  expect(influxdbProfile.metadata.configFlow).toEqual(true);
  expect(influxdbProfile.metadata.requirements).toEqual([
    "influxdb==5.3.1",
    "influxdb-client==1.50.0",
  ]);

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

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

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

tap.test('reads InfluxDB v1 repositories and query sensors over local HTTP', async () => {
  const requests: Array<{ url: string; authorization?: string }> = [];
  const server = createServer((requestArg: IncomingMessage, responseArg: ServerResponse) => {
    const url = new URL(requestArg.url || '/', 'http://127.0.0.1');
    requests.push({ url: requestArg.url || '', authorization: requestArg.headers.authorization });
    responseArg.setHeader('content-type', 'application/json');

    if (url.pathname === '/query' && url.searchParams.get('q') === 'SHOW DATABASES;') {
      responseArg.end(JSON.stringify({ results: [{ series: [{ name: 'databases', columns: ['name'], values: [['home_assistant'], ['_internal']] }] }] }));
      return;
    }

    if (url.pathname === '/query' && url.searchParams.get('db') === 'home_assistant' && (url.searchParams.get('q') || '').startsWith('select mean(value)')) {
      responseArg.end(JSON.stringify({ results: [{ series: [{ name: 'temperature', columns: ['time', 'value'], values: [['2026-01-01T00:00:00Z', 21.5]] }] }] }));
      return;
    }

    responseArg.statusCode = 404;
    responseArg.end(JSON.stringify({ error: 'not found' }));
  });

  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 config = {
      host: '127.0.0.1',
      port,
      apiVersion: '1' as const,
      database: 'home_assistant',
      username: 'ha',
      password: 'secret',
      timeoutMs: 1000,
      name: 'Local InfluxDB',
      queries: [{ name: 'Mean Temperature', measurement: 'temperature', field: 'value', where: 'time > now() - 15m', unitOfMeasurement: 'C' }],
    };
    const snapshot = await new InfluxdbClient(config).getSnapshot(true);
    const entities = InfluxdbMapper.toEntities(snapshot);

    expect(requests[0].authorization).toEqual(`Basic ${Buffer.from('ha:secret').toString('base64')}`);
    expect(requests.some((requestArg) => requestArg.url.includes('SHOW+DATABASES'))).toBeTrue();
    expect(snapshot.online).toBeTrue();
    expect(snapshot.source).toEqual('http');
    expect((snapshot.rawData as { repositories: Array<{ name: string }> }).repositories.map((repositoryArg) => repositoryArg.name)).toEqual(['home_assistant', '_internal']);
    expect(entities.find((entityArg) => entityArg.id === 'sensor.local_influxdb_mean_temperature')?.state).toEqual(21.5);

    const runtime = await new InfluxdbIntegration().setup(config, {});
    const status = await runtime.callService!({ domain: 'influxdb', service: 'status', target: {} });
    expect(status.success).toBeTrue();
    expect((status.data as IInfluxdbSnapshot).source).toEqual('http');
    await runtime.destroy();
  } finally {
    await new Promise<void>((resolve, reject) => server.close((errorArg) => errorArg ? reject(errorArg) : resolve()));
  }
});

tap.test('writes InfluxDB v2 line protocol over local HTTP', async () => {
  const bodies: string[] = [];
  const requests: string[] = [];
  const server = createServer((requestArg: IncomingMessage, responseArg: ServerResponse) => {
    const url = new URL(requestArg.url || '/', 'http://127.0.0.1');
    requests.push(`${requestArg.method} ${url.pathname}?${url.searchParams.toString()}`);
    const chunks: Buffer[] = [];
    requestArg.on('data', (chunkArg) => chunks.push(Buffer.isBuffer(chunkArg) ? chunkArg : Buffer.from(chunkArg)));
    requestArg.on('end', () => {
      bodies.push(Buffer.concat(chunks).toString('utf8'));
      expect(requestArg.headers.authorization).toEqual('Token token-123');
      if (requestArg.method === 'POST' && url.pathname === '/api/v2/write') {
        expect(url.searchParams.get('org')).toEqual('home');
        expect(url.searchParams.get('bucket')).toEqual('Home Assistant');
        responseArg.statusCode = 204;
        responseArg.end();
        return;
      }
      responseArg.statusCode = 404;
      responseArg.end('not found');
    });
  });

  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 result = await new InfluxdbClient({ host: '127.0.0.1', port, apiVersion: '2', token: 'token-123', organization: 'home', bucket: 'Home Assistant', timeoutMs: 1000 }).write({
      measurement: 'temperature',
      tags: { entity_id: 'sensor.kitchen' },
      fields: { value: 21.5, state: '21.5' },
    });

    expect(result.success).toBeTrue();
    expect(result.repository).toEqual('Home Assistant');
    expect(requests).toEqual(['POST /api/v2/write?org=home&bucket=Home+Assistant']);
    expect(bodies[0]).toEqual('temperature,entity_id=sensor.kitchen value=21.5,state="21.5"');
  } finally {
    await new Promise<void>((resolve, reject) => server.close((errorArg) => errorArg ? reject(errorArg) : resolve()));
  }
});

export default tap.start();
