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

const StudentDashboardPracticePage: Component = () => {
	const auth = useAuth();
	const studentId = createMemo(() => (auth.isReady() && auth.user()?.role === "student" ? auth.user()!.id : null));
	const [practiceData] = createResource(studentId, getDashboardPracticeData);
	const shellData = createMemo(() => practiceData()?.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 practice view is for student accounts.</section>}>
					<Show when={!practiceData.error} fallback={<section>We could not load your live practice view right now.</section>}>
						<Show when={practiceData()} keyed fallback={<section>Loading your live practice plan from the backend…</section>}>
							{(data) => <DashboardPracticeFocus {...data} />}
						</Show>
					</Show>
				</Show>
			</Show>
		</DashboardShell>
	);
};

export default StudentDashboardPracticePage;
