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

const httpClientMock = vi.hoisted(() => vi.fn());

vi.mock("../http_client", () => ({
  httpClient: httpClientMock,
}));

import { FeedbackApi } from "../feedback_api";

describe("FeedbackApi", () => {
  beforeEach(() => {
    httpClientMock.mockReset();
  });

  it("submits the supported fields as multipart data", async () => {
    httpClientMock.mockResolvedValue({
      success: true,
      data: { feedbackId: "feedback-123", ignored: "server-only" },
    });
    const image = new File(["image"], "screen.jpg", {
      type: "image/jpeg",
    });

    const response = await new FeedbackApi().submit({
      category: "problem",
      content: "The send button did not respond.",
      context: {
        appVersion: "0.1.0",
        platform: "android Android 15",
        browser: "facebook IAB / Chrome 135",
        viewport: "392x760@2.75",
      },
      images: [image],
      idempotencyKey: "payment_feedback_123",
    });

    expect(response).toEqual({ feedbackId: "feedback-123" });
    expect(httpClientMock).toHaveBeenCalledWith(
      "/api/feedback",
      expect.objectContaining({
        method: "POST",
        body: expect.any(FormData),
        headers: { "Idempotency-Key": "payment_feedback_123" },
      }),
    );
    const body = httpClientMock.mock.calls[0][1].body as FormData;
    expect(body.get("category")).toBe("problem");
    expect(body.get("content")).toBe("The send button did not respond.");
    expect(JSON.parse(String(body.get("context")))).toEqual({
      appVersion: "0.1.0",
      platform: "android Android 15",
      browser: "facebook IAB / Chrome 135",
      viewport: "392x760@2.75",
    });
    expect(body.getAll("images")).toEqual([image]);
  });
});
