/**
 * @vitest-environment jsdom
 */
import { act, renderHook, waitFor } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import type { TWorkflowListItem } from "@formbricks/workflows";
import type { TWorkflowListPage } from "../lib/api-client";
import { workflowKeys } from "../lib/query";
import { createWrapper, newQueryClient } from "./test-utils";
import { useArchiveWorkflow } from "./use-archive-workflow";

const queryKey = workflowKeys.list({ workspaceId: "ws_1", limit: 12, nameContains: "" });

const workflowListItem = (id: string, name: string): TWorkflowListItem => ({
  id,
  workspaceId: "ws_1",
  name,
  description: null,
  status: "draft",
  triggerType: "response.completed",
  surveyId: "survey_1",
  createdBy: null,
  creator: null,
  createdAt: "2026-06-11T09:30:00.000Z",
  updatedAt: "2026-06-11T09:30:00.000Z",
  lastRun: null,
  runCount: 0,
});

const seedData = (): { pages: TWorkflowListPage[]; pageParams: (string | null)[] } => ({
  pages: [
    {
      data: [workflowListItem("wf_1", "One"), workflowListItem("wf_2", "Two")],
      meta: { limit: 12, nextCursor: null },
    },
  ],
  pageParams: [null],
});

describe("useArchiveWorkflow", () => {
  beforeEach(() => {
    (globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT =
      true;
    vi.stubGlobal("fetch", vi.fn());
  });

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

  test("optimistically drops the archived workflow from the default list and invalidates on success", async () => {
    let resolveFetch: ((value: Response) => void) | undefined;
    const fetchPromise = new Promise<Response>((resolve) => {
      resolveFetch = resolve;
    });
    vi.mocked(global.fetch).mockReturnValue(fetchPromise);

    const queryClient = newQueryClient();
    queryClient.setQueryData(queryKey, seedData());
    const invalidateSpy = vi.spyOn(queryClient, "invalidateQueries");

    const { result } = renderHook(() => useArchiveWorkflow({ queryKey }), {
      wrapper: createWrapper(queryClient),
    });

    result.current.mutate({ workflowId: "wf_2" });

    await waitFor(() =>
      expect(
        queryClient.getQueryData<{ pages: TWorkflowListPage[] }>(queryKey)?.pages[0]?.data.map((w) => w.id)
      ).toEqual(["wf_1"])
    );

    resolveFetch?.(
      new Response(JSON.stringify({ data: { id: "wf_2", status: "archived" } }), {
        status: 200,
        headers: { "Content-Type": "application/json" },
      })
    );

    await waitFor(() => expect(result.current.isSuccess).toBe(true));
    expect(invalidateSpy).toHaveBeenCalledWith({ queryKey: workflowKeys.lists() });
  });

  test("rolls the cache back when archive fails", async () => {
    vi.mocked(global.fetch).mockResolvedValue(
      new Response(
        JSON.stringify({
          status: 422,
          detail: "Workflow is already archived.",
          code: "invalid_workflow_state",
        }),
        { status: 422, headers: { "Content-Type": "application/problem+json" } }
      )
    );

    const queryClient = newQueryClient();
    queryClient.setQueryData(queryKey, seedData());

    const { result } = renderHook(() => useArchiveWorkflow({ queryKey }), {
      wrapper: createWrapper(queryClient),
    });

    await expect(
      act(async () => {
        await result.current.mutateAsync({ workflowId: "wf_2" });
      })
    ).rejects.toThrow("Workflow is already archived.");

    expect(
      queryClient.getQueryData<{ pages: TWorkflowListPage[] }>(queryKey)?.pages[0]?.data.map((w) => w.id)
    ).toEqual(["wf_1", "wf_2"]);
  });
});
