export interface ApiModule {
    uuid: string;
    name: string;
    course: string;
    eventType?: string;
    prof: string;
    semester: string;
    confidenceScore?: number;
}

export async function fetchProfessorModules(): Promise<ApiModule[]> {
    if (import.meta.env.SSR) {
        return [];
    }
    try {
        const { pb } = await import("../service/pocketbase");

        if (!pb.authStore.isValid || !pb.authStore.token) {
            return Promise.reject(new Error("User is not authenticated"));
        }

        const response = await fetch("/api/professor/modules", {
            method: "GET",
            headers: {
                "Authorization": pb.authStore.token,
                "Content-Type": "application/json",
            },
        });

        if (!response.ok) {
            const error = new Error(`HTTP error! status: ${response.status}`);
            Object.assign(error, { status: response.status });
            throw error;
        }

        return await response.json();
    } catch (error) {
        return Promise.reject(error);
    }
}
