import { Injectable } from '@nestjs/common';
import { AuditRepository } from './repositories/audit.repository';

@Injectable()
export class AuditService {
  constructor(private readonly repo: AuditRepository) {}

  async log(
    entityType: string,
    entityId: string,
    action: string,
    changes?: { field: string; old_value: any; new_value: any }[] | null,
  ) {
    return this.repo.log(entityType, entityId, action, changes);
  }

  async getRecentActivity(limit = 10) {
    const items = await this.repo.findRecent(limit);
    return items.map(item => ({
      id: item.id,
      entity_type: item.entity_type,
      entity_id: item.entity_id,
      action: item.action,
      performer_name: (item as any).performer?.name || null,
      created_at: item.created_at,
    }));
  }

  async getByEntity(
    entityType: string,
    entityId: string,
    query?: { page?: number; limit?: number },
  ) {
    return this.repo.findByEntity(entityType, entityId, query);
  }
}
