import { fireEvent, render, screen } from "@testing-library/react";
import { expect, test, vi } from "vitest";

import { WorkflowLookup } from "@/features/workflows/workflow-lookup";
import type { WorkflowLookupItemVM } from "@/lib/types/view-models";

const WORKFLOW_ITEMS: WorkflowLookupItemVM[] = [
  {
    orderId: 4201,
    workflowId: "wf-4201",
    workflowType: "MidEndPipelineWorkflow",
    status: "waiting_review",
    statusMeta: {
      label: "待审核",
      tone: "warning",
    },
    currentStep: "review",
    currentStepLabel: "人工审核",
    updatedAt: "2026-03-27T09:15:00Z",
  },
];

test("shows the real recent-workflows entry state", () => {
  render(<WorkflowLookup items={WORKFLOW_ITEMS} />);

  expect(screen.getByText("流程追踪首页当前显示真实后端最近流程。")).toBeInTheDocument();
  expect(screen.getByText("订单 #4201")).toBeInTheDocument();
});

test("supports workflow status filtering and pagination actions", () => {
  const onStatusChange = vi.fn();
  const onPageChange = vi.fn();
  const onQuerySubmit = vi.fn();

  render(
    <WorkflowLookup
      currentPage={2}
      items={WORKFLOW_ITEMS}
      onPageChange={onPageChange}
      onQuerySubmit={onQuerySubmit}
      onStatusChange={onStatusChange}
      selectedStatus="waiting_review"
      selectedQuery=""
      totalPages={3}
    />,
  );

  fireEvent.change(screen.getByLabelText("流程关键词搜索"), {
    target: { value: "4201" },
  });
  fireEvent.click(screen.getByRole("button", { name: "搜索流程" }));
  fireEvent.change(screen.getByLabelText("流程状态筛选"), {
    target: { value: "failed" },
  });
  fireEvent.click(screen.getByRole("button", { name: "上一页" }));
  fireEvent.click(screen.getByRole("button", { name: "下一页" }));

  expect(onQuerySubmit).toHaveBeenCalledWith("4201");
  expect(onStatusChange).toHaveBeenCalledWith("failed");
  expect(onPageChange).toHaveBeenNthCalledWith(1, 1);
  expect(onPageChange).toHaveBeenNthCalledWith(2, 3);
});
