File size: 3,674 Bytes
2451d08
 
c15fa2f
 
 
 
2451d08
 
 
 
 
 
 
 
 
c15fa2f
 
2451d08
 
c15fa2f
2451d08
 
 
 
 
 
 
 
 
 
 
 
 
 
c15fa2f
2451d08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c15fa2f
2451d08
c15fa2f
 
 
 
2451d08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c15fa2f
 
2451d08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c15fa2f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { createClient } from '@supabase/supabase-js';
import { SUPABASE_URL, SUPABASE_ANON_KEY } from 'astro:env/client';

export type SpeakingRecord = Record<string, unknown> & {
  id: number | string;
  task_type: string;
  task_description?: string;
  rephrased_task?: string;
  vocabulary?: string[] | null;
  response?: string;
  response_alternative?: string;
  brainstorm?: Array<{title: string, description: string}> | null;
  brainstorm_alternative?: Array<{title: string, description: string}> | null;
  created_at?: string;
  updated_at?: string;
};

// Initialize Supabase client
let supabase: ReturnType<typeof createClient> | null = null;

function getSupabaseClient() {
  if (!supabase) {
    if (!SUPABASE_URL || !SUPABASE_ANON_KEY) {
      throw new Error('Missing Supabase configuration');
    }
    supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
  }
  return supabase;
}

async function loadFromDatabase(
  tableName: 'speaking_tasks' | 'writing_tasks',
  taskType?: string
): Promise<SpeakingRecord[]> {
  try {
    const client = getSupabaseClient();
    let query = client.from(tableName).select('*');

    // Add task type filter if specified
    if (taskType) {
      query = query.eq('task_type', taskType);
    }

    const { data, error } = await query.order('id', { ascending: true });

    if (error) {
      // If table doesn't exist (PGRST205 error), return empty array for writing_tasks
      // but log the error for speaking_tasks since it should exist
      if (error.code === 'PGRST205' && tableName === 'writing_tasks') {
        console.log(`Writing tasks table not found - returning empty array (table not created yet)`);
        return [];
      }
      console.error(`Error loading data from ${tableName}:`, error);
      return [];
    }

    return data || [];
  } catch (error) {
    console.error(`Database connection error for ${tableName}:`, error);
    return [];
  }
}

export async function loadSpeakingData(taskType?: string): Promise<SpeakingRecord[]> {
  return loadFromDatabase('speaking_tasks', taskType);
}

export async function loadWritingData(taskType?: string): Promise<SpeakingRecord[]> {
  return loadFromDatabase('writing_tasks', taskType);
}

// Legacy functions for backward compatibility
export async function loadAllSpeakingData(): Promise<SpeakingRecord[]> {
  return loadSpeakingData();
}

export async function loadAllWritingData(): Promise<SpeakingRecord[]> {
  return loadWritingData();
}

// Utility function to get task by ID and type
export async function getTaskById(
  tableName: 'speaking_tasks' | 'writing_tasks',
  id: number | string,
  taskType: string
): Promise<SpeakingRecord | null> {
  try {
    const client = getSupabaseClient();
    const { data, error } = await client
      .from(tableName)
      .select('*')
      .eq('id', id)
      .eq('task_type', taskType)
      .single();

    if (error) {
      console.error(`Error fetching task ${id}-${taskType}:`, error);
      return null;
    }

    return data;
  } catch (error) {
    console.error(`Database connection error for task fetch:`, error);
    return null;
  }
}

// Utility function to count tasks
export async function getTaskCount(tableName: 'speaking_tasks' | 'writing_tasks'): Promise<number> {
  try {
    const client = getSupabaseClient();
    const { count, error } = await client
      .from(tableName)
      .select('*', { count: 'exact', head: true });

    if (error) {
      console.error(`Error counting tasks in ${tableName}:`, error);
      return 0;
    }

    return count || 0;
  } catch (error) {
    console.error(`Database connection error for task count:`, error);
    return 0;
  }
}