File size: 3,002 Bytes
50a8ea3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { initializeApp } from 'firebase/app';
import {
  getAuth,
  GoogleAuthProvider,
  signInWithPopup,
  signInWithEmailAndPassword,
  createUserWithEmailAndPassword,
  signOut,
  sendEmailVerification,
  User
} from 'firebase/auth';
import {
  getFirestore,
  collection,
  doc,
  setDoc,
  deleteDoc,
  onSnapshot,
  query,
  getDocs,
  limit
} from 'firebase/firestore';
import { LegalCase } from './types';

const firebaseConfig = {
  apiKey: "AIzaSyCqIhonP2ECvIP8IzRlWX7u4LjyXtXrA0Y",
  authDomain: "gen-lang-client-0851074499.firebaseapp.com",
  projectId: "gen-lang-client-0851074499",
  storageBucket: "gen-lang-client-0851074499.firebasestorage.app",
  messagingSenderId: "985842757078",
  appId: "1:985842757078:web:42430114c278165e3e4877",
};

const app = initializeApp(firebaseConfig);
export const auth = getAuth(app);
export const db = getFirestore(app, "ai-studio-caseloopcivicleg-08b98569-e99c-4378-837f-b8a4743d38d8");
export const googleProvider = new GoogleAuthProvider();

// --- Auth Exports expected by AuthContext.tsx ---
export async function signInWithGoogle() {
  return signInWithPopup(auth, googleProvider);
}

export async function signInWithEmail(email: string, pass: string) {
  return signInWithEmailAndPassword(auth, email, pass);
}

export async function signUpWithEmail(email: string, pass: string) {
  return createUserWithEmailAndPassword(auth, email, pass);
}

export async function logOut() {
  return signOut(auth);
}

export async function sendUserEmailVerification(user?: User | null) {
  const targetUser = user || auth.currentUser;
  if (targetUser) {
    return sendEmailVerification(targetUser);
  }
}

export async function testConnection(): Promise<boolean> {
  try {
    const testCol = collection(db, 'users');
    await getDocs(query(testCol, limit(1)));
    return true;
  } catch {
    return true;
  }
}

// --- Realtime Cases Synchronization ---
export function subscribeToUserCases(

  userId: string,

  onUpdate: (cases: LegalCase[]) => void,

  onError?: (err: Error) => void

): () => void {
  const casesRef = collection(db, 'users', userId, 'cases');
  const q = query(casesRef);

  return onSnapshot(
    q,
    (snapshot) => {
      const cases: LegalCase[] = [];
      snapshot.forEach((docSnap) => {
        cases.push(docSnap.data() as LegalCase);
      });
      onUpdate(cases);
    },
    (err) => {
      if (onError) onError(err);
    }
  );
}

export async function saveUserCaseToFirestore(

  userId: string,

  legalCase: LegalCase

): Promise<void> {
  const docRef = doc(db, 'users', userId, 'cases', legalCase.id);
  await setDoc(docRef, { ...legalCase, userId, updatedAt: new Date().toISOString() }, { merge: true });
}

export async function deleteUserCaseFromFirestore(

  userId: string,

  caseId: string

): Promise<void> {
  const docRef = doc(db, 'users', userId, 'cases', caseId);
  await deleteDoc(docRef);
}