export type PortalSurface = 'admin' | 'member' | 'core';

const MEMBER_HOSTS = new Set(['secure.d-bis.org', 'members.d-bis.org']);
const CORE_HOSTS = new Set(['core.d-bis.org']);

function getSurfaceCopy(surface: PortalSurface) {
  switch (surface) {
    case 'member':
      return {
        pageTitle: 'DBIS Member Portal',
        sidebarTitle: 'DBIS Member',
        topbarTitle: 'DBIS Member Portal',
        loginTitle: 'DBIS Member Portal',
        loginSubtitle: 'Secure institution access for submissions, requests, reporting, and member services',
        homeRoute: '/portal/overview',
      };
    case 'core':
      return {
        pageTitle: 'DBIS Core Banking Application',
        sidebarTitle: 'DBIS Core',
        topbarTitle: 'DBIS Core Banking',
        loginTitle: 'DBIS Core Banking Application',
        loginSubtitle: 'Operational access to the DBIS Core banking application and institution workflows',
        homeRoute: '/dbis/overview',
      };
    default:
      return {
        pageTitle: 'DBIS Admin Console',
        sidebarTitle: 'DBIS Admin',
        topbarTitle: 'DBIS Admin Console',
        loginTitle: 'DBIS Admin Console',
        loginSubtitle: 'Operations access for DBIS and sovereign central bank administration',
        homeRoute: '/dbis/overview',
      };
  }
}

function detectSurface(): PortalSurface {
  if (typeof window === 'undefined') {
    return 'admin';
  }

  const host = window.location.hostname.toLowerCase();
  if (MEMBER_HOSTS.has(host)) {
    return 'member';
  }

  if (CORE_HOSTS.has(host)) {
    return 'core';
  }

  return 'admin';
}

const surface = detectSurface();
const surfaceCopy = getSurfaceCopy(surface);

export const runtimePortal = {
  surface,
  isAdmin: surface === 'admin',
  isMember: surface === 'member',
  isCore: surface === 'core',
  host: typeof window === 'undefined' ? '' : window.location.hostname.toLowerCase(),
  pageTitle: surfaceCopy.pageTitle,
  sidebarTitle: surfaceCopy.sidebarTitle,
  topbarTitle: surfaceCopy.topbarTitle,
  loginTitle: surfaceCopy.loginTitle,
  loginSubtitle: surfaceCopy.loginSubtitle,
  homeRoute: surfaceCopy.homeRoute,
} as const;
