import { app } from "@/firebase";

import {
  collection,
  doc,
  getDoc,
  getDocs,
  getFirestore,
  query,
  where,
} from "firebase/firestore";
import { User } from "@/interfaces/user";
const db = getFirestore(app);

export async function getUsers() {
  const snapshot = await getDocs(collection(db, "users"));

  return snapshot.docs.map((doc) => ({
    id: doc.id,
    ...doc.data(),
  })) as User[];
}

export async function getUser(id: string) {
  const userDoc = await getDoc(doc(db, "users", id));

  return { ...userDoc.data(), id } as User;
}

export async function getSpecificUsers(ids: string[]) {
  if (ids.length === 0) return [];

  const snapshot = await getDocs(
    query(collection(db, "users"), where("id", "in", ids))
  );

  const groups = snapshot.docs.map((doc) => ({
    id: doc.id,
    ...doc.data(),
  })) as User[];

  return groups;
}
