// لوکال: NEXT_PUBLIC_BASE_URL=http://localhost:3002/api/v1
// export const SOCKET_URL = "http://localhost:8000";
// export const BASE_URL = "http://localhost:8000/api/v1";
// export const IMAGE_BASE_URL = "http://localhost:8000/storage";

/** app.modstagram.ir — SSL معتبر برای fetch سمت سرور Next.js */
export const SOCKET_URL =
  process.env.NEXT_PUBLIC_SOCKET_URL ?? "https://app.modstagram.ir";
export const BASE_URL =
  process.env.NEXT_PUBLIC_BASE_URL ?? "https://app.modstagram.ir/api/v1";
export const IMAGE_BASE_URL =
  process.env.NEXT_PUBLIC_IMAGE_BASE_URL ?? "https://app.modstagram.ir/storage";

/** ساخت URL کامل فایل storage — مسیرهای DB را یکسان می‌کند */
export function buildStorageUrl(path: string | null | undefined): string {
  if (!path) return "";
  if (path.startsWith("http://") || path.startsWith("https://")) return path;

  let normalized = String(path).replace(/\\/g, "/");
  normalized = normalized.replace("/root/modstagram-back/storage", "");

  if (normalized.startsWith("/storage/")) {
    normalized = normalized.slice("/storage".length);
  } else if (normalized.startsWith("storage/")) {
    normalized = `/${normalized.slice("storage/".length)}`;
  }

  if (!normalized.startsWith("/")) normalized = `/${normalized}`;
  return `${IMAGE_BASE_URL.replace(/\/$/, "")}${normalized}`;
}
