import db from "../db.server";

type LogLevel = "error" | "warn" | "info";

interface LogContext {
  shopId?: string;
  [key: string]: unknown;
}

export async function adminLog(
  level: LogLevel,
  message: string,
  context?: LogContext,
): Promise<void> {
  try {
    await db.adminLog.create({
      data: {
        level,
        message,
        context: context ? JSON.stringify(context) : null,
      },
    });
  } catch {
    // Fallback to console if DB write fails
    console.error(`[AdminLog fallback] ${level}: ${message}`, context);
  }
}
