'use client';

import { useEffect } from 'react';

export function DynamicFavicon() {
  useEffect(() => {
    // Check for custom favicon from admin settings
    const checkFavicon = async () => {
      try {
        const res = await fetch('/api/admin/settings');
        const data = await res.json();
        if (data.success && data.flat) {
          if (data.flat.faviconUrl) {
            let link = document.querySelector("link[rel*='icon']") as HTMLLinkElement | null;
            if (!link) {
              link = document.createElement('link');
              link.rel = 'icon';
              document.head.appendChild(link);
            }
            link.href = data.flat.faviconUrl;
          }
          if (data.flat.appName && data.flat.appName !== 'Needyfy') {
            document.title = `${data.flat.appName} - Every Need, One Platform`;
          }
        }
      } catch { /* ignore */ }
    };
    checkFavicon();
  }, []);

  return null;
}
