import {
  Logger,
  AxiomJSTransport,
  ConsoleTransport,
  type Transport,
} from "@axiomhq/logging";
import { nextJsFormatters } from "@axiomhq/nextjs";
import { axiom, dataset, getCoreEventFields } from "./axiom";

/** JSON-serializable event data values */
type EventValue =
  | string
  | number
  | boolean
  | null
  | undefined
  | string[]
  | number[];
export type EventData = Record<string, EventValue>;

// Axiom is disabled in dev and during `next build` (its no-store ingest fetch
// would flip ISR routes to dynamic rendering).
const isBuildPhase = process.env.NEXT_PHASE === "phase-production-build";
const isDev = process.env.NODE_ENV === "development";

const transports: [Transport, ...Transport[]] =
  isDev || isBuildPhase
    ? [new ConsoleTransport()]
    : [new AxiomJSTransport({ axiom, dataset, ...nextJsFormatters })];

export const logger = new Logger({ transports });

/** Wide event logging helper for server-side events */
export function logEvent(eventName: string, data: EventData): void {
  const coreFields = getCoreEventFields();
  logger.info(eventName, {
    ...coreFields,
    eventName,
    data,
  });
}

/** Async version that flushes immediately (use for request handlers) */
export async function logEventAsync(
  eventName: string,
  data: EventData
): Promise<void> {
  logEvent(eventName, data);
  try {
    await logger.flush();
  } catch (error) {
    console.error("Failed to flush logs:", error);
  }
}

export { logger as serverLogger };
