import { Injectable } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { PrismaService } from '../database/prisma.service';

type AuditInput = {
  estateId?: string | null;
  actorType: string;
  actorId: string;
  action: string;
  metadata?: Record<string, unknown>;
};

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

  async record(input: AuditInput) {
    return this.prisma.auditLog.create({
      data: {
        estateId: input.estateId ?? null,
        actorType: input.actorType,
        actorId: input.actorId,
        action: input.action,
        metadata: (input.metadata ?? {}) as Prisma.InputJsonValue
      }
    });
  }
}
