import { db } from "@/lib/db";
import { SessionUser } from "@/lib/auth";

interface LogOptions {
  action: string;
  entity: string;
  entityId?: string | null;
  details?: string | null;
}

/** تسجيل حركة في سجل الحركات الأمني (Audit Log) */
export async function logAudit(user: SessionUser | null, opts: LogOptions) {
  try {
    await db.auditLog.create({
      data: {
        userId: user?.id ?? null,
        userName: user?.fullName ?? "غير معروف",
        role: user?.role ?? null,
        action: opts.action,
        entity: opts.entity,
        entityId: opts.entityId ?? null,
        details: opts.details ?? null,
      },
    });
  } catch (err) {
    console.error("audit log failed:", err);
  }
}
