import { eq } from "drizzle-orm"
import { db } from "../db/index.js"
import { servers } from "../db/schema.js"
import { listUsers } from "../modules/users/service/users-service.js"
import { readServerSamplesInRange } from "./traffic-collector.js"
import {
  buildTrafficFromSamples,
  mergeBuiltTraffic,
  type BuiltTrafficSeries,
} from "./traffic-rate.js"

export interface BoundIfaceTrafficDto {
  id: string
  bindingId: string
  userId: string
  userLogin: string
  userName: string
  interfaceName: string
  interfaceType: string
  peerPublicKey: string
  peerName: string
  comment: string
  serverId: string
  serverName: string
  serverSite: string
  serverCountry: string
  rxNow: number
  txNow: number
  rxPeak: number
  txPeak: number
  rxTotal: number
  txTotal: number
  rxSeries: number[]
  txSeries: number[]
  status: "online" | "offline"
}

export interface UserTrafficDto {
  id: string
  login: string
  displayName: string
  role: string
  active: boolean
  interfaces: BoundIfaceTrafficDto[]
  rxNow: number
  txNow: number
  rxPeak: number
  txPeak: number
  rxTotal: number
  txTotal: number
  rxSeries: number[]
  txSeries: number[]
}

function seriesFromBuilt(built: BuiltTrafficSeries) {
  return {
    rxNow: built.rxNow,
    txNow: built.txNow,
    rxPeak: built.rxPeak,
    txPeak: built.txPeak,
    rxTotal: built.rxTotalGiB,
    txTotal: built.txTotalGiB,
    rxSeries: built.rxSeries,
    txSeries: built.txSeries,
  }
}

function serverStatus(serverId: number): "online" | "offline" {
  const row = db.select().from(servers).where(eq(servers.id, serverId)).limit(1).all()[0]
  if (!row?.enabled) return "offline"
  return "online"
}

export function buildUserTrafficList(rangeStartMs: number, rangeEndMs: number): UserTrafficDto[] {
  const sinceIso = new Date(rangeStartMs).toISOString()
  const users = listUsers()
  const sampleCache = new Map<number, ReturnType<typeof readServerSamplesInRange>>()

  return users.map((user) => {
    const parts: BuiltTrafficSeries[] = []
    const interfaces: BoundIfaceTrafficDto[] = []

    for (const b of user.bindings) {
      let rows = sampleCache.get(b.serverId)
      if (!rows) {
        rows = readServerSamplesInRange(b.serverId, sinceIso)
        sampleCache.set(b.serverId, rows)
      }
      const peerKey = b.peerPublicKey ?? ""
      const built = buildTrafficFromSamples(rows, rangeStartMs, rangeEndMs, b.interfaceName, peerKey)
      parts.push(built)
      const last = [...rows.filter((r) =>
        r.interfaceName === b.interfaceName && (r.peerPublicKey ?? "") === peerKey,
      )]
        .sort((a, c) => a.sampledAt.localeCompare(c.sampledAt))
        .at(-1)
      const running = Boolean(last?.running) && !last?.disabled
      interfaces.push({
        id: `${b.userId}:${b.serverId}:${b.interfaceName}:${peerKey || "_iface"}`,
        bindingId: b.id,
        userId: user.id,
        userLogin: user.login,
        userName: user.name,
        interfaceName: b.interfaceName,
        interfaceType: b.interfaceType,
        peerPublicKey: peerKey,
        peerName: b.peerName ?? "",
        comment: b.comment,
        serverId: String(b.serverId),
        serverName: b.serverName,
        serverSite: b.serverSite,
        serverCountry: b.serverCountry,
        ...seriesFromBuilt(built),
        status: running && serverStatus(b.serverId) === "online" ? "online" : "offline",
      })
    }

    const merged = mergeBuiltTraffic(parts)
    return {
      id: user.id,
      login: user.login,
      displayName: user.name,
      role: user.role,
      active: user.active,
      interfaces,
      ...seriesFromBuilt(merged),
    }
  })
}

export function buildBoundInterfaceTraffic(rangeStartMs: number, rangeEndMs: number): BoundIfaceTrafficDto[] {
  return buildUserTrafficList(rangeStartMs, rangeEndMs).flatMap((u) => u.interfaces)
}
