import prisma from "./prisma";

export interface ActivityEntry {
  action: string;
  target?: string;
  metadata?: Record<string, unknown>;
  ipAddress?: string;
  userAgent?: string;
}

export async function logUserActivity(
  userId: string,
  tenantId: string,
  entry: ActivityEntry
) {
  return prisma.userActivityLog.create({
    data: {
      userId,
      tenantId,
      action: entry.action,
      target: entry.target,
      metadata: (entry.metadata ?? {}) as never,
      ipAddress: entry.ipAddress,
      userAgent: entry.userAgent,
    },
  });
}

export async function getUserActivity(
  userId: string,
  options: { limit?: number; offset?: number } = {}
) {
  const { limit = 20, offset = 0 } = options;

  return prisma.userActivityLog.findMany({
    where: { userId },
    orderBy: { createdAt: "desc" },
    take: limit,
    skip: offset,
  });
}

export async function getUserActivityCount(userId: string) {
  return prisma.userActivityLog.count({ where: { userId } });
}
