import type { Component } from "solid-js";
import { Show, createMemo, createResource } from "solid-js";
import DashboardShell from "../../../components/dashboard/shared/dashboard-shell";
import DashboardTeacherHome from "../../../components/dashboard/teacher/dashboard-teacher-home";
import { getTeacherDashboardHomeData } from "../../../components/dashboard/teacher/dashboard-teacher-home.data";
import { useAuth } from "~/context/auth/context";

const TeacherDashboardPage: Component = () => {
	const auth = useAuth();
	const teacherId = createMemo(() => (auth.isReady() && auth.user()?.role === "teacher" ? auth.user()!.id : null));
	const [teacherHomeData] = createResource(teacherId, getTeacherDashboardHomeData);
	const shellData = createMemo(() => teacherHomeData()?.shell);

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

export default TeacherDashboardPage;
