/** * 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 { 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 { 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: [], }; }