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

export async function fetchCallLogsForRestoration(limit = 100) {
  try {
    const { data, error } = await supabase
      .from('call_logs')
      .select('*')
      .order('created_at', { ascending: false })
      .limit(limit);

    if (error) throw error;

    const issues = [];
    data.forEach(log => {
        if (!log.client_id) issues.push({ id: log.id, issue: 'Missing Client ID' });
    });

    return { records: data, count: data.length, issues };
  } catch (err) {
    console.error('Restoration fetch failed:', err);
    throw err;
  }
}

export async function restoreCallLogs(ids) {
    if (!ids || ids.length === 0) return { successCount: 0 };

    let successCount = 0;
    const errors = [];

    for (const id of ids) {
        const { error } = await supabase
            .from('call_logs')
            .update({ updated_at: new Date().toISOString() })
            .eq('id', id);
        
        if (error) errors.push({ id, error: error.message });
        else successCount++;
    }

    return { successCount, errors };
}
