import { useWebAuth } from "@code/auth/web";
import { Navigate, Outlet, useLocation } from "react-router";

export default function AppLayout() {
  const auth = useWebAuth();
  const location = useLocation();

  if (auth.status === "loading") {
    return (
      <div className="grid min-h-svh place-items-center text-sm text-muted-foreground">
        Loading…
      </div>
    );
  }

  if (auth.status === "inconsistent") {
    return (
      <div className="grid min-h-svh place-items-center text-sm text-muted-foreground">
        Your session could not be verified
      </div>
    );
  }

  if (auth.status === "unauthenticated") {
    return <Navigate replace state={{ from: location.pathname }} to="/login" />;
  }

  return <Outlet />;
}
