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

import { OrdersHome } from "@/features/orders/orders-home";
import type { OrderSummaryVM } from "@/lib/types/view-models";

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

test("shows the real recent-orders entry state", () => {
  render(<OrdersHome recentOrders={RECENT_ORDERS} />);

  expect(screen.getByText("最近订单已接入真实后端接口")).toBeInTheDocument();
  expect(screen.getByText("订单 #4201")).toBeInTheDocument();
});

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

  render(
    <OrdersHome
      currentPage={2}
      onPageChange={onPageChange}
      onQuerySubmit={onQuerySubmit}
      onStatusChange={onStatusChange}
      recentOrders={RECENT_ORDERS}
      selectedStatus="waiting_review"
      selectedQuery=""
      totalPages={3}
    />,
  );

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

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