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

import {
  getKindFromUploadFile,
  inferMediaKindFromFileName,
  inferMediaKindFromMimeType,
} from "@/lib/media-service";

describe("inferMediaKindFromMimeType", () => {
  it("classifies image mime types as IMAGE", () => {
    expect(inferMediaKindFromMimeType("image/png")).toBe("IMAGE");
    expect(inferMediaKindFromMimeType("image/svg+xml")).toBe("IMAGE");
    expect(inferMediaKindFromMimeType("image/gif")).toBe("IMAGE");
  });

  it("classifies everything else as DOCUMENT", () => {
    expect(inferMediaKindFromMimeType("application/pdf")).toBe("DOCUMENT");
    expect(inferMediaKindFromMimeType(null)).toBe("DOCUMENT");
    expect(inferMediaKindFromMimeType(undefined)).toBe("DOCUMENT");
    expect(inferMediaKindFromMimeType("")).toBe("DOCUMENT");
  });
});

describe("inferMediaKindFromFileName", () => {
  it("treats known image extensions as IMAGE (case-insensitive)", () => {
    for (const name of ["a.gif", "a.jpg", "a.jpeg", "a.PNG", "a.webp", "a.SVG"]) {
      expect(inferMediaKindFromFileName(name)).toBe("IMAGE");
    }
  });

  it("treats other extensions as DOCUMENT", () => {
    expect(inferMediaKindFromFileName("report.pdf")).toBe("DOCUMENT");
    expect(inferMediaKindFromFileName("noext")).toBe("DOCUMENT");
  });
});

describe("getKindFromUploadFile", () => {
  it("derives IMAGE from an image mime type", () => {
    const file = new File([new Uint8Array([1])], "logo.png", { type: "image/png" });
    expect(getKindFromUploadFile(file)).toBe("IMAGE");
  });

  it("derives DOCUMENT from a pdf mime type", () => {
    const file = new File([new Uint8Array([1])], "doc.pdf", { type: "application/pdf" });
    expect(getKindFromUploadFile(file)).toBe("DOCUMENT");
  });

  it("falls back to DOCUMENT for unknown mime types", () => {
    const file = new File([new Uint8Array([1])], "thing.bin", { type: "application/octet-stream" });
    expect(getKindFromUploadFile(file)).toBe("DOCUMENT");
  });
});
