import { afterEach, describe, expect, test, vi } from "vitest";
import type { TV3CreateSurveyBody } from "@/app/api/v3/surveys/schemas";
import type { V3ApiError } from "@/modules/api/lib/v3-client";
import {
  buildSurveyListSearchParams,
  createV3Survey,
  deleteSurvey,
  generateSurveyCreatePayload,
  getSurveyVisibility,
  renameSurvey,
  updateSurveyVisibility,
  validateSurveyCreatePayload,
} from "./v3-surveys-client";

const surveyPayload = {
  workspaceId: "cm4workspace0000000000000000",
  type: "link",
  name: "Generated survey",
  status: "draft",
  languages: [{ code: "en-US", default: true }],
  blocks: [],
  endings: [],
} as unknown as TV3CreateSurveyBody;

afterEach(() => {
  vi.restoreAllMocks();
  vi.unstubAllGlobals();
});

describe("buildSurveyListSearchParams", () => {
  test("emits only supported v3 params using normalized filter values", () => {
    const searchParams = buildSurveyListSearchParams({
      workspaceId: "env_1",
      limit: 20,
      cursor: "cursor_1",
      filters: {
        name: "  Product feedback  ",
        status: ["paused", "draft"],
        type: ["link", "app"],
        visibility: [],
        sortBy: "relevance",
      },
    });

    expect(searchParams.toString()).toBe(
      "workspaceId=env_1&limit=20&sortBy=relevance&cursor=cursor_1&filter%5Bname%5D%5Bcontains%5D=Product+feedback&filter%5Bstatus%5D%5Bin%5D=draft&filter%5Bstatus%5D%5Bin%5D=paused&filter%5Btype%5D%5Bin%5D=app&filter%5Btype%5D%5Bin%5D=link"
    );
  });

  test("emits includeTotalCount=false when requested", () => {
    const searchParams = buildSurveyListSearchParams({
      workspaceId: "env_1",
      limit: 20,
      cursor: "cursor_1",
      includeTotalCount: false,
      filters: {
        name: "",
        status: [],
        type: [],
        visibility: [],
        sortBy: "relevance",
      },
    });

    expect(searchParams.toString()).toBe(
      "workspaceId=env_1&limit=20&sortBy=relevance&cursor=cursor_1&includeTotalCount=false"
    );
  });
});

describe("buildSurveyListSearchParams visibility", () => {
  test("emits one filter[visibility][in] per normalized value", () => {
    const searchParams = buildSurveyListSearchParams({
      workspaceId: "env_1",
      limit: 20,
      filters: {
        name: "",
        status: [],
        type: [],
        visibility: ["workspace", "restricted", "workspace"],
        sortBy: "relevance",
      },
    });

    expect(searchParams.getAll("filter[visibility][in]")).toEqual(["restricted", "workspace"]);
  });

  test("emits no visibility param when the filter is empty", () => {
    const searchParams = buildSurveyListSearchParams({
      workspaceId: "env_1",
      limit: 20,
      filters: { name: "", status: [], type: [], visibility: [], sortBy: "relevance" },
    });

    expect(searchParams.has("filter[visibility][in]")).toBe(false);
  });
});

describe("v3 survey write helpers", () => {
  test("generates a survey create payload", async () => {
    const fetchMock = vi.spyOn(globalThis, "fetch").mockResolvedValue(
      new Response(
        JSON.stringify({
          data: {
            language: "en-US",
            payload: surveyPayload,
            validation: {
              valid: true,
              invalid_params: [],
              languages: [{ code: "en-US", default: true, enabled: true }],
            },
          },
        }),
        { status: 200 }
      )
    );

    const result = await generateSurveyCreatePayload({
      workspaceId: "cm4workspace0000000000000000",
      prompt: "Measure whether onboarding explains the core product value clearly.",
      type: "link",
      language: "en-US",
    });

    expect(fetchMock).toHaveBeenCalledWith("/api/v3/surveys/generate", {
      method: "POST",
      cache: "no-store",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        workspaceId: "cm4workspace0000000000000000",
        prompt: "Measure whether onboarding explains the core product value clearly.",
        type: "link",
        language: "en-US",
      }),
    });
    expect(result.language).toBe("en-US");
    expect(result.payload).toEqual(surveyPayload);
    expect(result.validation.valid).toBe(true);
    expect(result.validation.languages).toEqual([{ code: "en-US", default: true, enabled: true }]);
  });

  test("validates a generated create payload", async () => {
    const fetchMock = vi.spyOn(globalThis, "fetch").mockResolvedValue(
      new Response(
        JSON.stringify({
          data: {
            valid: true,
            operation: "create",
            invalid_params: [],
          },
        }),
        { status: 200 }
      )
    );

    const result = await validateSurveyCreatePayload(surveyPayload);

    expect(fetchMock).toHaveBeenCalledWith("/api/v3/surveys/validate", {
      method: "POST",
      cache: "no-store",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        operation: "create",
        data: surveyPayload,
      }),
    });
    expect(result.valid).toBe(true);
  });

  test("creates the generated survey draft", async () => {
    const fetchMock = vi.spyOn(globalThis, "fetch").mockResolvedValue(
      new Response(
        JSON.stringify({
          data: {
            id: "survey_1",
          },
        }),
        { status: 201 }
      )
    );

    const result = await createV3Survey(surveyPayload);

    expect(fetchMock).toHaveBeenCalledWith("/api/v3/surveys", {
      method: "POST",
      cache: "no-store",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify(surveyPayload),
    });
    expect(result).toEqual({ id: "survey_1" });
  });
});

describe("deleteSurvey", () => {
  test("treats 204 No Content as a successful delete", async () => {
    const fetchMock = vi.fn().mockResolvedValue(new Response(null, { status: 204 }));
    vi.stubGlobal("fetch", fetchMock);

    await expect(deleteSurvey("survey_1")).resolves.toBeUndefined();

    expect(fetchMock).toHaveBeenCalledWith("/api/v3/surveys/survey_1", {
      method: "DELETE",
      cache: "no-store",
    });
  });

  test("maps v3 problem responses to V3ApiError", async () => {
    vi.stubGlobal(
      "fetch",
      vi.fn().mockResolvedValue(
        Response.json(
          {
            status: 403,
            detail: "You are not authorized to access this resource",
            code: "forbidden",
          },
          { status: 403 }
        )
      )
    );

    const expectedError: Partial<V3ApiError> = {
      status: 403,
      detail: "You are not authorized to access this resource",
      code: "forbidden",
    };
    await expect(deleteSurvey("survey_1")).rejects.toMatchObject(expectedError);
  });
});

describe("renameSurvey", () => {
  test("PATCHes only the name and returns the updated survey", async () => {
    const fetchMock = vi
      .fn()
      .mockResolvedValue(Response.json({ data: { id: "survey_1", name: "Q3 feedback" } }, { status: 200 }));
    vi.stubGlobal("fetch", fetchMock);

    await expect(renameSurvey("survey_1", "Q3 feedback")).resolves.toEqual({
      id: "survey_1",
      name: "Q3 feedback",
    });

    expect(fetchMock).toHaveBeenCalledWith("/api/v3/surveys/survey_1", {
      method: "PATCH",
      cache: "no-store",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ name: "Q3 feedback" }),
    });
  });

  test("maps v3 problem responses to V3ApiError", async () => {
    vi.stubGlobal(
      "fetch",
      vi.fn().mockResolvedValue(
        Response.json(
          {
            status: 422,
            detail: "Survey name is required",
            code: "unprocessable_entity",
          },
          { status: 422 }
        )
      )
    );

    const expectedError: Partial<V3ApiError> = {
      status: 422,
      detail: "Survey name is required",
      code: "unprocessable_entity",
    };
    await expect(renameSurvey("survey_1", " ")).rejects.toMatchObject(expectedError);
  });
});

describe("survey visibility", () => {
  const visibilityState = {
    id: "survey_1",
    visibility: "workspace",
    owner: { name: "Ada" },
    access: { via: "workspace", canManageVisibility: true },
    blockers: [],
    impact: { memberCount: 4, responseCount: 12 },
    pending: null,
    version: 3,
    allowedTargets: ["restricted"],
  };

  test("GETs the visibility sub-resource", async () => {
    const fetchMock = vi.fn().mockResolvedValue(Response.json({ data: visibilityState }, { status: 200 }));
    vi.stubGlobal("fetch", fetchMock);

    await expect(getSurveyVisibility("survey_1")).resolves.toEqual(visibilityState);
    expect(fetchMock).toHaveBeenCalledWith("/api/v3/surveys/survey_1/visibility", {
      method: "GET",
      cache: "no-store",
    });
  });

  test("POSTs only the requested visibility and returns the change result", async () => {
    const result = {
      id: "survey_1",
      visibility: "restricted",
      owner: { name: "Ada" },
      access: { via: "owner", canManageVisibility: true },
      version: 4,
      pending: null,
      changedAt: "2026-09-29T10:00:00.000Z",
      changedBy: { id: "user_1", name: "Ada", type: "user" },
    };
    const fetchMock = vi.fn().mockResolvedValue(Response.json({ data: result }, { status: 200 }));
    vi.stubGlobal("fetch", fetchMock);

    await expect(updateSurveyVisibility("survey_1", "restricted")).resolves.toEqual(result);
    expect(fetchMock).toHaveBeenCalledWith("/api/v3/surveys/survey_1/visibility", {
      method: "POST",
      cache: "no-store",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ visibility: "restricted" }),
    });
  });

  test.each([
    [getSurveyVisibility, 403, "visibility_not_enabled"],
    [(id: string) => updateSurveyVisibility(id, "restricted"), 409, "visibility_blocked_by_connections"],
    [(id: string) => updateSurveyVisibility(id, "workspace"), 503, "projection_pending"],
  ])("maps a %#th problem response to V3ApiError", async (call, status, code) => {
    vi.stubGlobal(
      "fetch",
      vi.fn().mockResolvedValue(Response.json({ status, detail: "Nope", code }, { status }))
    );

    const expectedError: Partial<V3ApiError> = { status, code };
    await expect(call("survey_1")).rejects.toMatchObject(expectedError);
  });
});
