/**
 * Tests for public testimonials API (NO AUTH)
 * GET /api/public/testimonials/[userId]
 *
 * Route returns: { settings: {...}, testimonials: [...] }
 */

jest.mock("@/lib/services/review-widget.service", () => ({
  listFeaturedReviews: jest.fn(),
  getWidgetSettings: jest.fn(),
}));
jest.mock("@/lib/logger", () => ({
  logger: {
    scope: () => ({
      info: jest.fn(),
      error: jest.fn(),
      warn: jest.fn(),
      debug: jest.fn(),
    }),
  },
}));

import { NextRequest } from "next/server";
import * as widgetService from "@/lib/services/review-widget.service";

function makeReq(url: string) {
  return new NextRequest(`http://localhost:9000${url}`);
}

describe("GET /api/public/testimonials/[userId]", () => {
  let mod: typeof import("@/app/api/public/testimonials/[userId]/route");
  beforeAll(async () => {
    mod = await import("@/app/api/public/testimonials/[userId]/route");
  });
  beforeEach(() => jest.clearAllMocks());

  const makeParams = (userId: string) => Promise.resolve({ userId });

  it("returns testimonials for a user", async () => {
    const mockReviews = [
      { id: "r1", customerName: "Alice", rating: 5, content: "Great!" },
      { id: "r2", customerName: "Bob", rating: 4, content: "Good" },
    ];
    const mockSettings = {
      userId: "user-1",
      businessName: "Test Biz",
      primaryColor: "#10b981",
    };
    (widgetService.listFeaturedReviews as jest.Mock).mockResolvedValue(
      mockReviews,
    );
    (widgetService.getWidgetSettings as jest.Mock).mockResolvedValue(
      mockSettings,
    );

    const res = await mod.GET(makeReq("/api/public/testimonials/user-1"), {
      params: makeParams("user-1"),
    });
    expect(res.status).toBe(200);
    const json = await res.json();
    expect(json.testimonials).toEqual(mockReviews);
    expect(json.settings.businessName).toBe("Test Biz");
    expect(widgetService.listFeaturedReviews).toHaveBeenCalledWith("user-1");
  });

  it("returns empty array for user with no featured reviews", async () => {
    (widgetService.listFeaturedReviews as jest.Mock).mockResolvedValue([]);
    (widgetService.getWidgetSettings as jest.Mock).mockResolvedValue({
      userId: "user-empty",
    });

    const res = await mod.GET(makeReq("/api/public/testimonials/user-empty"), {
      params: makeParams("user-empty"),
    });
    expect(res.status).toBe(200);
    const json = await res.json();
    expect(json.testimonials).toEqual([]);
  });

  it("returns successful response for embedding (CORS set in prod)", async () => {
    (widgetService.listFeaturedReviews as jest.Mock).mockResolvedValue([]);
    (widgetService.getWidgetSettings as jest.Mock).mockResolvedValue({
      userId: "user-1",
    });

    const res = await mod.GET(makeReq("/api/public/testimonials/user-1"), {
      params: makeParams("user-1"),
    });
    // jsdom does not fully support Headers iteration, so we verify the
    // response is successful (CORS headers are verified in e2e tests)
    expect(res.status).toBe(200);
    const json = await res.json();
    expect(json).toHaveProperty("settings");
    expect(json).toHaveProperty("testimonials");
  });

  it("returns 500 on service error", async () => {
    (widgetService.listFeaturedReviews as jest.Mock).mockRejectedValue(
      new Error("DB error"),
    );
    (widgetService.getWidgetSettings as jest.Mock).mockRejectedValue(
      new Error("DB error"),
    );

    const res = await mod.GET(makeReq("/api/public/testimonials/user-1"), {
      params: makeParams("user-1"),
    });
    expect(res.status).toBe(500);
  });
});
