import { beforeEach, describe, expect, it, vi } from "vitest";
import { prisma, __resetPrisma } from "../mocks/prisma";

const checkAdminAuth = vi.fn();
const authCheckPath = vi.hoisted(() => new URL("../../lib/auth-check.ts", import.meta.url).pathname);
const prismaPath = vi.hoisted(() => new URL("../../lib/prisma.ts", import.meta.url).pathname);
vi.mock(authCheckPath, () => ({ checkAdminAuth }));
vi.mock(prismaPath, () => ({ prisma }));

beforeEach(async () => {
  await vi.resetModules();
  __resetPrisma();
  checkAdminAuth.mockResolvedValue(null);
});

describe("Admin category images API", () => {
  it("POST /api/admin/category-images upserts image", async () => {
    prisma.categoryImage.upsert.mockResolvedValueOnce({ id: "c1" });
    const { POST } = await import("../../app/api/admin/category-images/route");

    const res = await POST(
      new Request("http://localhost/api/admin/category-images", {
        method: "POST",
        body: JSON.stringify({ categoryName: "CAT", imageUrl: "url" }),
        headers: { "content-type": "application/json" },
      })
    );

    expect(res.status).toBe(201);
  });

  it("DELETE /api/admin/category-images/[categoryName] deletes image", async () => {
    prisma.categoryImage.delete.mockResolvedValueOnce({ id: "c1" });
    const { DELETE } = await import("../../app/api/admin/category-images/[categoryName]/route");

    const res = await DELETE(new Request("http://localhost/api/admin/category-images/CAT"), {
      params: Promise.resolve({ categoryName: "CAT" }),
    });

    const data = await res.json();
    expect(data.success).toBe(true);
  });
});
