import { ContactLog } from '@/types';
import { getSupabaseClient } from '@/lib/supabase';

export async function fetchContactLogs(params?: {
  batchId?: string;
  status?: string;
  search?: string;
}): Promise<ContactLog[]> {
  const supabase = getSupabaseClient();
  let query = supabase
    .from('contact_logs')
    .select('*')
    .order('created_at', { ascending: false });

  if (params?.batchId) {
    query = query.eq('batch_id', params.batchId);
  }
  if (params?.status && params.status !== 'all') {
    query = query.eq('status', params.status);
  }
  if (params?.search) {
    query = query.or(`keyword.ilike.%${params.search}%,merchant_name.ilike.%${params.search}%,title.ilike.%${params.search}%`);
  }

  const { data, error } = await query;
  if (error) throw error;
  return (data ?? []) as ContactLog[];
}
