"use client";

import { useEffect, useState } from "react";
import { defaultSiteProfile, type SiteProfile } from "@/lib/site-profile";

export function useSiteProfile() {
  const [profile, setProfile] = useState<SiteProfile>(defaultSiteProfile);

  useEffect(() => {
    let mounted = true;
    fetch("/api/cms/site-profile")
      .then((response) => response.json())
      .then((data) => {
        if (!mounted) return;
        if (data?.ok && data?.profile) {
          setProfile({ ...defaultSiteProfile, ...data.profile });
        }
      })
      .catch(() => {
        // keep fallback defaults
      });

    return () => {
      mounted = false;
    };
  }, []);

  return profile;
}
