(function () {
  'use strict';

  var CONFIG_URL = (window.RESOURCES_CONFIG_URL || (window.location.pathname.replace(/\/[^\/]*$/, '') + '/resources-site-config.php'));

  function applySiteBranding(config) {
    var siteName = (config && config.site_name) || 'Mutalex Academy';
    var logo = (config && config.site_logo) || null;
    var homeUrl = (config && config.home_url) || '/home';

    // Update any element flagged as a site name holder.
    document.querySelectorAll('[data-site-name]').forEach(function (el) {
      el.textContent = siteName;
    });

    // Update any element flagged as a site logo holder.
    document.querySelectorAll('[data-site-logo]').forEach(function (el) {
      renderLogo(el, logo, siteName);
    });

    // Wire logo links / anchors to the configured home url when requested.
    document.querySelectorAll('[data-site-home]').forEach(function (el) {
      el.setAttribute('href', homeUrl);
    });
  }

  function renderLogo(el, logo, siteName) {
    el.innerHTML = '';
    el.setAttribute('aria-hidden', 'true');

    if (logo && logo.type === 'svg' && logo.svg) {
      el.innerHTML = logo.svg;
    } else if (logo && logo.type === 'image' && logo.src) {
      var img = document.createElement('img');
      img.src = logo.src;
      img.alt = siteName;
      img.className = 'site-logo';
      el.appendChild(img);
    } else if (logo && logo.icon) {
      var i = document.createElement('i');
      i.className = logo.icon;
      el.appendChild(i);
    } else {
      var fallback = document.createElement('i');
      fallback.className = 'fas fa-newspaper';
      el.appendChild(fallback);
    }
  }

  function injectBrandingStyles() {
    if (document.getElementById('resources-site-branding-styles')) return;

    var css = [
      '[data-site-logo] { display: inline-flex; align-items: center; justify-content: center; min-width: 48px; min-height: 48px; width: auto; height: auto; }',
      '[data-site-logo] img.site-logo { width: 48px; height: 48px; object-fit: contain; border-radius: 10px; }',
      '[data-site-name] { display: inline-flex; align-items: center; font-weight: 700; font-size: 1rem; line-height: 1.2; color: inherit; }',
      '[data-site-name] span { display: block; font-size: 0.85em; font-weight: 500; opacity: 0.8; }',
      '[data-site-home] { display: inline-flex; align-items: center; gap: 0.75rem; text-decoration: none; color: inherit; }',
      '.logo-text, .brand, .logo-area { display: inline-flex; align-items: center; gap: 0.75rem; }',
      '.logo-icon { display: inline-flex; align-items: center; justify-content: center; min-width: 48px; min-height: 48px; width: 48px; height: 48px; }'
    ].join('\n');

    var style = document.createElement('style');
    style.id = 'resources-site-branding-styles';
    style.type = 'text/css';
    style.appendChild(document.createTextNode(css));
    document.head.appendChild(style);
  }

  function fetchConfig() {
    return fetch(CONFIG_URL, { credentials: 'same-origin' })
      .then(function (res) {
        if (!res.ok) throw new Error('Failed to load ' + CONFIG_URL);
        return res.json();
      });
  }

  function init() {
    injectBrandingStyles();
    fetchConfig()
      .then(applySiteBranding)
      .catch(function (err) {
        console.error('[resources-header]', err);
        // Fall back to whatever static defaults already exist in the markup.
      });
  }

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', init);
  } else {
    init();
  }
})();
