import type { SupabaseClient } from "@supabase/supabase-js";

type AdminAuditArgs = {
  action: string;
  after?: unknown;
  before?: unknown;
  operatorEmail: string;
  operatorId: string;
  role: string;
  supabase: SupabaseClient;
  targetId: string;
  targetType: string;
};

export async function recordAdminAuditLog({
  action,
  after,
  before,
  operatorEmail,
  operatorId,
  role,
  supabase,
  targetId,
  targetType,
}: AdminAuditArgs) {
  const { error } = await supabase.from("admin_audit_log").insert({
    action,
    after,
    before,
    operator_email: operatorEmail,
    operator_id: operatorId,
    role,
    target_id: targetId,
    target_type: targetType,
  });

  if (error) {
    throw new Error(`Failed to write admin audit log: ${error.message}`);
  }
}
