import { useNavigate } from "@solidjs/router";
import type { Component } from "solid-js";
import { Show, createEffect } from "solid-js";
import { useAuth } from "../../context/auth/context";
import { getDashboardHomeHref, getDashboardProgressHref } from "../../lib/routes";

const DashboardProgressPage: Component = () => {
	const auth = useAuth();
	const navigate = useNavigate();

	createEffect(() => {
		if (!auth.isReady()) return;
		const user = auth.user();
		if (!user) return;
		const nextHref = user.role === "student" ? getDashboardProgressHref() : getDashboardHomeHref("teacher");
		void navigate(nextHref, { replace: true });
	});

	return (
		<Show when={auth.isReady()} fallback={<section>Loading your dashboard session…</section>}>
			<section>Redirecting to the right progress view…</section>
		</Show>
	);
};

export default DashboardProgressPage;
