// src/utils/StartupSync.js
import React, { useEffect } from "react";
import moduleRoutes from "../Modules/ModuleRoutes";
import { syncPermissions } from "./permissions";


const StartupSync = ({ children }) => {
  useEffect(() => {
    const runSync = async () => {
      try {
        const token = localStorage.getItem("accessToken");
        const user = JSON.parse(localStorage.getItem("user"));

        if (!token || !user) {
          console.log("No user or token found; skipping sync");
          return;
        }

        console.log("Running permissions sync for user:", user.email);

        await syncPermissions(moduleRoutes, token);

        console.log("Permissions sync completed");
      } catch (error) {
        console.error("Permissions sync failed in background", error);
      }
    };

    runSync();
  }, []);

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


export default StartupSync;
