// public/js/portal-login.js
import { patchFetchForBasePath, withBase } from './basePath.js?v={{APP_QVER}}';
import { t, applyTranslations, setLocale } from './i18n.js?v={{APP_QVER}}';

// Ensure /api/* calls work when FileRise is mounted under a subpath (e.g. /fr).
patchFetchForBasePath();

function getStoredLocale() {
  try { return localStorage.getItem('language') || ''; } catch (e) { /* ignore */ }
  return '';
}

function getSavedLocale() {
  return getStoredLocale() || 'en';
}

async function resolveLocale() {
  const stored = getStoredLocale();
  if (stored) return stored;
  try {
    const res = await fetch(withBase('/api/public/siteConfig.php'), { credentials: 'include' });
    const cfg = await res.json().catch(() => ({}));
    const defaultLang = cfg && cfg.display && cfg.display.defaultLanguage
      ? String(cfg.display.defaultLanguage)
      : '';
    if (defaultLang) {
      setSavedLocale(defaultLang);
      return defaultLang;
    }
  } catch (e) { /* ignore */ }
  return 'en';
}

function setSavedLocale(locale) {
  if (!locale) return;
  try { localStorage.setItem('language', locale); } catch (e) { /* ignore */ }
}

async function applyLocale() {
  const saved = await resolveLocale();
  const applied = await setLocale(saved);
  applyTranslations();
  document.documentElement.lang = applied;
}

function setupLanguageSelect() {
  const select = document.getElementById('portalLoginLangSelect');
  if (!select) return;

  const options = Array.from(select.options).map(opt => opt.value);
  const saved = getSavedLocale();
  select.value = options.includes(saved) ? saved : 'en';

  select.addEventListener('change', async () => {
    const next = select.value || 'en';
    setSavedLocale(next);
    const applied = await setLocale(next);
    applyTranslations();
    document.documentElement.lang = applied;
  });
}

// -------- URL helpers --------
function sanitizeRedirect(raw, { fallback = '/' } = {}) {
  if (!raw) return fallback;
  try {
    const str = String(raw).trim();
    if (!str) return fallback;

    // Resolve against current page so subpath mounts (e.g. /fr) are preserved
    const candidate = new URL(str, window.location.href);

    // 1) Must stay on the same origin
    if (candidate.origin !== window.location.origin) {
      return fallback;
    }

    // 2) Only allow http/https
    if (candidate.protocol !== 'http:' && candidate.protocol !== 'https:') {
      return fallback;
    }

    // Return a relative URL (prevents host changes)
    return candidate.pathname + candidate.search + candidate.hash;
  } catch (e) {
    return fallback;
  }
}

function getRedirectTarget() {
  try {
    const url = new URL(window.location.href);
    const raw = url.searchParams.get('redirect');

    // Default fallback: root
    let target = sanitizeRedirect(raw, { fallback: withBase('/') });

    // If there was no *usable* redirect but we have a portal slug,
    // send them back to that portal by default.
    if (!target || target === '/') {
      const slug = getPortalSlugFromUrl();
      if (slug) {
        target = sanitizeRedirect(withBase('/portal/' + encodeURIComponent(slug)), { fallback: withBase('/') });
      }
    }

    return target || withBase('/');
  } catch (e) {
    return withBase('/');
  }
}
  
  function getPortalSlugFromUrl() {
    try {
      const url = new URL(window.location.href);
  
      // 1) Direct ?slug=portal-xxxxx on login page (if ever used)
      let slug = url.searchParams.get('slug');
      if (slug && slug.trim()) {
        console.log('portal-login: slug from top-level param =', slug.trim());
        return slug.trim();
      }
  
      // 2) From redirect param: may be portal.html?slug=... or /portal/<slug>
      const redirect = url.searchParams.get('redirect');
      if (redirect) {
        console.log('portal-login: raw redirect param =', redirect);
  
        try {
          const redirectUrl = new URL(redirect, window.location.origin);
  
          // 2a) ?slug=... in redirect
          const innerSlug = redirectUrl.searchParams.get('slug');
          if (innerSlug && innerSlug.trim()) {
            console.log('portal-login: slug from redirect URL =', innerSlug.trim());
            return innerSlug.trim();
          }
  
          // 2b) Pretty path /portal/<slug> in redirect
          const pathMatch = redirectUrl.pathname.match(/\/portal\/([^\/?#]+)/i);
          if (pathMatch && pathMatch[1]) {
            const fromPath = pathMatch[1].trim();
            console.log('portal-login: slug from redirect path =', fromPath);
            return fromPath;
          }
        } catch (err) {
          console.warn('portal-login: failed to parse redirect URL', err);
        }
  
        // 2c) Fallback regex on redirect string
        const m = redirect.match(/[?&]slug=([^&]+)/);
        if (m && m[1]) {
          const decoded = decodeURIComponent(m[1]).trim();
          console.log('portal-login: slug from redirect regex =', decoded);
          return decoded;
        }
      }
  
      // 3) Legacy fallback on current query string
      const qs = window.location.search || '';
      const m2 = qs.match(/[?&]slug=([^&]+)/);
      if (m2 && m2[1]) {
        const decoded2 = decodeURIComponent(m2[1]).trim();
        console.log('portal-login: slug from own query regex =', decoded2);
        return decoded2;
      }
  
      console.log('portal-login: no slug found');
      return '';
    } catch (err) {
      console.warn('portal-login: getPortalSlugFromUrl error', err);
      const qs = window.location.search || '';
      const m = qs.match(/[?&]slug=([^&]+)/);
      return m && m[1] ? decodeURIComponent(m[1]).trim() : '';
    }
  }
  
  // --- CSRF helpers (same pattern as portal.js) ---
  function setCsrfToken(token) {
    if (!token) return;
    window.csrfToken = token;
    try {
      localStorage.setItem('csrf', token);
    } catch (e) { /* ignore */ }
  
    let meta = document.querySelector('meta[name="csrf-token"]');
    if (!meta) {
      meta = document.createElement('meta');
      meta.name = 'csrf-token';
      document.head.appendChild(meta);
    }
    meta.content = token;
  }
  
  function getCsrfToken() {
    return (
      window.csrfToken ||
      (document.querySelector('meta[name="csrf-token"]')?.content) ||
      ''
    );
  }
  
  async function loadCsrfToken() {
    try {
      const res = await fetch('/api/auth/token.php', {
        method: 'GET',
        credentials: 'include'
      });
  
      const hdr = res.headers.get('X-CSRF-Token');
      if (hdr) setCsrfToken(hdr);
  
      let body = {};
      try {
        body = await res.json();
      } catch (e) {
        body = {};
      }
  
      const token = body.csrf_token || getCsrfToken();
      setCsrfToken(token);
    } catch (e) {
      console.warn('portal-login: failed to load CSRF token', e);
    }
  }
  
  // --- UI helpers ---
  function showError(msg) {
    const box = document.getElementById('portalLoginError');
    if (!box) return;
    box.textContent = msg || t('portal_login_error_default') || 'Login failed.';
    box.classList.add('show');
  }
  
  function clearError() {
    const box = document.getElementById('portalLoginError');
    if (!box) return;
    box.textContent = '';
    box.classList.remove('show');
  }
  
  // -------- Portal meta (title + accent) --------
  async function fetchPortalMeta(slug) {
    if (!slug) return null;
    console.log('portal-login: calling publicMeta.php for slug', slug);
    try {
      const res = await fetch(
        '/api/pro/portals/publicMeta.php?slug=' + encodeURIComponent(slug),
        { method: 'GET', credentials: 'include' }
      );
      const text = await res.text();
      let data = {};
      try {
        data = text ? JSON.parse(text) : {};
      } catch (e) {
        data = {};
      }
      if (!res.ok || !data || !data.success || !data.portal) {
        console.warn('portal-login: publicMeta not ok', res.status, data);
        return null;
      }
      return data.portal;
    } catch (e) {
      console.warn('portal-login: failed to load portal meta', e);
      return null;
    }
  }
  
  function applyPortalBranding(portal) {
    if (!portal) return;

    const title =
      (portal.title && portal.title.trim()) ||
      portal.label ||
      portal.slug ||
      (t('portal_label_default') || 'Client portal');
  
    const headingEl  = document.getElementById('portalLoginTitle');
    const subtitleEl = document.getElementById('portalLoginSubtitle');
    const footerEl   = document.getElementById('portalLoginFooter');
    const logoEl     = document.getElementById('portalLoginLogo');
  
    if (headingEl) {
      headingEl.textContent = t('portal_login_heading_with_title', { title }) || ('Sign in to ' + title);
    }
    if (subtitleEl) {
      subtitleEl.textContent = t('portal_login_subtitle') || 'to access this client portal';
    }
  
    // Footer text from portal metadata, if provided
    if (footerEl) {
      const ft = (portal.footerText && portal.footerText.trim()) || '';
      if (ft) {
        footerEl.textContent = ft;
        footerEl.style.display = 'block';
      } else {
        footerEl.textContent = '';
        footerEl.style.display = 'none';
      }
    }

    // 🔹 Portal logo: use logoFile from metadata if present
    if (logoEl) {
    const buildPortalLogoUrl = (fileName) =>
      fileName ? `/api/public/profilePic.php?file=${encodeURIComponent(fileName)}` : '';

    let logoSrc = '';

    // If you ever decide to store a direct URL:
    if (portal.logoUrl && portal.logoUrl.trim()) {
      logoSrc = portal.logoUrl.trim();
    } else if (portal.logoFile && portal.logoFile.trim()) {
      logoSrc = buildPortalLogoUrl(portal.logoFile.trim());
    }

    const legacyMatch = logoSrc.match(/\/uploads\/profile_pics\/([^?#]+)/);
    if (legacyMatch && legacyMatch[1]) {
      let legacyName = legacyMatch[1];
      try { legacyName = decodeURIComponent(legacyName); } catch (e) {}
      logoSrc = buildPortalLogoUrl(legacyName);
    }

    if (logoSrc && logoSrc.startsWith('/')) {
      logoSrc = withBase(logoSrc);
    }

    if (logoSrc) {
      logoEl.src = logoSrc;
      logoEl.alt = title;
    }
    }
  
    // Document title
    try {
      document.title = t('portal_login_doc_title', { title }) || ('Sign in – ' + title);
    } catch (e) { /* ignore */ }
  
    // Accent: portal brandColor -> CSS var
    const brand = portal.brandColor && portal.brandColor.trim();
    if (brand) {
      document.documentElement.style.setProperty('--portal-accent', brand);
    }
  
    // Reapply card/button accent after we know portal color
    applyAccentFromTheme();
  }
  
  // --- Accent (card + button) ---
  function applyAccentFromTheme() {
    const card = document.querySelector('.portal-login-card');
    const btn  = document.getElementById('portalLoginSubmit');
    const rootStyles = getComputedStyle(document.documentElement);
  
    // Prefer per-portal accent if present
    let accent = rootStyles.getPropertyValue('--portal-accent').trim();
    if (!accent) {
      accent = rootStyles.getPropertyValue('--filr-accent-500').trim() || '#0b5ed7';
    }
  
    if (card) {
      card.style.borderTop = `3px solid ${accent}`;
    }
    if (btn) {
      btn.style.backgroundColor = accent;
      btn.style.borderColor = accent;
    }
  
    const metaTheme = document.querySelector('meta[name="theme-color"]');
    if (metaTheme) {
      metaTheme.setAttribute('content', accent);
    }
  }
  
  // --- Login call (JSON -> auth.php) ---
  async function doLogin(username, password) {
    const csrf = getCsrfToken() || '';
  
    const payload = {
      username,
      password
    };
    if (csrf) {
      payload.csrf_token = csrf;
    }
  
    const res = await fetch('/api/auth/auth.php', {
      method: 'POST',
      credentials: 'include',
      headers: {
        'X-CSRF-Token': csrf,
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(payload)
    });
  
    const text = await res.text();
    let body = {};
    try {
      body = text ? JSON.parse(text) : {};
    } catch (e) {
      body = {};
    }
  
    if (!res.ok) {
      const msg = body.error || body.message || text || 'Login failed.';
      const err = new Error(msg);
      err.status = res.status;
      throw err;
    }
  
    if (body.success === false || body.error || body.logged_in === false) {
      throw new Error(body.error || 'Invalid username or password.');
    }
  
    return body;
  }
  
  // --- Init ---
document.addEventListener('DOMContentLoaded', async () => {
  const form   = document.getElementById('portalLoginForm');
  const userEl = document.getElementById('portalLoginUser');
  const passEl = document.getElementById('portalLoginPass');
  const btn    = document.getElementById('portalLoginSubmit');

  await applyLocale();
  setupLanguageSelect();
  
    // Accent first (fallback to global accent)
    applyAccentFromTheme();
  
    // Try to load portal meta (title + brand color) using slug
    const slug = getPortalSlugFromUrl();
    console.log('portal-login: computed slug =', slug);
    if (slug) {
      fetchPortalMeta(slug).then(portal => {
        if (portal) {
          console.log('portal-login: got portal meta for', slug, portal);
          applyPortalBranding(portal);
        }
      });
    }
  
    // Pre-load CSRF (for auth.php)
    loadCsrfToken().catch(() => {});
  
    if (!form || !userEl || !passEl || !btn) return;
  
    // Focus username
    userEl.focus();
  
    form.addEventListener('submit', async (e) => {
      e.preventDefault();
      clearError();
  
      const username = userEl.value.trim();
      const password = passEl.value;
  
    if (!username || !password) {
        showError(t('portal_login_error_required') || 'Username and password are required');
        return;
      }

      btn.disabled = true;
      btn.textContent = t('portal_login_signing_in') || 'Signing in…';
  
      try {
        await doLogin(username, password);
        const target = getRedirectTarget();
        window.location.href = target;
      } catch (err) {
        console.error('portal-login: auth failed', err);
        showError(err.message || t('portal_login_error_failed_retry') || 'Login failed. Please try again.');
        btn.disabled = false;
        btn.textContent = t('portal_login_sign_in') || 'Sign in';
      }
    });
  });
