import path from "path";

import { describe, expect, it } from "vitest";

import {
  MAX_MEDIA_FILE_SIZE,
  MEDIA_UPLOAD_ROOT,
  getExtensionForMimeType,
  isManagedMediaFilePath,
  removeManagedMediaFile,
  resolveMediaUploadPath,
  sanitizeBaseName,
} from "@/lib/media-storage";

describe("sanitizeBaseName", () => {
  it("lowercases, hyphenates, and strips symbols", () => {
    expect(sanitizeBaseName("Brand Redesign 2026!.svg")).toBe("brand-redesign-2026-svg");
  });

  it("collapses repeated separators and trims edges", () => {
    expect(sanitizeBaseName("--Hello___World--")).toBe("hello-world");
  });

  it("truncates to 60 characters", () => {
    expect(sanitizeBaseName("a".repeat(100)).length).toBe(60);
  });
});

describe("getExtensionForMimeType", () => {
  it("maps known image and document mime types", () => {
    expect(getExtensionForMimeType("image/gif")).toBe(".gif");
    expect(getExtensionForMimeType("image/jpeg")).toBe(".jpg");
    expect(getExtensionForMimeType("image/png")).toBe(".png");
    expect(getExtensionForMimeType("image/webp")).toBe(".webp");
    expect(getExtensionForMimeType("image/svg+xml")).toBe(".svg");
    expect(getExtensionForMimeType("image/x-icon")).toBe(".ico");
    expect(getExtensionForMimeType("image/vnd.microsoft.icon")).toBe(".ico");
    expect(getExtensionForMimeType("application/pdf")).toBe(".pdf");
  });

  it("returns null for unknown mime types", () => {
    expect(getExtensionForMimeType("application/zip")).toBeNull();
    expect(getExtensionForMimeType("")).toBeNull();
  });
});

describe("isManagedMediaFilePath", () => {
  it("accepts managed upload paths only", () => {
    expect(isManagedMediaFilePath("/uploads/media/covers/x.svg")).toBe(true);
    expect(isManagedMediaFilePath("https://example.com/x.svg")).toBe(false);
    expect(isManagedMediaFilePath("../x.svg")).toBe(false);
    expect(isManagedMediaFilePath("/uploads/other/x.svg")).toBe(false);
    expect(isManagedMediaFilePath(null)).toBe(false);
    expect(isManagedMediaFilePath(undefined)).toBe(false);
  });
});

describe("resolveMediaUploadPath", () => {
  it("resolves managed paths inside the upload root", () => {
    const resolved = resolveMediaUploadPath("/uploads/media/assets/test.svg");
    expect(resolved.startsWith(MEDIA_UPLOAD_ROOT)).toBe(true);
    expect(resolved.endsWith(path.join("assets", "test.svg"))).toBe(true);
  });

  it("throws for unmanaged paths", () => {
    expect(() => resolveMediaUploadPath("https://example.com/x.svg")).toThrow(/managed/i);
  });

  it("throws when a traversal attempt escapes the root", () => {
    expect(() => resolveMediaUploadPath("/uploads/media/../../etc/passwd")).toThrow(/escapes/i);
  });
});

describe("removeManagedMediaFile", () => {
  it("returns false without touching disk for unmanaged paths", async () => {
    await expect(removeManagedMediaFile("https://example.com/x.svg")).resolves.toBe(false);
    await expect(removeManagedMediaFile(null)).resolves.toBe(false);
  });
});

describe("constants", () => {
  it("caps media uploads at 5 MB", () => {
    expect(MAX_MEDIA_FILE_SIZE).toBe(5 * 1024 * 1024);
  });
});
