"use client";

import { getGeneralSettings } from "@/services/cmsService";
import { createContext, useContext, useEffect, useState } from "react";

const GeneralSettingsContext = createContext(null);

export const GeneralSettingsProvider = ({ children }) => {
  const [settings, setSettings] = useState(null);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    const fetchSettings = async () => {
      try {
        const res = await getGeneralSettings();
        setSettings(res.data);
      } catch (err) {
        console.error("General settings error:", err);
      } finally {
        setLoading(false);
      }
    };

    fetchSettings();
  }, []);

  return (
    <GeneralSettingsContext.Provider value={{ settings, loading }}>
      {children}
    </GeneralSettingsContext.Provider>
  );
};

export const useGeneralSettings = () =>
  useContext(GeneralSettingsContext);