import { supabase } from '@/config'; import { auditedSupabase } from '@/hooks/useAuditedSupabase'; // ============================================================================= // Types // ============================================================================= export interface Question { id: string; text: string; partner_text: string | null; intensity: number; pack_id: string; created_at: string | null; } export interface QuestionFormData { text: string; partner_text: string; intensity: number; } // ============================================================================= // Read Operations // ============================================================================= /** * Fetch all questions for a pack */ export async function fetchQuestionsForPack(packId: string): Promise { const { data, error } = await supabase .from('questions') .select('*') .eq('pack_id', packId) .order('created_at', { ascending: true }); if (error) throw error; return data || []; } /** * Fetch a single question by ID */ export async function fetchQuestionById(id: string): Promise { const { data, error } = await supabase .from('questions') .select('*') .eq('id', id) .maybeSingle(); if (error) throw error; return data; } /** * Fetch question count for a pack */ export async function fetchQuestionCount(packId: string): Promise { const { count, error } = await supabase .from('questions') .select('*', { count: 'exact', head: true }) .eq('pack_id', packId); if (error) throw error; return count || 0; } // ============================================================================= // Write Operations // ============================================================================= /** * Create a new question */ export async function createQuestion( packId: string, data: QuestionFormData ): Promise { const { data: created, error } = await auditedSupabase.insert('questions', { text: data.text, partner_text: data.partner_text || null, intensity: data.intensity, pack_id: packId, }); if (error) throw error; if (!created || created.length === 0) throw new Error('Failed to create question'); return created[0] as Question; } /** * Update an existing question */ export async function updateQuestion( id: string, data: Partial ): Promise { const { error } = await auditedSupabase.update('questions', id, { text: data.text, partner_text: data.partner_text || null, intensity: data.intensity, }); if (error) throw error; } /** * Delete a question */ export async function deleteQuestion(id: string): Promise { const { error } = await auditedSupabase.delete('questions', id); if (error) throw error; } /** * Delete multiple questions */ export async function deleteQuestions(ids: string[]): Promise { await Promise.all( ids.map(id => auditedSupabase.delete('questions', id)) ); } /** * Bulk create questions (for AI generation) */ export async function bulkCreateQuestions( packId: string, questions: Array<{ text: string; partner_text?: string; intensity: number }> ): Promise { const results: Question[] = []; for (const q of questions) { const { data, error } = await auditedSupabase.insert('questions', { pack_id: packId, text: q.text, partner_text: q.partner_text || null, intensity: q.intensity, }); if (error) throw error; if (data && data.length > 0) results.push(data[0] as Question); } return results; }