import { connectDB } from "@/lib/db";
import { Client } from "@/models/Client";
import { TeamActivity } from "@/models/TeamActivity";
import { getSessionClientId } from "@/lib/auth";

// Bewusst nur für sensible, teamweite Änderungen instrumentiert (Rechte,
// Konten, Positionen) — nicht für jede einzelne Auftragsaktion, die bereits
// über OrderActivity protokolliert wird.
export async function logTeamActivity(action: string, targetLabel: string = "") {
  const actorId = await getSessionClientId();
  if (!actorId) return;
  await connectDB();
  const actor = await Client.findById(actorId).select("name").lean();
  await TeamActivity.create({ actorId, actorName: actor?.name || "Unbekannt", action, targetLabel });
}

// Gleiche Funktion, neutraler Name für Aufrufe außerhalb von actions/team.ts
// (z. B. sicherheitsrelevante Einstellungsänderungen) — bewusst dieselbe
// Collection/Anzeige statt eines zweiten, separaten Logs.
export const logAdminActivity = logTeamActivity;
