
import { supabase } from '@/integrations/supabase/client';

/**
 * Routing Audit Logger
 */
export async function writeRoutingAudit({
  transactionId,
  ruleId = null,
  matchedRulePriority = null,
  debitAccountId = null,
  creditAccountId = null,
  explanation = '',
  status = 'success',
  errorMessage = null
}) {
  try {
    const { error } = await supabase.from('routing_audit_log').insert([{
      transaction_id: transactionId,
      rule_id: ruleId,
      matched_rule_priority: matchedRulePriority,
      debit_account_id: debitAccountId,
      credit_account_id: creditAccountId,
      explanation,
      status,
      error_message: errorMessage
    }]);
    
    if (error) {
      console.error("Failed to write routing audit:", error);
    }
  } catch (err) {
    console.error("Exception in writeRoutingAudit:", err);
  }
}
