import { setHeader } from 'h3';

import Database from '#server/utils/Database';
import WireGuard from '#server/utils/WireGuard';
import { defineMetricsHandler } from '#server/utils/handler';
import { formatPrometheusLabels } from '#server/utils/prometheus';
import { isPeerConnected } from '#shared/utils/time';

export default defineMetricsHandler('prometheus', async ({ event }) => {
  setHeader(event, 'Content-Type', 'text/plain');
  return getPrometheusResponse();
});

async function getPrometheusResponse() {
  const wgInterface = await Database.interfaces.get();
  const clients = await WireGuard.getAllClients();
  let wireguardEnabledPeersCount = 0;
  let wireguardConnectedPeersCount = 0;
  const wireguardSentBytes = [];
  const wireguardReceivedBytes = [];
  const wireguardLatestHandshakeSeconds = [];
  for (const client of clients) {
    if (client.enabled === true) {
      wireguardEnabledPeersCount++;
    }

    if (isPeerConnected(client)) {
      wireguardConnectedPeersCount++;
    }

    const id = formatPrometheusLabels({
      interface: wgInterface.name,
      enabled: client.enabled,
      ipv4Address: client.ipv4Address,
      ipv6Address: client.ipv6Address,
      name: client.name,
    });

    wireguardSentBytes.push(
      `wireguard_sent_bytes{${id}} ${client.transferTx ?? 0}`
    );
    wireguardReceivedBytes.push(
      `wireguard_received_bytes{${id}} ${client.transferRx ?? 0}`
    );
    // TODO: if latestHandshakeAt is null this would result in client showing as online?
    wireguardLatestHandshakeSeconds.push(
      `wireguard_latest_handshake_seconds{${id}} ${client.latestHandshakeAt ? (Date.now() - client.latestHandshakeAt.getTime()) / 1000 : 0}`
    );
  }

  const id = formatPrometheusLabels({ interface: wgInterface.name });

  const returnText = [
    '# HELP wireguard_configured_peers',
    '# TYPE wireguard_configured_peers gauge',
    `wireguard_configured_peers{${id}} ${clients.length}`,
    '',
    '# HELP wireguard_enabled_peers',
    '# TYPE wireguard_enabled_peers gauge',
    `wireguard_enabled_peers{${id}} ${wireguardEnabledPeersCount}`,
    '',
    '# HELP wireguard_connected_peers',
    '# TYPE wireguard_connected_peers gauge',
    `wireguard_connected_peers{${id}} ${wireguardConnectedPeersCount}`,
    '',
    '# HELP wireguard_sent_bytes Bytes sent to the peer',
    '# TYPE wireguard_sent_bytes counter',
    `${wireguardSentBytes.join('\n')}`,
    '',
    '# HELP wireguard_received_bytes Bytes received from the peer',
    '# TYPE wireguard_received_bytes counter',
    `${wireguardReceivedBytes.join('\n')}`,
    '',
    '# HELP wireguard_latest_handshake_seconds UNIX timestamp seconds of the last handshake',
    '# TYPE wireguard_latest_handshake_seconds gauge',
    `${wireguardLatestHandshakeSeconds.join('\n')}`,
    '',
  ];

  return returnText.join('\n');
}
