import type { AppSettings } from "@/types/settings";

export function buildSettingsDiff(before: AppSettings, after: AppSettings) {
  const entries = Object.entries(after).flatMap(([key, nextValue]) => {
    if (key === "updated_at") return [];

    const prevValue = before[key as keyof AppSettings];
    const sameValue =
      typeof prevValue === "object" || typeof nextValue === "object"
        ? JSON.stringify(prevValue) === JSON.stringify(nextValue)
        : prevValue === nextValue;
    if (sameValue) return [];

    return [
      {
        field: key,
        before: key === "smtp_password" ? "[hidden]" : prevValue,
        after: key === "smtp_password" && nextValue ? "[updated]" : nextValue
      }
    ];
  });

  return entries;
}
