const DEFAULT_BACKEND_ORIGIN = "https://news.vccihcm.vn";
const DEFAULT_FRONTEND_ORIGIN = "https://news.vccihcm.vn";
const LEGACY_MEDIA_HOSTS = new Set([
  "vietprodev.duckdns.org",
  "vcci-hcm.org.vn",
  "vccihcm.vn",
]);

const normalizeOrigin = (value?: string | null) => value?.trim().replace(/\/+$/, "") || "";

const extractUploadPath = (pathname: string) => {
  const markers = ["/api/uploads/", "/uploads/", "/wp-content/uploads/"];

  for (const marker of markers) {
    const index = pathname.indexOf(marker);
    if (index >= 0) {
      const cleanPath = pathname.slice(index).replace(/^\/api\/uploads\//, "/uploads/");
      return cleanPath;
    }
  }

  return null;
};

const readOrigin = (key: "NEXT_PUBLIC_BACKEND_HOST" | "NEXT_PUBLIC_FRONTEND_HOST") => {
  const envOrigin = normalizeOrigin(
    key === "NEXT_PUBLIC_BACKEND_HOST"
      ? process.env.NEXT_PUBLIC_BACKEND_HOST
      : process.env.NEXT_PUBLIC_FRONTEND_HOST,
  );
  if (envOrigin) return envOrigin;
  if (key === "NEXT_PUBLIC_BACKEND_HOST" && process.env.NODE_ENV === "production") {
    return DEFAULT_BACKEND_ORIGIN;
  }
  if (key === "NEXT_PUBLIC_FRONTEND_HOST" && process.env.NODE_ENV === "production") {
    return DEFAULT_FRONTEND_ORIGIN;
  }

  if (typeof window !== "undefined" && key === "NEXT_PUBLIC_FRONTEND_HOST") {
    return normalizeOrigin(window.location.origin);
  }

  if (process.env.NODE_ENV !== "production") {
    console.warn(`${key} is missing`);
  }

  return "";
};

const toUrl = (value: string) => {
  if (!value) return null;

  try {
    return new URL(value);
  } catch {
    if (process.env.NODE_ENV !== "production") {
      console.warn(`Invalid origin: ${value}`);
    }
    return null;
  }
};

const backendOrigin = readOrigin("NEXT_PUBLIC_BACKEND_HOST");
const frontendOrigin = readOrigin("NEXT_PUBLIC_FRONTEND_HOST");

const backendUrl = toUrl(backendOrigin);
const frontendUrl = toUrl(frontendOrigin);
const uploadsEndpoint = backendOrigin ? `${backendOrigin}/uploads/` : "/uploads/";

export const resolveUploadUrl = (value?: string | null) => {
  const trimmed = value?.trim();

  if (!trimmed) return "";
  if (
    trimmed.startsWith("http://") ||
    trimmed.startsWith("https://") ||
    trimmed.startsWith("blob:") ||
    trimmed.startsWith("data:")
  ) {
    return trimmed;
  }

  if (/^https?:\/\//i.test(trimmed)) {
    try {
      const url = new URL(trimmed);
      if (LEGACY_MEDIA_HOSTS.has(url.hostname)) {
        const uploadPath = extractUploadPath(url.pathname);
        if (uploadPath) {
          return backendOrigin ? `${backendOrigin}${uploadPath}` : uploadPath;
        }
      }
    } catch {
      return trimmed;
    }

    return trimmed;
  }

  if (trimmed.startsWith("/")) {
    if (trimmed.startsWith("/uploads/") || trimmed.startsWith("/api/uploads/")) {
      const cleanPath = trimmed.replace(/^\/+/, "").replace(/^api\/uploads\//, "uploads/");
      return backendOrigin ? `${backendOrigin}/${cleanPath}` : `/${cleanPath}`;
    }

    return trimmed;
  }

  const cleanPath = trimmed.replace(/^\/+/, "").replace(/^api\/uploads\//, "uploads/");
  if (cleanPath.startsWith("uploads/")) {
    return backendOrigin ? `${backendOrigin}/${cleanPath}` : `/${cleanPath}`;
  }

  return `${uploadsEndpoint}${cleanPath}`;
};

const links = {
  analyticsGoogle: "G-C9TEK9BS4C",
  apiEndpoint: backendOrigin ? `${backendOrigin}/api/v1.0` : "/api/v1.0",
  imageEndpoint: uploadsEndpoint,
  resolveUploadUrl,
  backendHost: backendUrl?.hostname || "",
  backendProtocol: backendUrl?.protocol.replace(":", "") || "",
  backendPathname: backendUrl?.pathname.replace(/\/+$/, "") || "/",
  siteURL: frontendUrl ? `${frontendUrl.origin}/` : "/",
};

export default links;
