const DEFAULT_ADMIN_HOST = "root.mohfarawati.de";
const DEFAULT_ADMIN_URL = `https://${DEFAULT_ADMIN_HOST}`;
const DEFAULT_SITE_URL = "https://mohfarawati.de";
const DEV_ADMIN_PREFIX = "/root";

export const INTERNAL_ADMIN_PREFIX = "/admin-internal";

function normalizePathname(pathname: string): string {
  if (!pathname || pathname === "/") {
    return "/";
  }

  return pathname.startsWith("/") ? pathname : `/${pathname}`;
}

function normalizeBaseUrl(url: string): string {
  return url.replace(/\/+$/, "");
}

function parseHostname(value: string | undefined): string | undefined {
  if (!value) {
    return undefined;
  }

  const trimmed = value.trim().toLowerCase();

  if (!trimmed) {
    return undefined;
  }

  try {
    return new URL(trimmed).hostname.toLowerCase();
  } catch {
    return trimmed.replace(/^https?:\/\//, "").split("/")[0]?.replace(/:\d+$/, "") || undefined;
  }
}

export function getAdminHost(): string {
  return (process.env.ADMIN_HOST ?? DEFAULT_ADMIN_HOST).trim().toLowerCase();
}

export function getSiteHost(): string {
  return parseHostname(process.env.NEXT_PUBLIC_SITE_URL ?? DEFAULT_SITE_URL) ?? "localhost";
}

export function getRequestHostname(hostHeader?: string | null): string {
  return (hostHeader ?? "")
    .split(",")[0]
    ?.trim()
    .toLowerCase()
    .replace(/:\d+$/, "") ?? "";
}

export function isAdminHost(hostname: string): boolean {
  return hostname === getAdminHost();
}

export function hasDedicatedAdminHost(): boolean {
  return getAdminHost() !== getSiteHost();
}

export function isLegacyAdminPath(pathname: string): boolean {
  return pathname === "/root" || pathname.startsWith("/root/");
}

export function isDevelopmentAdminPath(pathname: string): boolean {
  return pathname === DEV_ADMIN_PREFIX || pathname.startsWith(`${DEV_ADMIN_PREFIX}/`);
}

export function isInternalAdminPath(pathname: string): boolean {
  return pathname === INTERNAL_ADMIN_PREFIX || pathname.startsWith(`${INTERNAL_ADMIN_PREFIX}/`);
}

export function toInternalAdminPath(pathname: string): string {
  const normalizedPathname = normalizePathname(pathname);

  if (normalizedPathname === "/") {
    return INTERNAL_ADMIN_PREFIX;
  }

  return `${INTERNAL_ADMIN_PREFIX}${normalizedPathname}`;
}

export function fromDevelopmentAdminPath(pathname: string): string {
  if (pathname === DEV_ADMIN_PREFIX) {
    return "/";
  }

  if (pathname.startsWith(`${DEV_ADMIN_PREFIX}/`)) {
    return pathname.slice(DEV_ADMIN_PREFIX.length);
  }

  return pathname;
}

export function getAdminBaseUrl(): string {
  return normalizeBaseUrl(process.env.NEXT_PUBLIC_ADMIN_URL ?? DEFAULT_ADMIN_URL);
}

export function getAdminAppPath(pathname = "/"): string {
  const normalizedPathname = normalizePathname(pathname);

  if (process.env.NODE_ENV !== "production" && !hasDedicatedAdminHost()) {
    return normalizedPathname === "/"
      ? DEV_ADMIN_PREFIX
      : `${DEV_ADMIN_PREFIX}${normalizedPathname}`;
  }

  return normalizedPathname;
}

export function buildAdminUrl(pathname = "/"): string {
  return `${getAdminBaseUrl()}${getAdminAppPath(pathname)}`;
}

export function getSiteBaseUrl(): string {
  return normalizeBaseUrl(process.env.NEXT_PUBLIC_SITE_URL ?? DEFAULT_SITE_URL);
}

export function buildSiteUrl(pathname = "/"): string {
  return `${getSiteBaseUrl()}${normalizePathname(pathname)}`;
}
