"use client";

import { useRouter } from "next/navigation";
import { useEffect } from "react";
import { AppShell } from "@/components/app/AppShell";
import { Alert } from "@/components/ui/Alert";
import { Button } from "@/components/ui/Button";
import { isTeacherRole } from "@/lib/permissions";
import { getActiveBranchId } from "@/lib/session-context";

export default function MySchoolClassesPage() {
  const router = useRouter();
  const branchId = getActiveBranchId();
  const teacher = isTeacherRole();

  useEffect(() => {
    if (teacher) {
      router.replace("/dashboard");
      return;
    }
    if (branchId) {
      router.replace(`/schools/${branchId}/classes`);
    }
  }, [branchId, router, teacher]);

  if (teacher) {
    return (
      <AppShell title="کلاس‌های من" subtitle="در حال انتقال…">
        <p className="text-sm text-[var(--muted)]">انتقال به داشبورد معلم…</p>
      </AppShell>
    );
  }

  if (branchId) {
    return (
      <AppShell title="کلاس‌های من" subtitle="در حال انتقال…">
        <p className="text-sm text-[var(--muted)]">انتقال به صفحه کلاس‌های مدرسه…</p>
      </AppShell>
    );
  }

  return (
    <AppShell title="کلاس‌های من" subtitle="انتخاب مدرسه فعال">
      <Alert tone="warning">
        برای مشاهده کلاس‌ها ابتدا نقش و مدرسه فعال را از صفحه انتخاب زمینه مشخص کنید.
      </Alert>
      <div className="mt-4">
        <Button onClick={() => router.push("/select-context")}>انتخاب زمینه</Button>
      </div>
    </AppShell>
  );
}
