"use client";

import { ThemeProvider } from "next-themes";
import { useEffect } from "react";
import Cookies from "js-cookie";
import { setAccessToken } from "@/lib/tokenStore";

export function Providers({ children }) {
  useEffect(() => {
    if (!Cookies.get("userInfo")) return;
    fetch("/api/auth/refresh", { method: "POST" })
      .then((res) => (res.ok ? res.json() : null))
      .then((data) => {
        if (data?.access_token) setAccessToken(data.access_token);
      })
      .catch(() => {});
  }, []);

  return (
    <ThemeProvider
      attribute="data-"
      defaultTheme="system"
      enableSystem
    >
      {children}
    </ThemeProvider>
  );
}
