import { collection } from "../firebase";
import type { UserProfile } from "./authServices";

export class UserSearchService {
  static async findUserByEmail(email: string): Promise<UserProfile | null> {
    const trimmed = email.trim().toLowerCase();
    if (!trimmed) return null;

    try {
      const usersRef = collection("users");
      const querySnapshot = await usersRef
        .where("email", "==", trimmed)
        .limit(1)
        .get();

      if (querySnapshot.empty) {
        return null;
      }

      const docSnap = querySnapshot.docs[0];
      return (docSnap.data ? docSnap.data() : docSnap.data()) as UserProfile;
    } catch (error) {
      console.error("[UserSearchService] Error searching user by email", error);
      return null;
    }
  }
}

export default UserSearchService;
