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

vi.mock("@/lib/prisma", () => ({
  prisma: { user: { findMany: vi.fn().mockResolvedValue([]) } },
}));

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

import { GET } from "./route";
import { buildRoomStatus } from "@/lib/buildRoomStatus";

const mockBuildRoomStatus = vi.mocked(buildRoomStatus);

beforeEach(() => {
  vi.clearAllMocks();
});

describe("GET /api/room/[id]", () => {
  it("returns room status", async () => {
    mockBuildRoomStatus.mockResolvedValue({
      roomId: "ROOM01",
      userCount: 2,
      match: null,
      matchType: null,
      matchLikes: 0,
      runnerUps: [],
      likeCounts: {},
      swipeCounts: {},
      restaurants: [],
      creatorId: "user-1",
      locked: false,
      users: ["user-1"],
      userProfiles: {},
      scene: "eat",
    });

    const req = createRequest("/api/room/ROOM01");
    const ctx = createRouteContext({ id: "ROOM01" });
    const res = await GET(req, ctx);
    const { status, data } = await parseJsonResponse(res);

    expect(status).toBe(200);
    expect(data.roomId).toBe("ROOM01");
  });

  it("returns 404 for nonexistent room", async () => {
    mockBuildRoomStatus.mockResolvedValue(null);

    const req = createRequest("/api/room/NONEXIST");
    const ctx = createRouteContext({ id: "NONEXIST" });
    const res = await GET(req, ctx);
    expect(res.status).toBe(404);
  });
});
