import { diffFields } from '@/lib/audit';

const SKIP_FIELDS = new Set(['createdAt', 'updatedAt', 'portId']);

/**
 * Wraps `diffFields` with automatic exclusion of metadata fields
 * (createdAt, updatedAt, portId).
 */
export function diffEntity<T extends Record<string, unknown>>(
  oldRecord: T,
  newRecord: Partial<T>,
): { changed: boolean; diff: Record<string, { old: unknown; new: unknown }> } {
  // Only compare keys present in newRecord
  const filteredOld: Record<string, unknown> = {};
  const filteredNew: Record<string, unknown> = {};

  for (const key of Object.keys(newRecord)) {
    if (SKIP_FIELDS.has(key)) continue;
    filteredOld[key] = oldRecord[key];
    filteredNew[key] = newRecord[key];
  }

  const changes = diffFields(filteredOld, filteredNew);

  const diff: Record<string, { old: unknown; new: unknown }> = {};
  for (const change of changes) {
    diff[change.field] = { old: change.oldValue, new: change.newValue };
  }

  return { changed: changes.length > 0, diff };
}
