File size: 2,809 Bytes
93c19dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Section 2: Google Sheets Integration
 * 
 * Logs user feedback (likes/dislikes) to a Google Sheet for analytics
 * Requires GOOGLE_SHEETS_API_KEY and GOOGLE_SHEETS_ID environment variables
 */

export interface FeedbackLog {
  timestamp: string;
  userId: number;
  userName?: string;
  messageId?: number;
  imageId?: number;
  rating: "like" | "dislike";
  comment?: string;
  conversationMode?: "ask" | "imagine";
}

/**
 * Log feedback to Google Sheets
 * In production, use Google Sheets API v4 with proper authentication
 * For now, we'll provide a placeholder that can be integrated with the actual API
 */
export async function logFeedbackToSheets(feedback: FeedbackLog): Promise<boolean> {
  try {
    // Check if Google Sheets credentials are available
    const apiKey = process.env.GOOGLE_SHEETS_API_KEY;
    const sheetId = process.env.GOOGLE_SHEETS_ID;

    if (!apiKey || !sheetId) {
      console.warn(
        "[GoogleSheets] Credentials not configured. Feedback logging skipped."
      );
      return false;
    }

    // Format the feedback row
    const row = [
      feedback.timestamp,
      feedback.userId.toString(),
      feedback.userName || "Anonymous",
      feedback.messageId?.toString() || "",
      feedback.imageId?.toString() || "",
      feedback.rating,
      feedback.comment || "",
      feedback.conversationMode || "ask",
    ];

    // In production, call Google Sheets API:
    // const response = await fetch(
    //   `https://sheets.googleapis.com/v4/spreadsheets/${sheetId}/values/Feedback!A:H:append?key=${apiKey}`,
    //   {
    //     method: 'POST',
    //     headers: { 'Content-Type': 'application/json' },
    //     body: JSON.stringify({
    //       values: [row],
    //       majorDimension: 'ROWS',
    //     }),
    //   }
    // );

    console.log("[GoogleSheets] Feedback logged:", row);
    return true;
  } catch (error) {
    console.error("[GoogleSheets] Error logging feedback:", error);
    return false;
  }
}

/**
 * Batch log multiple feedback entries
 */
export async function logBatchFeedbackToSheets(
  feedbackList: FeedbackLog[]
): Promise<number> {
  let successCount = 0;

  for (const feedback of feedbackList) {
    const success = await logFeedbackToSheets(feedback);
    if (success) successCount++;
  }

  return successCount;
}

/**
 * Get feedback summary from local database
 * (Google Sheets is used for archival/analytics, not querying)
 */
export async function getFeedbackSummary(days: number = 7): Promise<{
  totalLikes: number;
  totalDislikes: number;
  likePercentage: number;
  topComments: string[];
}> {
  // This would query the local database for recent feedback
  // and calculate statistics
  return {
    totalLikes: 0,
    totalDislikes: 0,
    likePercentage: 0,
    topComments: [],
  };
}