// import {
//   createContext,
//   useContext,
//   useState,
//   useEffect,
//   ReactNode,
// } from "react";
// import {
//   signInWithPopup,
//   signOut,
//   onAuthStateChanged,
//   GoogleAuthProvider,
//   createUserWithEmailAndPassword,
//   signInWithEmailAndPassword,
//   sendPasswordResetEmail,
//   sendEmailVerification,
// } from "firebase/auth";

// import {doc, setDoc,getDoc} from "firebase/firestore";

// import { auth,db } from "@/db/config";


// interface User {
//   displayName: any | "User";
//   photoURL: string | undefined;
//   uid: string;
//   email: string;
//   emailVerified: boolean;
// }

// interface AuthContextType {
//   user: User | null;
//   googleSignIn: () => void;
//   emailSignIn: (email: string, password: string) => void;
//   createUser: (email: string, password: string,fullName:string) => void;
//   resetPassword: (email: string) => void;
//   logOut: () => void;
// }

// export const AuthContext = createContext<AuthContextType>({
//   user: null,
//   googleSignIn: () => {},
//   emailSignIn: () => {},
//   createUser: () => {},
//   resetPassword: () => {},
//   logOut: () => {},
// });

// export const AuthContextProvider = ({ children }: { children: ReactNode }) => {
//   const [user, setUser] = useState<User | null>(null);

//   const googleSignIn = async () => {
//     const provider = new GoogleAuthProvider();
//     const newUser = await signInWithPopup(auth, provider);
//     const userRef = doc(db, "users", newUser.user.uid);
//     const userSnap = await getDoc(userRef);
//     if (!userSnap.exists()) {
//       await setDoc(userRef, {
//         email: newUser.user.email,
//         name: newUser.user.displayName,
//         photoURL: newUser.user.photoURL,
//         mobileNumber: null,
//       });
//     }

//   };

//   const emailSignIn = async (email: string, password: string) => {
//     try {
//       const userCredential = await signInWithEmailAndPassword(
//         auth,
//         email,
//         password
//       );
//       const user = userCredential.user;
//       if (!user.emailVerified) {
//         await signOut(auth);
//         throw new Error("Email not verified. Please verify your email first and then try to log in.");
//       }
//     } catch (error: any) {
//       if (error.message === "Firebase: Error (auth/invalid-credential).") {
//         throw new Error(
//           "User not found. Please check your email and password again or cretae your new Barrack account."
//         );
//       } else {
//         throw new Error("Failed to Sign In: " + error.message);
//       }
//     }
//   };

//   const createUser = async (email: string, password: string,fullName:string) => {
//     try {
//         console.log("Heat signup 1");
//         const newUserCredentials = await createUserWithEmailAndPassword(
//           auth,
//           email,
//           password
//         );
//         console.log("Heat signup 2");
//         await sendEmailVerification(newUserCredentials.user as any);
//         const userRef = doc(db, "users", newUserCredentials.user.uid);
//         const userSnap = await getDoc(userRef);
//         if (!userSnap.exists()) {
//           await setDoc(userRef, {
//             id: newUserCredentials.user.uid,
//             email: newUserCredentials.user.email,
//             name: fullName,
//             image: null,
//             role: "user",
//             mobile: null,
//           });
//         }
//     } catch (error: any) {
//       if (error.code === "auth/email-already-in-use") {
//         throw new Error(
//           "Email is already in use. Please use a different email."
//         );
//       } else {
//         throw new Error("Failed to create user: " + error.message);
//       }
//     }
//   };


//   const resetPassword = async (email: string) => {
//     try {
//      await sendPasswordResetEmail(auth, email);
//     }catch(error:any){
//         throw new Error("Failed to reset password."+error.message);
//     }
//   }

//   const logOut = async () => {
//     await signOut(auth);
//   };

//   useEffect(() => {
//     const unsubscribe = onAuthStateChanged(auth, (currentUser) => {
//       if (currentUser && currentUser.emailVerified) {
//         setUser(currentUser as User);
//       } else {
//         setUser(null);
//       }
//     });
//     return () => unsubscribe();
//   }, []);



//   return (
//     <AuthContext.Provider value={{ user, googleSignIn, logOut, emailSignIn,createUser, resetPassword }}>
//       {children}
//     </AuthContext.Provider>
//   );
// };

// export const UserAuth = () => {
//   return useContext(AuthContext);
// };
