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, getDashboardPracticeHref } from "../../lib/routes";

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

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

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

export default DashboardPracticePage;
