import {createAdminClient} from "@/lib/backend/server-appwrite";
import {Databases, Query} from "node-appwrite";
import {PatientProfile, UserProfile} from "@/lib/backend/appwrite_types";

export async function fetchPatientProfile(id: string) {
    const {client} = await createAdminClient();
    const databases = new Databases(client);
    let response = await databases.listDocuments(
        process.env.NEXT_APPWRITE_DATABASE_ID!,
        process.env.NEXT_APPWRITE_PATIENTS_COLLECTION_ID!,
        [Query.equal('userId', id)],
        1
    )
    if (response.total > 0 && response.documents.length > 0)
        return response.documents[0] as PatientProfile
    else
        throw Error("User profile not found.")
}