import {app} from "@/firebase";
import {Assignment} from "@/interfaces/results";
import {collection, getDocs, getFirestore, query, where} from "firebase/firestore";

const db = getFirestore(app);

export const getAssignmentsByAssigner = async (id: string) => {
	const {docs} = await getDocs(query(collection(db, "assignments"), where("assigner", "==", id)));
	return docs.map((x) => ({...x.data(), id: x.id})) as Assignment[];
};

export const getAssignmentsByAssignerBetweenDates = async (id: string, startDate: Date, endDate: Date) => {
	const {docs} = await getDocs(query(collection(db, "assignments"), where("assigner", "==", id), ));
	return docs.map((x) => ({...x.data(), id: x.id})) as Assignment[];
};

export const getAssignmentsByAssigners = async (ids: string[]) => {
	return (await Promise.all(ids.map(getAssignmentsByAssigner))).flat();
};
