import type { Component } from "solid-js";
import { Show, createMemo, createResource } from "solid-js";
import DashboardProgressFocus from "../../../components/dashboard/student/dashboard-progress-focus";
import { getDashboardProgressData } from "../../../components/dashboard/student/dashboard-progress.data";
import DashboardShell from "../../../components/dashboard/shared/dashboard-shell";
import { useAuth } from "~/context/auth/context";

const StudentDashboardProgressPage: Component = () => {
	const auth = useAuth();
	const studentId = createMemo(() => (auth.isReady() && auth.user()?.role === "student" ? auth.user()!.id : null));
	const [progressData] = createResource(studentId, getDashboardProgressData);
	const shellData = createMemo(() => progressData()?.shell);

	return (
		<DashboardShell sidebarData={shellData()} topbarData={shellData()}>
			<Show when={auth.isReady()} fallback={<section>Loading your dashboard session…</section>}>
				<Show when={auth.user()?.role === "student"} fallback={<section>This progress view is for student accounts.</section>}>
					<Show when={!progressData.error} fallback={<section>We could not load your live progress view right now.</section>}>
						<Show when={progressData()} keyed fallback={<section>Loading your live progress from the backend…</section>}>
							{(data) => <DashboardProgressFocus {...data} />}
						</Show>
					</Show>
				</Show>
			</Show>
		</DashboardShell>
	);
};

export default StudentDashboardProgressPage;
