const value = (key: string): string => (process.env[key] || "").trim();

export const siteConfig = {
  rootDomain: value("NEXT_PUBLIC_ROOT_DOMAIN"),
  webmailHost: value("NEXT_PUBLIC_WEBMAIL_HOST"),
  webmailUrl: value("NEXT_PUBLIC_WEBMAIL_URL"),
  brandName: value("NEXT_PUBLIC_BRAND_NAME") || "Zimphony Webmail",
  brandTagline: value("NEXT_PUBLIC_BRAND_TAGLINE") || "Business email that grows with you",
  brandLogoUrl: value("NEXT_PUBLIC_BRAND_LOGO_URL") || "/api/branding/logo",
  brandDefaultLogoUrl: "/brand-logo.png",
  brandLogoAlt: value("NEXT_PUBLIC_BRAND_LOGO_ALT") || "Brand logo",
  brandFaviconUrl: value("NEXT_PUBLIC_BRAND_FAVICON_URL") || "/api/branding/logo",
  adminBrandDomain: value("NEXT_PUBLIC_ADMIN_BRAND_DOMAIN"),
  adminHelpText: value("NEXT_PUBLIC_ADMIN_HELP_TEXT") || "Help : +668 9493 3300",
  adminSubtitle: value("NEXT_PUBLIC_ADMIN_SUBTITLE") || "Tenant admin",
};

export function stripPort(host: string): string {
  return host.split(":")[0].toLowerCase();
}

export function buildHost(prefix: string, rootDomain: string): string {
  return rootDomain ? `${prefix}.${rootDomain}` : "";
}

export function resolveWebmailHost(tenant = ""): string {
  if (tenant) return `mail.${tenant}`;
  return siteConfig.webmailHost || buildHost("mail", siteConfig.rootDomain);
}

export function resolveManageHost(): string {
  return buildHost("manage", siteConfig.rootDomain);
}

export function resolveAdminHost(): string {
  return buildHost("admin", siteConfig.rootDomain);
}

export function resolveRootDomain(): string {
  return siteConfig.rootDomain;
}

export function resolveBrandDomainFromHost(host: string): string {
  const normalizedHost = stripPort(host || "");
  const rootDomain = stripPort(siteConfig.rootDomain || "");
  if (!normalizedHost) return "";
  if (rootDomain && normalizedHost === rootDomain) return rootDomain;
  if (rootDomain && normalizedHost === `mail.${rootDomain}`) return rootDomain;
  if (rootDomain && normalizedHost === `admin.${rootDomain}`) return rootDomain;
  if (rootDomain && normalizedHost === `manage.${rootDomain}`) return rootDomain;
  if (rootDomain && normalizedHost.endsWith(`.${rootDomain}`)) return rootDomain;
  for (const prefix of ["mail.", "admin.", "manage."]) {
    if (normalizedHost.startsWith(prefix)) return normalizedHost.slice(prefix.length);
  }
  if (normalizedHost === "localhost" || normalizedHost === "127.0.0.1" || normalizedHost === "::1") return "";
  if (/^\d{1,3}(\.\d{1,3}){3}$/.test(normalizedHost)) return "";
  return normalizedHost;
}

export function resolveManageUrl(): string {
  if (siteConfig.webmailUrl) {
    return new URL("admin", siteConfig.webmailUrl.endsWith("/") ? siteConfig.webmailUrl : `${siteConfig.webmailUrl}/`).toString();
  }
  return resolveManageHost() ? `https://${resolveManageHost()}/admin` : "/admin";
}

export function resolveAdminUrl(): string {
  if (siteConfig.webmailUrl) {
    return new URL("admin", siteConfig.webmailUrl.endsWith("/") ? siteConfig.webmailUrl : `${siteConfig.webmailUrl}/`).toString();
  }
  return resolveAdminHost() ? `https://${resolveAdminHost()}/admin` : "/admin";
}
