import { describe, it, expect, vi, beforeEach } from "vitest";
import { prismaMock, resetPrismaMock } from "@/__tests__/helpers/prisma-mock";
import { createRequest, parseJsonResponse } from "@/__tests__/helpers/api-test-utils";

vi.mock("@/lib/auth", () => ({
  getAuthUserId: vi.fn().mockResolvedValue("user-1"),
}));

vi.mock("@/lib/blindbox", () => ({
  requireMembership: vi.fn().mockResolvedValue({}),
}));

vi.mock("@/lib/ai", () => ({
  suggestIdeas: vi.fn().mockResolvedValue(["去爬山", "骑自行车", "看日出", "野餐"]),
}));

import { GET } from "./route";

const mockCtx = { params: Promise.resolve({}) };

beforeEach(() => {
  resetPrismaMock();
});

describe("GET /api/blindbox/suggest", () => {
  it("returns AI suggestions when enough ideas exist", async () => {
    prismaMock.blindBoxIdea.findMany.mockResolvedValue([
      { content: "去公园" },
      { content: "看电影" },
      { content: "吃火锅" },
    ] as never);

    const req = createRequest("/api/blindbox/suggest?roomId=bb-room-1&userId=user-1");
    const res = await GET(req, mockCtx);
    const { status, data } = await parseJsonResponse(res);

    expect(status).toBe(200);
    expect(data.suggestions).toHaveLength(4);
    expect(data.source).toBe("ai");
  });

  it("returns empty when less than 2 ideas", async () => {
    prismaMock.blindBoxIdea.findMany.mockResolvedValue([
      { content: "去公园" },
    ] as never);

    const req = createRequest("/api/blindbox/suggest?roomId=bb-room-1&userId=user-1");
    const res = await GET(req, mockCtx);
    const { data } = await parseJsonResponse(res);

    expect(data.suggestions).toHaveLength(0);
    expect(data.source).toBe("none");
  });

  it("returns 400 when roomId missing", async () => {
    const req = createRequest("/api/blindbox/suggest?userId=user-1");
    const res = await GET(req, mockCtx);
    expect(res.status).toBe(400);
  });
});
