import { Injectable } from '@nestjs/common';
import { PrismaService } from '../infra/prisma.service';

export interface RecordActivityInput {
  userId: string;
  action: string;
  title: string;
  detail?: string;
  entityType?: string;
  entityId?: string;
  ipAddress?: string;
  userAgent?: string;
  actorId?: string;
}

@Injectable()
export class ActivityLogService {
  constructor(private readonly prisma: PrismaService) {}

  async record(input: RecordActivityInput) {
    return this.prisma.userActivityLog.create({
      data: {
        userId: input.userId,
        action: input.action,
        title: input.title,
        detail: input.detail,
        entityType: input.entityType,
        entityId: input.entityId,
        ipAddress: input.ipAddress,
        userAgent: input.userAgent,
        actorId: input.actorId
      }
    });
  }

  recordSafe(input: RecordActivityInput) {
    void this.record(input).catch(() => undefined);
  }
}
