import useUserStore from "@/shared/hooks/user";
import { removeAuthToken, removeRefAuthToken } from "@/shared/lib/authCookies";
import React from "react";
import { Navigate } from "react-router-dom";
import { toast } from "sonner";

const allowedRoles = [
  "superuser",
  "admin",
  "moderator",
  "tour_admin",
  "buxgalter",
  "operator",
];

const ProtectedRoute = ({ children }: { children: React.ReactNode }) => {
  const { user } = useUserStore();

  if (!user) {
    return <Navigate to="/login" replace />;
  }

  if (!allowedRoles.includes(user.role)) {
    toast.error("Kirishga huquq yo‘q!", {
      richColors: true,
      position: "top-center",
    });
    removeAuthToken();
    removeRefAuthToken();
    return <Navigate to="/login" replace />;
  }

  return <>{children}</>;
};

export default ProtectedRoute;
