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

/**
 * Logs the event of converting a checklist item to a task.
 */
export async function trackChecklistToTaskConversion({ taskId, checklistId, itemId, itemTitle, userId }) {
  try {
    const timestamp = new Date().toISOString();
    
    console.log(`[Audit] Checklist Item Converted to Task`, {
      event: 'checklist_item_conversion',
      timestamp,
      userId,
      checklistId,
      itemId,
      taskId,
      itemTitle
    });

    const { error } = await supabase.from('notifications').insert({
      user_id: userId,
      type: 'system_log',
      title: 'Task Created from Checklist',
      content: `Converted item "${itemTitle}" from checklist to task.`,
      related_item_id: taskId,
      related_item_type: 'task',
      is_read: true
    });

    if (error) {
      console.warn('Failed to log conversion notification:', error);
    }

  } catch (err) {
    console.error('Error tracking conversion:', err);
  }
}
