'use client'; import { addDoc, collection, deleteDoc, doc, getDoc, getDocs, orderBy, query, serverTimestamp, limit, setDoc, Firestore, } from 'firebase/firestore'; import { summarizeDocument as summarizeDocumentAI } from '@/ai/flows/ai-document-summarization'; export type Document = { id: string; name: string; content: string; summary: string; createdAt: string; }; export type UserProfile = { id: string; email: string; firstName: string; lastName: string; createdAt: string; updatedAt: string; }; export async function createUserProfile( firestore: Firestore, userId: string, data: { firstName: string; lastName: string; email: string } ) { const userRef = doc(firestore, 'users', userId); return setDoc(userRef, { id: userId, ...data, createdAt: serverTimestamp(), updatedAt: serverTimestamp(), }); } export async function addDocumentAndSummary( firestore: Firestore, userId: string, file: File, fileContentBase64: string ): Promise { // 1. Summarize and get content const { summary, documentContent } = await summarizeDocumentAI({ fileContentBase64, mimeType: file.type, }); // 2. Create Document record const docRef = await addDoc( collection(firestore, 'users', userId, 'documents'), { userId, fileName: file.name, fileType: file.type, fileSize: file.size, uploadDate: serverTimestamp(), status: 'summarized', content: documentContent, } ); // 3. Create Summary record await addDoc(collection(docRef, 'summaries'), { documentId: docRef.id, userId, summaryText: summary, generatedAt: serverTimestamp(), aiModelUsed: 'gemini-2.5-flash', }); return { id: docRef.id, name: file.name, content: documentContent, summary, createdAt: new Date().toISOString(), }; } export async function getDocuments( firestore: Firestore, userId: string ): Promise { const documentsCol = collection(firestore, 'users', userId, 'documents'); const q = query(documentsCol, orderBy('uploadDate', 'desc')); const querySnapshot = await getDocs(q); const documents = querySnapshot.docs.map((doc) => ({ id: doc.id, name: doc.data().fileName, createdAt: doc.data().uploadDate?.toDate().toISOString() ?? new Date().toISOString(), content: '', // Not stored in the document record summary: '', // Not stored in the document record })); return documents; } export async function getDocumentWithSummary( firestore: Firestore, userId: string, docId: string ): Promise { const docRef = doc(firestore, 'users', userId, 'documents', docId); const docSnap = await getDoc(docRef); if (!docSnap.exists()) { return null; } const documentData = docSnap.data(); // Get summary const summariesCol = collection(docRef, 'summaries'); const summaryQuery = query(summariesCol, orderBy('generatedAt', 'desc'), limit(1)); const summarySnapshot = await getDocs(summaryQuery); const summary = summarySnapshot.empty ? '' : summarySnapshot.docs[0].data().summaryText; return { id: docSnap.id, name: documentData.fileName, createdAt: documentData.uploadDate?.toDate().toISOString() ?? new Date().toISOString(), content: documentData.content || 'Original document content not found.', summary: summary, }; } export async function deleteDocumentAndSubcollections( firestore: Firestore, userId: string, docId: string ) { const docRef = doc(firestore, 'users', userId, 'documents', docId); // Delete summaries subcollection const summariesCol = collection(docRef, 'summaries'); const summarySnapshot = await getDocs(summariesCol); const deletePromises = summarySnapshot.docs.map((summaryDoc) => deleteDoc(summaryDoc.ref) ); await Promise.all(deletePromises); // Delete document await deleteDoc(docRef); }