/**
 * Platform Logger — writes structured logs to PlatformLog for the
 * admin dashboard. Fire-and-forget.
 */

import { prisma } from "@/lib/prisma";

export function logPlatform(
  level: "error" | "warn" | "info" | "debug",
  source: string,
  message: string,
  details?: { userId?: string; brandId?: string; endpoint?: string; duration?: number; statusCode?: number; stack?: string; [key: string]: unknown },
): void {
  prisma.platformLog.create({
    data: {
      level, source, message,
      details: details ? (details as object) : undefined,
      userId: details?.userId ?? null,
      brandId: details?.brandId ?? null,
      endpoint: details?.endpoint ?? null,
      duration: details?.duration ?? null,
      statusCode: details?.statusCode ?? null,
    },
  }).catch((err) => console.error("[platform-logger]", err));
}
