/* Phase 1 network pages: window-style iframe for explicit tool links. */
(function(){
  'use strict';

  var body = document.body;
  if (!body || !body.classList.contains('ua-shell-chrome')) return;

  var pages = [
    'ua-p-routers',
    'ua-p-sectors',
    'ua-p-subscribers-distribution',
    'ua-p-customer-connect',
    'ua-p-wizard-network',
    'ua-p-mikrotik',
    'ua-p-mikrotik-import'
  ];
  var active = body.classList.contains('ua-sec-mtk') || pages.some(function(cls){ return body.classList.contains(cls); });
  if (!active) return;

  var modal = null;
  var frame = null;
  var title = null;

  function localPhpUrl(href){
    try {
      var u = new URL(href, location.href);
      if (u.origin !== location.origin) return null;
      if (!/\.php$/i.test(u.pathname.split('/').pop() || '')) return null;
      return u;
    } catch(e) {
      return null;
    }
  }

  function framedHref(href){
    var u = localPhpUrl(href);
    if (!u) return '';
    u.searchParams.set('embed', '1');
    u.searchParams.set('ua_modal', '1');
    return u.pathname.split('/').pop() + u.search + u.hash;
  }

  function ensureModal(){
    if (modal) return modal;
    modal = document.createElement('div');
    modal.className = 'ua-net-window';
    modal.setAttribute('aria-hidden', 'true');
    modal.innerHTML =
      '<div class="ua-net-window__box" role="dialog" aria-modal="true">' +
        '<div class="ua-net-window__head">' +
          '<div class="ua-net-window__title"></div>' +
          '<div class="ua-net-window__controls">' +
            '<button type="button" class="ua-net-window__btn ua-net-window__btn--close" data-ua-net-close title="Close" aria-label="Close">×</button>' +
            '<button type="button" class="ua-net-window__btn" data-ua-net-max title="Maximize" aria-label="Maximize">□</button>' +
          '</div>' +
        '</div>' +
        '<div class="ua-net-window__body"><iframe class="ua-net-window__frame" title="Network window"></iframe></div>' +
      '</div>';
    document.body.appendChild(modal);
    frame = modal.querySelector('.ua-net-window__frame');
    title = modal.querySelector('.ua-net-window__title');
    modal.addEventListener('click', function(ev){
      if (ev.target === modal || ev.target.closest('[data-ua-net-close]')) {
        closeWindow();
        ev.preventDefault();
        return;
      }
      if (ev.target.closest('[data-ua-net-max]')) {
        modal.classList.toggle('is-max');
        ev.preventDefault();
      }
    }, true);
    return modal;
  }

  function openWindow(href, label){
    var src = framedHref(href);
    if (!src) return false;
    ensureModal();
    if (title) title.textContent = label || 'Window';
    if (frame) frame.src = src;
    modal.classList.add('is-open');
    modal.setAttribute('aria-hidden', 'false');
    body.classList.add('ua-net-window-open');
    return true;
  }

  function closeWindow(){
    if (!modal) return;
    modal.classList.remove('is-open', 'is-max');
    modal.setAttribute('aria-hidden', 'true');
    body.classList.remove('ua-net-window-open');
    if (frame) frame.src = 'about:blank';
  }

  document.addEventListener('keydown', function(ev){
    if (ev.key === 'Escape' && modal && modal.classList.contains('is-open')) closeWindow();
  });

  document.addEventListener('click', function(ev){
    var a = ev.target.closest && ev.target.closest('a[href]');
    if (!a) return;
    var explicit = a.hasAttribute('data-ua-net-window');
    var legacyToolWindow = a.matches('a[target="_blank"].btn[href]');
    if (!explicit && !legacyToolWindow) return;
    if (!localPhpUrl(a.getAttribute('href') || '')) return;
    ev.preventDefault();
    ev.stopPropagation();
    openWindow(a.getAttribute('href') || '', a.getAttribute('data-title') || a.getAttribute('title') || a.textContent.trim() || 'Window');
  }, true);
})();
