/**
 * Shared white-label branding loader for auth/static pages.
 * Fetches /api/settings and applies branding (logo, favicon, title,
 * theme colour, brand-name placeholders) on any page that includes it.
 */
(function () {
  fetch('/api/settings')
    .then(function (r) { return r.ok ? r.json() : null; })
    .then(function (data) {
      var b = (data && data.branding) || {};

      // Logo images (class="logo" or class="topbar-logo")
      if (b.logo) {
        document.querySelectorAll('img.logo, img.topbar-logo, .hdr-logo').forEach(function (img) {
          img.src = b.logo;
          if (b.siteName) img.alt = b.siteName;
        });
        var fav = document.querySelector('link[rel="icon"]');
        if (!fav) { fav = document.createElement('link'); fav.rel = 'icon'; document.head.appendChild(fav); }
        fav.href = b.logo;
      }

      // Document title: swap the brand portion
      if (b.siteName) {
        document.title = document.title.replace(/BARE RADIO|Bare Radio|Pulse Chat/i, b.siteName);
        document.querySelectorAll('[data-brand-name]').forEach(function (el) {
          el.textContent = b.siteName;
        });
      }
      if (b.creditText) {
        document.querySelectorAll('.sidebar-credit, [data-brand-credit]').forEach(function (el) {
          el.textContent = b.creditText;
        });
      }

      // Theme accent colour
      if (b.themeColor) {
        document.documentElement.style.setProperty('--red', b.themeColor);
        document.documentElement.style.setProperty('--red-bright', b.themeColor);
      }
    })
    .catch(function () { /* keep hardcoded fallback */ });
})();
